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 network; |
---|
17 | |
---|
18 | PacketGenerator::PacketGenerator() {} |
---|
19 | |
---|
20 | //following functions create a packet in form of bytestream |
---|
21 | |
---|
22 | ENetPacket* 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 */ |
---|
34 | ENetPacket* 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 */ |
---|
47 | ENetPacket* 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 */ |
---|
59 | ENetPacket* 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 */ |
---|
71 | ENetPacket* PacketGenerator::gstate( GameStateCompressed* states, int reliable ) |
---|
72 | { |
---|
73 | int* gid = new int; |
---|
74 | *gid = GAMESTATE; //first assign the correct enet id |
---|
75 | int totalLen = 3*sizeof( int ) + 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 | //place the GameStateCompressed data at the end of the enet datastream |
---|
84 | memcpy( (void*)(data+4*sizeof( int )), (const void*)states->data, states->compsize ); |
---|
85 | //create an enet packet with the generated bytestream |
---|
86 | ENetPacket *packet = enet_packet_create( data , totalLen, reliable ); |
---|
87 | |
---|
88 | return packet; |
---|
89 | } |
---|
90 | |
---|
91 | ENetPacket* PacketGenerator::clid( int classid, std::string classname, int reliable ){ |
---|
92 | unsigned char* data = (unsigned char *)malloc(3*sizeof(int)+classname.length()+1); |
---|
93 | *(int *)data = CLASSID; |
---|
94 | *((int *)data+1) = classname.length()+1; |
---|
95 | *((int *)data+2) = classid; |
---|
96 | memcpy( (void *)(data+3*sizeof(int)), classname.c_str(), classname.length()+1); |
---|
97 | ENetPacket *packet = enet_packet_create( data , 3*sizeof(int)+classname.length()+1, reliable ); |
---|
98 | return packet; |
---|
99 | } |
---|
100 | |
---|
101 | |
---|