1 /* Copyright (C) 2017 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
19 #include "NetMessage.h"
21 #include "ps/CLogger.h"
24 #include "simulation2/Simulation2.h"
26 #undef ALLNETMSGS_DONT_CREATE_NMTS
27 #define ALLNETMSGS_IMPLEMENT
28 #include "NetMessages.h"
30 CNetMessage::CNetMessage()
35 CNetMessage::CNetMessage(NetMessageType type
)
40 CNetMessage::~CNetMessage()
44 u8
* CNetMessage::Serialize(u8
* pBuffer
) const
46 size_t size
= GetSerializedLength();
47 Serialize_int_1(pBuffer
, m_Type
);
48 Serialize_int_2(pBuffer
, size
);
53 const u8
* CNetMessage::Deserialize(const u8
* pStart
, const u8
* pEnd
)
55 if (pStart
+ 3 > pEnd
)
57 LOGERROR("CNetMessage: Corrupt packet (smaller than header)");
61 const u8
* pBuffer
= pStart
;
65 Deserialize_int_1(pBuffer
, type
);
66 Deserialize_int_2(pBuffer
, size
);
67 m_Type
= (NetMessageType
)type
;
69 if (pStart
+ size
!= pEnd
)
71 LOGERROR("CNetMessage: Corrupt packet (incorrect size)");
78 size_t CNetMessage::GetSerializedLength() const
80 // By default, return header size
84 CStr
CNetMessage::ToString() const
86 // This is called only when the subclass doesn't override it
88 if (GetType() == NMT_INVALID
)
89 return "MESSAGE_TYPE_NONE { Undefined Message }";
91 return "Unknown Message " + CStr::FromInt(GetType());
94 CNetMessage
* CNetMessageFactory::CreateMessage(const void* pData
,
96 const ScriptInterface
& scriptInterface
)
98 CNetMessage
* pNewMessage
= NULL
;
101 // Figure out message type
102 header
.Deserialize((const u8
*)pData
, (const u8
*)pData
+ dataSize
);
104 switch (header
.GetType())
107 pNewMessage
= new CGameSetupMessage(scriptInterface
);
110 case NMT_PLAYER_ASSIGNMENT
:
111 pNewMessage
= new CPlayerAssignmentMessage
;
114 case NMT_FILE_TRANSFER_REQUEST
:
115 pNewMessage
= new CFileTransferRequestMessage
;
118 case NMT_FILE_TRANSFER_RESPONSE
:
119 pNewMessage
= new CFileTransferResponseMessage
;
122 case NMT_FILE_TRANSFER_DATA
:
123 pNewMessage
= new CFileTransferDataMessage
;
126 case NMT_FILE_TRANSFER_ACK
:
127 pNewMessage
= new CFileTransferAckMessage
;
130 case NMT_JOIN_SYNC_START
:
131 pNewMessage
= new CJoinSyncStartMessage
;
135 pNewMessage
= new CRejoinedMessage
;
139 pNewMessage
= new CKickedMessage
;
142 case NMT_CLIENT_TIMEOUT
:
143 pNewMessage
= new CClientTimeoutMessage
;
146 case NMT_CLIENT_PERFORMANCE
:
147 pNewMessage
= new CClientPerformanceMessage
;
150 case NMT_CLIENTS_LOADING
:
151 pNewMessage
= new CClientsLoadingMessage
;
154 case NMT_CLIENT_PAUSED
:
155 pNewMessage
= new CClientPausedMessage
;
158 case NMT_LOADED_GAME
:
159 pNewMessage
= new CLoadedGameMessage
;
162 case NMT_SERVER_HANDSHAKE
:
163 pNewMessage
= new CSrvHandshakeMessage
;
166 case NMT_SERVER_HANDSHAKE_RESPONSE
:
167 pNewMessage
= new CSrvHandshakeResponseMessage
;
170 case NMT_CLIENT_HANDSHAKE
:
171 pNewMessage
= new CCliHandshakeMessage
;
174 case NMT_AUTHENTICATE
:
175 pNewMessage
= new CAuthenticateMessage
;
178 case NMT_AUTHENTICATE_RESULT
:
179 pNewMessage
= new CAuthenticateResultMessage
;
183 pNewMessage
= new CGameStartMessage
;
186 case NMT_END_COMMAND_BATCH
:
187 pNewMessage
= new CEndCommandBatchMessage
;
191 pNewMessage
= new CSyncCheckMessage
;
195 pNewMessage
= new CSyncErrorMessage
;
199 pNewMessage
= new CChatMessage
;
203 pNewMessage
= new CReadyMessage
;
206 case NMT_SIMULATION_COMMAND
:
207 pNewMessage
= new CSimulationMessage(scriptInterface
);
210 case NMT_CLEAR_ALL_READY
:
211 pNewMessage
= new CClearAllReadyMessage
;
214 case NMT_ASSIGN_PLAYER
:
215 pNewMessage
= new CAssignPlayerMessage
;
219 LOGERROR("CNetMessageFactory::CreateMessage(): Unknown message type '%d' received", header
.GetType());
224 pNewMessage
->Deserialize((const u8
*)pData
, (const u8
*)pData
+ dataSize
);