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