source: network-game/common/MessageContainer.h@ e437a19

Last change on this file since e437a19 was e6c26b8, checked in by Dmitry Portnoy <dportnoy@…>, 11 years ago

The client dynamically allocates memory for players and passes around a map with player pointers and some includes are now in individual files instead of in Common.h

  • Property mode set to 100644
File size: 2.8 KB
Line 
1#ifndef _MESSAGE_CONTAINER_H
2#define _MESSAGE_CONTAINER_H
3
4#include <string>
5
6#include "Compiler.h"
7
8#if defined WINDOWS
9 #include <winsock2.h>
10#elif defined LINUX
11 #include <netinet/in.h>
12#endif
13
14using namespace std;
15
16#define MSG_TYPE_ACK 1
17#define MSG_TYPE_REGISTER 2
18#define MSG_TYPE_LOGIN 3
19#define MSG_TYPE_LOGOUT 4
20#define MSG_TYPE_CHAT 5
21#define MSG_TYPE_PLAYER 6 // server sends this to update player positions
22#define MSG_TYPE_PLAYER_MOVE 7 // client sends this when a player wants to move
23#define MSG_TYPE_OBJECT 8
24#define MSG_TYPE_REMOVE_OBJECT 9
25#define MSG_TYPE_PICKUP_FLAG 10
26#define MSG_TYPE_DROP_FLAG 11
27#define MSG_TYPE_SCORE 12
28#define MSG_TYPE_START_ATTACK 13
29#define MSG_TYPE_ATTACK 14
30#define MSG_TYPE_PROJECTILE 15
31#define MSG_TYPE_REMOVE_PROJECTILE 16
32#define MSG_TYPE_CREATE_GAME 17
33#define MSG_TYPE_JOIN_GAME 18
34#define MSG_TYPE_LEAVE_GAME 19
35#define MSG_TYPE_GAME_INFO 20
36#define MSG_TYPE_JOIN_GAME_SUCCESS 21
37#define MSG_TYPE_JOIN_GAME_FAILURE 22
38#define MSG_TYPE_JOIN_GAME_ACK 23
39
40typedef struct
41{
42 unsigned int id;
43 unsigned short type;
44 char buffer[256];
45} NETWORK_MSG;
46
47class MessageContainer {
48private:
49 NETWORK_MSG msg;
50 struct sockaddr_in clientAddr;
51 bool isAcked;
52 unsigned long long timeAcked;
53
54public:
55 MessageContainer();
56 MessageContainer(const MessageContainer& mc);
57 MessageContainer(NETWORK_MSG msg, struct sockaddr_in clientAddr);
58 ~MessageContainer();
59
60 bool getAcked();
61 unsigned long long getTimeAcked();
62 NETWORK_MSG* getMessage();
63
64 void setAcked(bool acked);
65 void setTimeAcked(unsigned long long time);
66
67 static string getMsgTypeString(int msgType) {
68 switch(msgType) {
69 case MSG_TYPE_ACK: return "MSG_TYPE_ACK";
70 case MSG_TYPE_REGISTER: return "MSG_TYPE_REGISTER";
71 case MSG_TYPE_LOGIN: return "MSG_TYPE_LOGIN";
72 case MSG_TYPE_LOGOUT: return "MSG_TYPE_LOGOUT";
73 case MSG_TYPE_CHAT: return "MSG_TYPE_CHAT";
74 case MSG_TYPE_PLAYER: return "MSG_TYPE_PLAYER";
75 case MSG_TYPE_PLAYER_MOVE: return "MSG_TYPE_PLAYER_MOVE";
76 case MSG_TYPE_OBJECT: return "MSG_TYPE_OBJECT";
77 case MSG_TYPE_REMOVE_OBJECT: return "MSG_TYPE_REMOVE_OBJECT";
78 case MSG_TYPE_PICKUP_FLAG: return "MSG_TYPE_PICKUP_FLAG";
79 case MSG_TYPE_DROP_FLAG: return "MSG_TYPE_DROP_FLAG";
80 case MSG_TYPE_SCORE: return "MSG_TYPE_SCORE";
81 case MSG_TYPE_START_ATTACK: return "MSG_TYPE_START_ATACK";
82 case MSG_TYPE_ATTACK: return "MSG_TYPE_ATTACK";
83 case MSG_TYPE_PROJECTILE: return "MSG_TYPE_PROJECTILE";
84 case MSG_TYPE_REMOVE_PROJECTILE: return "MSG_TYPE_REMOVE_PROJECTILE";
85 default: return "Unknown";
86 }
87 }
88};
89
90#endif
Note: See TracBrowser for help on using the repository browser.