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