1 | /* |
---|
2 | *Class generates packets that can be send by enet |
---|
3 | * |
---|
4 | * Autor: Dumeni Manatschal |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #include "PacketManager.h" |
---|
9 | #include "enet/enet.h" |
---|
10 | #include <iostream> |
---|
11 | #include <list> |
---|
12 | #include <string> |
---|
13 | #include <cstring> |
---|
14 | |
---|
15 | using namespace std; |
---|
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 | cout << "generating new acknowledgement" << 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 | cout << "generating new mouse" << 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 | cout << "generating new keyboard" << 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 | |
---|