| 1 | #ifndef _MESSAGE_PROCESSOR_H
|
|---|
| 2 | #define _MESSAGE_PROCESSOR_H
|
|---|
| 3 |
|
|---|
| 4 | #include <map>
|
|---|
| 5 |
|
|---|
| 6 | #include "Compiler.h"
|
|---|
| 7 |
|
|---|
| 8 | #if defined WINDOWS
|
|---|
| 9 | #include <winsock2.h>
|
|---|
| 10 | #include <WS2tcpip.h>
|
|---|
| 11 | #elif defined LINUX
|
|---|
| 12 | #include <netinet/in.h>
|
|---|
| 13 | #endif
|
|---|
| 14 |
|
|---|
| 15 | #define MSG_TYPE_ACK 1
|
|---|
| 16 | #define MSG_TYPE_REGISTER 2
|
|---|
| 17 | #define MSG_TYPE_LOGIN 3
|
|---|
| 18 | #define MSG_TYPE_LOGOUT 4
|
|---|
| 19 | #define MSG_TYPE_CHAT 5
|
|---|
| 20 | #define MSG_TYPE_PLAYER 6 // server sends this to update player positions
|
|---|
| 21 | #define MSG_TYPE_PLAYER_MOVE 7 // client sends this when a player wants to move
|
|---|
| 22 | #define MSG_TYPE_OBJECT 8
|
|---|
| 23 | #define MSG_TYPE_REMOVE_OBJECT 9
|
|---|
| 24 | #define MSG_TYPE_PICKUP_FLAG 10
|
|---|
| 25 | #define MSG_TYPE_DROP_FLAG 11
|
|---|
| 26 | #define MSG_TYPE_SCORE 12
|
|---|
| 27 | #define MSG_TYPE_START_ATTACK 13
|
|---|
| 28 | #define MSG_TYPE_ATTACK 14
|
|---|
| 29 | #define MSG_TYPE_PROJECTILE 15
|
|---|
| 30 | #define MSG_TYPE_REMOVE_PROJECTILE 16
|
|---|
| 31 |
|
|---|
| 32 | typedef struct
|
|---|
| 33 | {
|
|---|
| 34 | unsigned int id;
|
|---|
| 35 | unsigned short type;
|
|---|
| 36 | char buffer[256];
|
|---|
| 37 | } NETWORK_MSG;
|
|---|
| 38 |
|
|---|
| 39 | using namespace std;
|
|---|
| 40 |
|
|---|
| 41 | class MessageProcessor {
|
|---|
| 42 | public:
|
|---|
| 43 | MessageProcessor();
|
|---|
| 44 | ~MessageProcessor();
|
|---|
| 45 |
|
|---|
| 46 | int sendMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest);
|
|---|
| 47 | int receiveMessage(NETWORK_MSG *msg, int sock, struct sockaddr_in *dest);
|
|---|
| 48 | void resendUnackedMessages(int sock);
|
|---|
| 49 | void cleanAckedMessages();
|
|---|
| 50 |
|
|---|
| 51 | private:
|
|---|
| 52 | // this should eventually just replace the Message struct
|
|---|
| 53 | class MessageContainer {
|
|---|
| 54 | public:
|
|---|
| 55 | MessageContainer() {
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | MessageContainer(const MessageContainer& mc) {
|
|---|
| 59 | this->msg = mc.msg;
|
|---|
| 60 | this->clientAddr = mc.clientAddr;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | MessageContainer(NETWORK_MSG msg, struct sockaddr_in clientAddr) {
|
|---|
| 64 | this->clientAddr = clientAddr;
|
|---|
| 65 | this->msg = msg;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | ~MessageContainer() {
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | NETWORK_MSG msg;
|
|---|
| 72 | struct sockaddr_in clientAddr;
|
|---|
| 73 | bool isAcked;
|
|---|
| 74 | unsigned long long timeAcked;
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | int lastUsedId;
|
|---|
| 78 |
|
|---|
| 79 | // map from message ids to maps from player addresses to message info
|
|---|
| 80 | map<int, map<unsigned long, MessageContainer> > sentMessages;
|
|---|
| 81 |
|
|---|
| 82 | // map from message ids to the time each mesage was acked
|
|---|
| 83 | map<unsigned int, unsigned long long> ackedMessages;
|
|---|
| 84 | };
|
|---|
| 85 |
|
|---|
| 86 | #endif
|
|---|