[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 | |
---|
| 16 | using namespace std; |
---|
| 17 | using namespace network; |
---|
| 18 | |
---|
| 19 | PacketGenerator::PacketGenerator() {} |
---|
| 20 | |
---|
[223] | 21 | //following functions create a packet in form of bytestream |
---|
| 22 | |
---|
[199] | 23 | ENetPacket* PacketGenerator::acknowledgement( int state, int reliable ) |
---|
| 24 | { |
---|
| 25 | cout << "generating new acknowledgement" << endl; |
---|
| 26 | ack* ackreq = new ack; |
---|
| 27 | ackreq->id = ACK; |
---|
| 28 | ackreq->a = state; |
---|
| 29 | |
---|
| 30 | ENetPacket *packet = enet_packet_create( ackreq , sizeof( *ackreq ), reliable ); |
---|
| 31 | |
---|
| 32 | return packet; |
---|
| 33 | } |
---|
[223] | 34 | /*### mouseupdates */ |
---|
[199] | 35 | ENetPacket* PacketGenerator::mousem( double x, double y, int reliable ) |
---|
| 36 | { |
---|
| 37 | cout << "generating new mouse" << endl; |
---|
| 38 | mouse* mousemove = new mouse; |
---|
| 39 | mousemove->id = MOUSE; |
---|
| 40 | mousemove->x = x; |
---|
| 41 | mousemove->y = y; |
---|
| 42 | |
---|
| 43 | ENetPacket *packet = enet_packet_create( mousemove , sizeof( *mousemove ), reliable ); |
---|
| 44 | |
---|
| 45 | return packet; |
---|
| 46 | } |
---|
[223] | 47 | /*### keystrikes updates */ |
---|
[199] | 48 | ENetPacket* PacketGenerator::keystrike( char press, int reliable ) |
---|
| 49 | { |
---|
| 50 | cout << "generating new keyboard" << endl; |
---|
| 51 | keyboard* key = new keyboard; |
---|
| 52 | key->id = KEYBOARD; |
---|
| 53 | key->press = press; |
---|
| 54 | |
---|
| 55 | ENetPacket *packet = enet_packet_create( key , sizeof( *key ), reliable ); |
---|
| 56 | |
---|
| 57 | return packet; |
---|
| 58 | } |
---|
[223] | 59 | /*### chat messages packet */ |
---|
[199] | 60 | ENetPacket* PacketGenerator::chatMessage( const char* message, int reliable ) |
---|
| 61 | { |
---|
| 62 | int* trans = new int[sizeof(int) + strlen(message) + 1]; |
---|
| 63 | *trans = CHAT; |
---|
[223] | 64 | //be carefull here, don't forget to allocate the space before using it ;-) |
---|
[199] | 65 | memcpy( &trans[1], (const void*)message, strlen( message ) + 1); |
---|
| 66 | ENetPacket *packet = enet_packet_create( trans , sizeof( int ) + strlen( message ) + 1, reliable ); |
---|
| 67 | |
---|
| 68 | return packet; |
---|
| 69 | } |
---|
| 70 | |
---|
[332] | 71 | /*### gamestate packet */ |
---|
| 72 | ENetPacket* PacketGenerator::gstate( GameState* states, int reliable ) |
---|
| 73 | { |
---|
| 74 | int* gid; *gid = GAMESTATE; //first assign the correct enet id |
---|
| 75 | int totalLen = 3*sizeof( int ) + states->size; //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 gamestate id |
---|
| 79 | //this is the size of the gamestate data, place at 3th position of the enet datastream |
---|
| 80 | memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->size), sizeof(int)); |
---|
| 81 | //place the gamestate data at the end of the enet datastream |
---|
| 82 | memcpy( (void*)(data+3*sizeof( int )), (const void*)states->data, states->size ); |
---|
| 83 | //create an enet packet with the generated bytestream |
---|
| 84 | ENetPacket *packet = enet_packet_create( data , totalLen, reliable ); |
---|
| 85 | |
---|
| 86 | return packet; |
---|
| 87 | } |
---|