Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/network/PacketGenerator.cc @ 442

Last change on this file since 442 was 437, checked in by dumenim, 17 years ago

changed PacketGenerator/Decoder with diffed

File size: 3.7 KB
Line 
1/*
2 *Class generates packets that can be send by enet
3 * ->don't read this without the class PacketDecoder, since they belong together
4 *
5 * Autor: Dumeni Manatschal
6 * 
7*/
8
9#include "PacketManager.h"
10#include <enet/enet.h>
11#include <iostream>
12#include <list>
13#include <string>
14#include <cstring>
15
16using namespace network;
17
18PacketGenerator::PacketGenerator() {}
19
20//following functions create a packet in form of bytestream
21
22ENetPacket* PacketGenerator::acknowledgement( int state, int reliable )
23{
24    std::cout << "generating new acknowledgement" << std::endl;
25        ack* ackreq = new ack;
26        ackreq->id = ACK;
27        ackreq->a = state;
28       
29        ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable );
30       
31        return packet;
32}
33/*### mouseupdates */
34ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )
35{
36        std::cout << "generating new mouse" << std::endl;
37        mouse* mousemove = new mouse;
38        mousemove->id = MOUSE;
39        mousemove->x = x;
40        mousemove->y = y;
41       
42        ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable );
43       
44        return packet;
45}
46/*### keystrikes updates */
47ENetPacket* PacketGenerator::keystrike( char press, int reliable )
48{
49        std::cout << "generating new keyboard" << std::endl;
50        keyboard* key = new keyboard;
51        key->id = KEYBOARD;
52        key->press = press;
53       
54        ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable );
55       
56        return packet;
57}
58/*### chat messages packet */
59ENetPacket* PacketGenerator::chatMessage( const char* message, int reliable )
60{
61        int* trans = new int[sizeof(int) + strlen(message) + 1];
62        *trans = CHAT;
63        //be carefull here, don't forget to allocate the space before using it ;-)
64        memcpy( &trans[1], (const void*)message, strlen( message ) + 1);
65        ENetPacket *packet = enet_packet_create( trans , sizeof( int ) + strlen( message ) + 1, reliable );
66       
67        return packet;
68}
69
70/*### gamestate packet */
71ENetPacket* PacketGenerator::gstate( GameStateCompressed* states, int reliable )
72{
73        int* gid = new int; 
74        *gid = GAMESTATE; //first assign the correct enet id
75        int totalLen = 4*sizeof( int ) + sizeof(bool) + states->compsize; //calculate the total size of the datastream memory
76        unsigned char* data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream
77        memcpy( (void*)(data), (const void*)gid, sizeof( int ) ); //this is the enet id
78        memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id
79        //this is the compressed size of the GameStateCompressed data, place at 3th position of the enet datastream
80        memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->compsize), sizeof(int));
81        //this is the uncompressed size of GameStateCompressed data
82        memcpy( (void*)(data+3*sizeof(int)), (const void*)&(states->normsize), sizeof(int));
83        //since there is a new parameter inside GameStateCompressed, change this function to create packet
84        memcpy( (void*)(data+4*sizeof(int)), (const void*)states->diffed, sizeof(bool));
85        //place the GameStateCompressed data at the end of the enet datastream
86        memcpy( (void*)(data+4*sizeof( int ) + sizeof(bool)), (const void*)states->data, states->compsize );
87        //create an enet packet with the generated bytestream
88        ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
89       
90        return packet;
91}
92
93ENetPacket* PacketGenerator::clid( int classid, std::string classname, int reliable ){
94        unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1);
95        *(int *)data = CLASSID;
96        *((int *)data+1) = classname.length()+1;
97        *((int *)data+2) = classid;
98        memcpy( (void *)(data+3*sizeof(int)), classname.c_str(), classname.length()+1);
99        ENetPacket *packet = enet_packet_create( data , 3*sizeof(int)+classname.length()+1, reliable );
100        return packet;
101}
102
103
Note: See TracBrowser for help on using the repository browser.