1 // ==============================================================
2 //	This file is part of Glest (www.glest.org)
3 //
4 //	Copyright (C) 2001-2008 Marti�o Figueroa
5 //
6 //	You can redistribute this code and/or modify it under
7 //	the terms of the GNU General Public License as published
8 //	by the Free Software Foundation; either version 2 of the
9 //	License, or (at your option) any later version
10 // ==============================================================
11 
12 #ifndef _GLEST_GAME_NETWORKMESSAGE_H_
13 #define _GLEST_GAME_NETWORKMESSAGE_H_
14 
15 #include "socket.h"
16 #include "game_constants.h"
17 #include "network_types.h"
18 
19 using Shared::Platform::Socket;
20 using Shared::Platform::int8;
21 using Shared::Platform::int16;
22 
23 namespace Glest{ namespace Game{
24 
25 class GameSettings;
26 
27 enum NetworkMessageType{
28 	nmtInvalid,
29 	nmtIntro,
30 	nmtPing,
31 	nmtReady,
32 	nmtLaunch,
33 	nmtCommandList,
34 	nmtText,
35 	nmtQuit,
36 
37 	nmtCount
38 };
39 
40 // =====================================================
41 //	class NetworkMessage
42 // =====================================================
43 
44 class NetworkMessage{
45 public:
~NetworkMessage()46 	virtual ~NetworkMessage(){}
47 	virtual bool receive(Socket* socket)= 0;
48 	virtual void send(Socket* socket) const = 0;
49 
50 protected:
51 	bool receive(Socket* socket, void* data, int dataSize);
52 	void send(Socket* socket, const void* data, int dataSize) const;
53 };
54 
55 // =====================================================
56 //	class NetworkMessageIntro
57 //
58 //	Message sent from the server to the client
59 //	when the client connects and vice versa
60 // =====================================================
61 
62 class NetworkMessageIntro: public NetworkMessage{
63 private:
64 	static const int maxVersionStringSize= 64;
65 	static const int maxNameSize= 16;
66 
67 private:
68 	struct Data{
69 		int8 messageType;
70 		NetworkString<maxVersionStringSize> versionString;
71 		NetworkString<maxNameSize> name;
72 		int16 playerIndex;
73 	};
74 
75 private:
76 	Data data;
77 
78 public:
79 	NetworkMessageIntro();
80 	NetworkMessageIntro(const string &versionString, const string &name, int playerIndex);
81 
getVersionString()82 	string getVersionString() const		{return data.versionString.getString();}
getName()83 	string getName() const				{return data.name.getString();}
getPlayerIndex()84 	int getPlayerIndex() const			{return data.playerIndex;}
85 
86 	virtual bool receive(Socket* socket);
87 	virtual void send(Socket* socket) const;
88 };
89 
90 // =====================================================
91 //	class NetworkMessageReady
92 //
93 //	Message sent at the beggining of the game
94 // =====================================================
95 
96 class NetworkMessageReady: public NetworkMessage{
97 private:
98 	struct Data{
99 		int8 messageType;
100 		int32 checksum;
101 	};
102 
103 private:
104 	Data data;
105 
106 public:
107 	NetworkMessageReady();
108 	NetworkMessageReady(int32 checksum);
109 
getChecksum()110 	int32 getChecksum() const	{return data.checksum;}
111 
112 	virtual bool receive(Socket* socket);
113 	virtual void send(Socket* socket) const;
114 };
115 
116 // =====================================================
117 //	class NetworkMessageLaunch
118 //
119 //	Message sent from the server to the client
120 //	to launch the game
121 // =====================================================
122 
123 class NetworkMessageLaunch: public NetworkMessage{
124 private:
125 	static const int maxStringSize= 256;
126 
127 private:
128 	struct Data{
129 		int8 messageType;
130 		NetworkString<maxStringSize> description;
131 		NetworkString<maxStringSize> map;
132 		NetworkString<maxStringSize> tileset;
133 		NetworkString<maxStringSize> tech;
134 		NetworkString<maxStringSize> factionTypeNames[GameConstants::maxPlayers]; //faction names
135 
136 		int8 factionControls[GameConstants::maxPlayers];
137 
138 		int8 thisFactionIndex;
139 		int8 factionCount;
140 		int8 teams[GameConstants::maxPlayers];
141 		int8 startLocationIndex[GameConstants::maxPlayers];
142 		int8 defaultResources;
143         int8 defaultUnits;
144         int8 defaultVictoryConditions;
145 	};
146 
147 private:
148 	Data data;
149 
150 public:
151 	NetworkMessageLaunch();
152 	NetworkMessageLaunch(const GameSettings *gameSettings);
153 
154 	void buildGameSettings(GameSettings *gameSettings) const;
155 
156 	virtual bool receive(Socket* socket);
157 	virtual void send(Socket* socket) const;
158 };
159 
160 // =====================================================
161 //	class CommandList
162 //
163 //	Message to order a commands to several units
164 // =====================================================
165 
166 class NetworkMessageCommandList: public NetworkMessage{
167 private:
168 	static const int maxCommandCount= 16*4;
169 
170 private:
171 	struct Data{
172 		int8 messageType;
173 		int8 commandCount;
174 		int32 frameCount;
175 		NetworkCommand commands[maxCommandCount];
176 	};
177 
178 private:
179 	Data data;
180 
181 public:
182 	NetworkMessageCommandList(int32 frameCount= -1);
183 
184 	bool addCommand(const NetworkCommand* networkCommand);
185 
clear()186 	void clear()									{data.commandCount= 0;}
getCommandCount()187 	int getCommandCount() const						{return data.commandCount;}
getFrameCount()188 	int getFrameCount() const						{return data.frameCount;}
getCommand(int i)189 	const NetworkCommand* getCommand(int i) const	{return &data.commands[i];}
190 
191 	virtual bool receive(Socket* socket);
192 	virtual void send(Socket* socket) const;
193 };
194 
195 // =====================================================
196 //	class NetworkMessageText
197 //
198 //	Chat text message
199 // =====================================================
200 
201 class NetworkMessageText: public NetworkMessage{
202 private:
203 	static const int maxStringSize= 64;
204 
205 private:
206 	struct Data{
207 		int8 messageType;
208 		NetworkString<maxStringSize> text;
209 		NetworkString<maxStringSize> sender;
210 		int8 teamIndex;
211 	};
212 
213 private:
214 	Data data;
215 
216 public:
NetworkMessageText()217 	NetworkMessageText(){}
218 	NetworkMessageText(const string &text, const string &sender, int teamIndex);
219 
getText()220 	string getText() const		{return data.text.getString();}
getSender()221 	string getSender() const	{return data.sender.getString();}
getTeamIndex()222 	int getTeamIndex() const	{return data.teamIndex;}
223 
224 	virtual bool receive(Socket* socket);
225 	virtual void send(Socket* socket) const;
226 };
227 
228 // =====================================================
229 //	class NetworkMessageQuit
230 //
231 //	Message sent at the beggining of the game
232 // =====================================================
233 
234 class NetworkMessageQuit: public NetworkMessage{
235 private:
236 	struct Data{
237 		int8 messageType;
238 	};
239 
240 private:
241 	Data data;
242 
243 public:
244 	NetworkMessageQuit();
245 
246 	virtual bool receive(Socket* socket);
247 	virtual void send(Socket* socket) const;
248 };
249 
250 }}//end namespace
251 
252 #endif
253