Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NetMessage.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 #include "NetMessage.h"
20 
21 #include "ps/CLogger.h"
22 
23 #include "ps/Game.h"
25 
26 #undef ALLNETMSGS_DONT_CREATE_NMTS
27 #define ALLNETMSGS_IMPLEMENT
28 #include "NetMessages.h"
29 
31 {
33 }
34 
36 {
37  m_Type = type;
38 }
39 
41 {
42 }
43 
44 u8* CNetMessage::Serialize(u8* pBuffer) const
45 {
46  size_t size = GetSerializedLength();
47  Serialize_int_1(pBuffer, m_Type);
48  Serialize_int_2(pBuffer, size);
49 
50  return pBuffer;
51 }
52 
53 const u8* CNetMessage::Deserialize(const u8* pStart, const u8* pEnd)
54 {
55  if (pStart + 3 > pEnd)
56  {
57  LOGERROR(L"CNetMessage: Corrupt packet (smaller than header)");
58  return NULL;
59  }
60 
61  const u8* pBuffer = pStart;
62 
63  int type;
64  size_t size;
65  Deserialize_int_1(pBuffer, type);
66  Deserialize_int_2(pBuffer, size);
67  m_Type = (NetMessageType)type;
68 
69  if (pStart + size != pEnd)
70  {
71  LOGERROR(L"CNetMessage: Corrupt packet (incorrect size)");
72  return NULL;
73  }
74 
75  return pBuffer;
76 }
77 
79 {
80  // By default, return header size
81  return 3;
82 }
83 
85 {
86  // This is called only when the subclass doesn't override it
87 
88  if (GetType() == NMT_INVALID)
89  return "MESSAGE_TYPE_NONE { Undefined Message }";
90  else
91  return "Unknown Message " + CStr::FromInt(GetType());
92 }
93 
95  size_t dataSize,
96  ScriptInterface& scriptInterface)
97 {
98  CNetMessage* pNewMessage = NULL;
99  CNetMessage header;
100 
101  // Figure out message type
102  header.Deserialize((const u8*)pData, (const u8*)pData + dataSize);
103 
104  switch (header.GetType())
105  {
106  case NMT_GAME_SETUP:
107  pNewMessage = new CGameSetupMessage(scriptInterface);
108  break;
109 
111  pNewMessage = new CPlayerAssignmentMessage;
112  break;
113 
115  pNewMessage = new CFileTransferRequestMessage;
116  break;
117 
119  pNewMessage = new CFileTransferResponseMessage;
120  break;
121 
123  pNewMessage = new CFileTransferDataMessage;
124  break;
125 
127  pNewMessage = new CFileTransferAckMessage;
128  break;
129 
130  case NMT_JOIN_SYNC_START:
131  pNewMessage = new CJoinSyncStartMessage;
132  break;
133 
134  case NMT_LOADED_GAME:
135  pNewMessage = new CLoadedGameMessage;
136  break;
137 
139  pNewMessage = new CSrvHandshakeMessage;
140  break;
141 
143  pNewMessage = new CSrvHandshakeResponseMessage;
144  break;
145 
147  pNewMessage = new CCliHandshakeMessage;
148  break;
149 
150  case NMT_AUTHENTICATE:
151  pNewMessage = new CAuthenticateMessage;
152  break;
153 
155  pNewMessage = new CAuthenticateResultMessage;
156  break;
157 
158  case NMT_GAME_START:
159  pNewMessage = new CGameStartMessage;
160  break;
161 
163  pNewMessage = new CEndCommandBatchMessage;
164  break;
165 
166  case NMT_SYNC_CHECK:
167  pNewMessage = new CSyncCheckMessage;
168  break;
169 
170  case NMT_SYNC_ERROR:
171  pNewMessage = new CSyncErrorMessage;
172  break;
173 
174  case NMT_CHAT:
175  pNewMessage = new CChatMessage;
176  break;
177 
179  pNewMessage = new CSimulationMessage(scriptInterface);
180  break;
181 
182  default:
183  LOGERROR(L"CNetMessageFactory::CreateMessage(): Unknown message type '%d' received", header.GetType());
184  break;
185  }
186 
187  if (pNewMessage)
188  pNewMessage->Deserialize((const u8*)pData, (const u8*)pData + dataSize);
189 
190  return pNewMessage;
191 }
#define u8
Definition: types.h:39
NetMessageType
Definition: NetMessages.h:38
#define Deserialize_int_1(_pos, _val)
Definition: Serialization.h:48
#define LOGERROR
Definition: CLogger.h:35
The list of messages used by the network subsystem.
virtual u8 * Serialize(u8 *pBuffer) const
Serialize the message into the specified buffer parameter.
Definition: NetMessage.cpp:44
#define Serialize_int_1(_pos, _val)
Definition: Serialization.h:21
Special message type for updated to game startup settings.
Definition: NetMessage.h:134
virtual ~CNetMessage()
Definition: NetMessage.cpp:40
NetMessageType GetType() const
Retrieves the message type.
Definition: NetMessage.h:46
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:32
#define Deserialize_int_2(_pos, _val)
Definition: Serialization.h:51
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessage.cpp:78
#define Serialize_int_2(_pos, _val)
Definition: Serialization.h:24
Abstraction around a SpiderMonkey JSContext.
virtual const u8 * Deserialize(const u8 *pStart, const u8 *pEnd)
Deserializes the message from the specified buffer.
Definition: NetMessage.cpp:53
NetMessageType m_Type
Definition: NetMessage.h:88
static CNetMessage * CreateMessage(const void *pData, size_t dataSize, ScriptInterface &scriptInterface)
Factory method which creates a message object based on the given data.
Definition: NetMessage.cpp:94
virtual CStr ToString() const
Returns a string representation for the message.
Definition: NetMessage.cpp:84
Special message type for simulation commands.
Definition: NetMessage.h:113