Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changed PacketGenerator/Decoder with diffed

File size: 3.7 KB
RevLine 
[223]1/*
2 *Class generates packets that can be send by enet
[332]3 * ->don't read this without the class PacketDecoder, since they belong together
[223]4 *
5 * Autor: Dumeni Manatschal
6 * 
7*/
8
[199]9#include "PacketManager.h"
[341]10#include <enet/enet.h>
[199]11#include <iostream>
12#include <list>
13#include <string>
14#include <cstring>
15
16using namespace network;
17
18PacketGenerator::PacketGenerator() {}
19
[223]20//following functions create a packet in form of bytestream
21
[199]22ENetPacket* PacketGenerator::acknowledgement( int state, int reliable )
23{
[437]24    std::cout << "generating new acknowledgement" << std::endl;
[199]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}
[223]33/*### mouseupdates */
[199]34ENetPacket* PacketGenerator::mousem( double x, double y, int reliable )
35{
[415]36        std::cout << "generating new mouse" << std::endl;
[199]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}
[223]46/*### keystrikes updates */
[199]47ENetPacket* PacketGenerator::keystrike( char press, int reliable )
48{
[415]49        std::cout << "generating new keyboard" << std::endl;
[199]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}
[223]58/*### chat messages packet */
[199]59ENetPacket* PacketGenerator::chatMessage( const char* message, int reliable )
60{
61        int* trans = new int[sizeof(int) + strlen(message) + 1];
62        *trans = CHAT;
[223]63        //be carefull here, don't forget to allocate the space before using it ;-)
[199]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
[332]70/*### gamestate packet */
[403]71ENetPacket* PacketGenerator::gstate( GameStateCompressed* states, int reliable )
[332]72{
[405]73        int* gid = new int; 
74        *gid = GAMESTATE; //first assign the correct enet id
[437]75        int totalLen = 4*sizeof( int ) + sizeof(bool) + states->compsize; //calculate the total size of the datastream memory
[332]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
[403]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));
[437]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));
[403]85        //place the GameStateCompressed data at the end of the enet datastream
[437]86        memcpy( (void*)(data+4*sizeof( int ) + sizeof(bool)), (const void*)states->data, states->compsize );
[332]87        //create an enet packet with the generated bytestream
88        ENetPacket *packet = enet_packet_create( data , totalLen, reliable );
89       
90        return packet;
91}
[400]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.