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 | |
---|
16 | using namespace std; |
---|
17 | using namespace network; |
---|
18 | |
---|
19 | PacketGenerator::PacketGenerator() {} |
---|
20 | |
---|
21 | //following functions create a packet in form of bytestream |
---|
22 | |
---|
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 | } |
---|
34 | /*### mouseupdates */ |
---|
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 | } |
---|
47 | /*### keystrikes updates */ |
---|
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 | } |
---|
59 | /*### chat messages packet */ |
---|
60 | ENetPacket* PacketGenerator::chatMessage( const char* message, int reliable ) |
---|
61 | { |
---|
62 | int* trans = new int[sizeof(int) + strlen(message) + 1]; |
---|
63 | *trans = CHAT; |
---|
64 | //be carefull here, don't forget to allocate the space before using it ;-) |
---|
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 | |
---|
71 | /*### gamestate packet */ |
---|
72 | ENetPacket* PacketGenerator::gstate( GameStateCompressed* states, int reliable ) |
---|
73 | { |
---|
74 | int* gid = new int; |
---|
75 | *gid = GAMESTATE; //first assign the correct enet id |
---|
76 | int totalLen = 3*sizeof( int ) + states->compsize; //calculate the total size of the datastream memory |
---|
77 | unsigned char* data = (unsigned char*)malloc( totalLen ); //allocate the memory for datastream |
---|
78 | memcpy( (void*)(data), (const void*)gid, sizeof( int ) ); //this is the enet id |
---|
79 | memcpy( (void*)(data+sizeof(int)), (const void*)&(states->id), sizeof(int) ); //the GameStateCompressed id |
---|
80 | //this is the compressed size of the GameStateCompressed data, place at 3th position of the enet datastream |
---|
81 | memcpy( (void*)(data+2*sizeof(int)), (const void*)&(states->compsize), sizeof(int)); |
---|
82 | //this is the uncompressed size of GameStateCompressed data |
---|
83 | memcpy( (void*)(data+3*sizeof(int)), (const void*)&(states->normsize), sizeof(int)); |
---|
84 | //place the GameStateCompressed data at the end of the enet datastream |
---|
85 | memcpy( (void*)(data+4*sizeof( int )), (const void*)states->data, states->compsize ); |
---|
86 | //create an enet packet with the generated bytestream |
---|
87 | ENetPacket *packet = enet_packet_create( data , totalLen, reliable ); |
---|
88 | |
---|
89 | return packet; |
---|
90 | } |
---|
91 | |
---|
92 | ENetPacket* PacketGenerator::clid( int classid, std::string classname, int reliable ){ |
---|
93 | unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1); |
---|
94 | *(int *)data = CLASSID; |
---|
95 | *((int *)data+1) = classname.length()+1; |
---|
96 | *((int *)data+2) = classid; |
---|
97 | memcpy( (void *)(data+3*sizeof(int)), classname.c_str(), classname.length()+1); |
---|
98 | ENetPacket *packet = enet_packet_create( data , 3*sizeof(int)+classname.length()+1, reliable ); |
---|
99 | return packet; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|