[199] | 1 | #ifndef PACKETMANAGER_H_ |
---|
| 2 | #define PACKETMANAGER_H_ |
---|
| 3 | |
---|
| 4 | #include <enet/enet.h> |
---|
[341] | 5 | #include "GameStateManager.h" |
---|
[199] | 6 | |
---|
[223] | 7 | //enum netowk generaly used to set the type ID of a packet |
---|
[199] | 8 | namespace network |
---|
| 9 | { |
---|
| 10 | enum packet_id { |
---|
| 11 | ACK, |
---|
| 12 | MOUSE, |
---|
| 13 | KEYBOARD, |
---|
[332] | 14 | CHAT, |
---|
| 15 | GAMESTATE |
---|
[199] | 16 | }; |
---|
| 17 | |
---|
[223] | 18 | /* |
---|
| 19 | * class to generate packets |
---|
| 20 | * |
---|
[332] | 21 | * @autor: Dumeni Manatschal |
---|
[223] | 22 | * |
---|
| 23 | */ |
---|
[199] | 24 | class PacketGenerator |
---|
| 25 | { |
---|
| 26 | public: |
---|
| 27 | PacketGenerator(); |
---|
[223] | 28 | //call one of this functions out of an instance of PacketGenerator to create a packet |
---|
[199] | 29 | ENetPacket* acknowledgement( int state, int reliable = ENET_PACKET_FLAG_RELIABLE ); |
---|
| 30 | ENetPacket* mousem( double x, double y, int reliable = ENET_PACKET_FLAG_RELIABLE ); |
---|
| 31 | ENetPacket* keystrike( char press, int reliable = ENET_PACKET_FLAG_RELIABLE ); |
---|
| 32 | ENetPacket* chatMessage( const char* message, int reliable = ENET_PACKET_FLAG_RELIABLE ); |
---|
[332] | 33 | ENetPacket* gstate( GameState* states, int reliable = ENET_PACKET_FLAG_RELIABLE ); |
---|
[199] | 34 | private: |
---|
[223] | 35 | //used to set the bytes in the right order |
---|
[199] | 36 | struct ack { |
---|
| 37 | int id; |
---|
| 38 | int a; |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | struct mouse { |
---|
| 42 | int id; |
---|
| 43 | double x; |
---|
| 44 | double y; |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | struct keyboard { |
---|
| 48 | int id; |
---|
| 49 | char press; |
---|
| 50 | }; |
---|
| 51 | }; |
---|
| 52 | |
---|
[223] | 53 | /* |
---|
| 54 | * class used to decode incoming packets |
---|
| 55 | * |
---|
[332] | 56 | * @autor: Dumeni Manatschal |
---|
[223] | 57 | * |
---|
| 58 | */ |
---|
[199] | 59 | class PacketDecoder |
---|
| 60 | { |
---|
| 61 | public: |
---|
| 62 | PacketDecoder(); |
---|
[223] | 63 | //call this function to decode, it calls the right decoding function below |
---|
[203] | 64 | bool elaborate( ENetPacket* packet, int clientId ); |
---|
[199] | 65 | private: |
---|
| 66 | struct ack { |
---|
| 67 | int id; |
---|
| 68 | int a; |
---|
| 69 | }; |
---|
| 70 | |
---|
| 71 | struct mouse { |
---|
| 72 | int id; |
---|
| 73 | double x; |
---|
| 74 | double y; |
---|
| 75 | }; |
---|
| 76 | |
---|
| 77 | struct keyboard { |
---|
| 78 | int id; |
---|
| 79 | char press; |
---|
| 80 | }; |
---|
[223] | 81 | //only in this class, not PacketGenerator, used as pattern to put incoming |
---|
| 82 | //bytes inside |
---|
[199] | 83 | struct chat { |
---|
| 84 | int id; |
---|
| 85 | const char* message; |
---|
| 86 | }; |
---|
| 87 | |
---|
[203] | 88 | void acknowledgement( ENetPacket* packet ); |
---|
| 89 | void mousem( ENetPacket* packet ); |
---|
| 90 | void keystrike( ENetPacket* packet ); |
---|
| 91 | void chatMessage( ENetPacket* packet ); |
---|
[332] | 92 | void gstate( ENetPacket* packet ); |
---|
[199] | 93 | |
---|
| 94 | //print functions |
---|
| 95 | void printAck( ack* data ); |
---|
| 96 | void printMouse( mouse* data ); |
---|
| 97 | void printKey( keyboard* data ); |
---|
| 98 | void printChat( chat* data ); |
---|
[332] | 99 | void printGamestate( GameState* data ); |
---|
[199] | 100 | }; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | #endif /*PACKETMANAGER_H_*/ |
---|