Line data Source code
1 : /* Copyright (C) 2017 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"
24 : #include "simulation2/Simulation2.h"
25 :
26 : #undef ALLNETMSGS_DONT_CREATE_NMTS
27 : #define ALLNETMSGS_IMPLEMENT
28 : #include "NetMessages.h"
29 :
30 1 : CNetMessage::CNetMessage()
31 : {
32 1 : m_Type = NMT_INVALID;
33 1 : }
34 :
35 2 : CNetMessage::CNetMessage(NetMessageType type)
36 : {
37 2 : m_Type = type;
38 2 : }
39 :
40 3 : CNetMessage::~CNetMessage()
41 : {
42 3 : }
43 :
44 1 : u8* CNetMessage::Serialize(u8* pBuffer) const
45 : {
46 1 : size_t size = GetSerializedLength();
47 1 : Serialize_int_1(pBuffer, m_Type);
48 1 : Serialize_int_2(pBuffer, size);
49 :
50 1 : return pBuffer;
51 : }
52 :
53 2 : const u8* CNetMessage::Deserialize(const u8* pStart, const u8* pEnd)
54 : {
55 2 : if (pStart + 3 > pEnd)
56 : {
57 0 : LOGERROR("CNetMessage: Corrupt packet (smaller than header)");
58 0 : return NULL;
59 : }
60 :
61 2 : const u8* pBuffer = pStart;
62 :
63 : int type;
64 : size_t size;
65 2 : Deserialize_int_1(pBuffer, type);
66 2 : Deserialize_int_2(pBuffer, size);
67 2 : m_Type = (NetMessageType)type;
68 :
69 2 : if (pStart + size != pEnd)
70 : {
71 0 : LOGERROR("CNetMessage: Corrupt packet (incorrect size)");
72 0 : return NULL;
73 : }
74 :
75 2 : return pBuffer;
76 : }
77 :
78 2 : size_t CNetMessage::GetSerializedLength() const
79 : {
80 : // By default, return header size
81 2 : return 3;
82 : }
83 :
84 0 : CStr CNetMessage::ToString() const
85 : {
86 : // This is called only when the subclass doesn't override it
87 :
88 0 : if (GetType() == NMT_INVALID)
89 0 : return "MESSAGE_TYPE_NONE { Undefined Message }";
90 : else
91 0 : return "Unknown Message " + CStr::FromInt(GetType());
92 : }
93 :
94 1 : CNetMessage* CNetMessageFactory::CreateMessage(const void* pData,
95 : size_t dataSize,
96 : const ScriptInterface& scriptInterface)
97 : {
98 1 : CNetMessage* pNewMessage = NULL;
99 2 : CNetMessage header;
100 :
101 : // Figure out message type
102 1 : header.Deserialize((const u8*)pData, (const u8*)pData + dataSize);
103 :
104 1 : switch (header.GetType())
105 : {
106 0 : case NMT_GAME_SETUP:
107 0 : pNewMessage = new CGameSetupMessage(scriptInterface);
108 0 : break;
109 :
110 0 : case NMT_PLAYER_ASSIGNMENT:
111 0 : pNewMessage = new CPlayerAssignmentMessage;
112 0 : break;
113 :
114 0 : case NMT_FILE_TRANSFER_REQUEST:
115 0 : pNewMessage = new CFileTransferRequestMessage;
116 0 : break;
117 :
118 0 : case NMT_FILE_TRANSFER_RESPONSE:
119 0 : pNewMessage = new CFileTransferResponseMessage;
120 0 : break;
121 :
122 0 : case NMT_FILE_TRANSFER_DATA:
123 0 : pNewMessage = new CFileTransferDataMessage;
124 0 : break;
125 :
126 0 : case NMT_FILE_TRANSFER_ACK:
127 0 : pNewMessage = new CFileTransferAckMessage;
128 0 : break;
129 :
130 0 : case NMT_JOIN_SYNC_START:
131 0 : pNewMessage = new CJoinSyncStartMessage;
132 0 : break;
133 :
134 0 : case NMT_REJOINED:
135 0 : pNewMessage = new CRejoinedMessage;
136 0 : break;
137 :
138 0 : case NMT_KICKED:
139 0 : pNewMessage = new CKickedMessage;
140 0 : break;
141 :
142 0 : case NMT_CLIENT_TIMEOUT:
143 0 : pNewMessage = new CClientTimeoutMessage;
144 0 : break;
145 :
146 0 : case NMT_CLIENT_PERFORMANCE:
147 0 : pNewMessage = new CClientPerformanceMessage;
148 0 : break;
149 :
150 0 : case NMT_CLIENTS_LOADING:
151 0 : pNewMessage = new CClientsLoadingMessage;
152 0 : break;
153 :
154 0 : case NMT_CLIENT_PAUSED:
155 0 : pNewMessage = new CClientPausedMessage;
156 0 : break;
157 :
158 0 : case NMT_LOADED_GAME:
159 0 : pNewMessage = new CLoadedGameMessage;
160 0 : break;
161 :
162 0 : case NMT_SERVER_HANDSHAKE:
163 0 : pNewMessage = new CSrvHandshakeMessage;
164 0 : break;
165 :
166 0 : case NMT_SERVER_HANDSHAKE_RESPONSE:
167 0 : pNewMessage = new CSrvHandshakeResponseMessage;
168 0 : break;
169 :
170 0 : case NMT_CLIENT_HANDSHAKE:
171 0 : pNewMessage = new CCliHandshakeMessage;
172 0 : break;
173 :
174 0 : case NMT_AUTHENTICATE:
175 0 : pNewMessage = new CAuthenticateMessage;
176 0 : break;
177 :
178 0 : case NMT_AUTHENTICATE_RESULT:
179 0 : pNewMessage = new CAuthenticateResultMessage;
180 0 : break;
181 :
182 0 : case NMT_GAME_START:
183 0 : pNewMessage = new CGameStartMessage;
184 0 : break;
185 :
186 0 : case NMT_END_COMMAND_BATCH:
187 0 : pNewMessage = new CEndCommandBatchMessage;
188 0 : break;
189 :
190 0 : case NMT_SYNC_CHECK:
191 0 : pNewMessage = new CSyncCheckMessage;
192 0 : break;
193 :
194 0 : case NMT_SYNC_ERROR:
195 0 : pNewMessage = new CSyncErrorMessage;
196 0 : break;
197 :
198 0 : case NMT_CHAT:
199 0 : pNewMessage = new CChatMessage;
200 0 : break;
201 :
202 0 : case NMT_READY:
203 0 : pNewMessage = new CReadyMessage;
204 0 : break;
205 :
206 1 : case NMT_SIMULATION_COMMAND:
207 1 : pNewMessage = new CSimulationMessage(scriptInterface);
208 1 : break;
209 :
210 0 : case NMT_CLEAR_ALL_READY:
211 0 : pNewMessage = new CClearAllReadyMessage;
212 0 : break;
213 :
214 0 : case NMT_ASSIGN_PLAYER:
215 0 : pNewMessage = new CAssignPlayerMessage;
216 0 : break;
217 :
218 0 : default:
219 0 : LOGERROR("CNetMessageFactory::CreateMessage(): Unknown message type '%d' received", header.GetType());
220 0 : break;
221 : }
222 :
223 1 : if (pNewMessage)
224 1 : pNewMessage->Deserialize((const u8*)pData, (const u8*)pData + dataSize);
225 :
226 2 : return pNewMessage;
227 : }
|