Fixes calls to SetVertexAttributeFormat with zero stride.
[0ad.git] / source / network / NetMessage.cpp
blob61126d8e1b077a381fe0127985e8a98832b6eba5
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"
23 #include "ps/Game.h"
24 #include "simulation2/Simulation2.h"
26 #undef ALLNETMSGS_DONT_CREATE_NMTS
27 #define ALLNETMSGS_IMPLEMENT
28 #include "NetMessages.h"
30 CNetMessage::CNetMessage()
32 m_Type = NMT_INVALID;
35 CNetMessage::CNetMessage(NetMessageType type)
37 m_Type = 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);
50 return pBuffer;
53 const u8* CNetMessage::Deserialize(const u8* pStart, const u8* pEnd)
55 if (pStart + 3 > pEnd)
57 LOGERROR("CNetMessage: Corrupt packet (smaller than header)");
58 return NULL;
61 const u8* pBuffer = pStart;
63 int type;
64 size_t size;
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)");
72 return NULL;
75 return pBuffer;
78 size_t CNetMessage::GetSerializedLength() const
80 // By default, return header size
81 return 3;
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 }";
90 else
91 return "Unknown Message " + CStr::FromInt(GetType());
94 CNetMessage* CNetMessageFactory::CreateMessage(const void* pData,
95 size_t dataSize,
96 const ScriptInterface& scriptInterface)
98 CNetMessage* pNewMessage = NULL;
99 CNetMessage header;
101 // Figure out message type
102 header.Deserialize((const u8*)pData, (const u8*)pData + dataSize);
104 switch (header.GetType())
106 case NMT_GAME_SETUP:
107 pNewMessage = new CGameSetupMessage(scriptInterface);
108 break;
110 case NMT_PLAYER_ASSIGNMENT:
111 pNewMessage = new CPlayerAssignmentMessage;
112 break;
114 case NMT_FILE_TRANSFER_REQUEST:
115 pNewMessage = new CFileTransferRequestMessage;
116 break;
118 case NMT_FILE_TRANSFER_RESPONSE:
119 pNewMessage = new CFileTransferResponseMessage;
120 break;
122 case NMT_FILE_TRANSFER_DATA:
123 pNewMessage = new CFileTransferDataMessage;
124 break;
126 case NMT_FILE_TRANSFER_ACK:
127 pNewMessage = new CFileTransferAckMessage;
128 break;
130 case NMT_JOIN_SYNC_START:
131 pNewMessage = new CJoinSyncStartMessage;
132 break;
134 case NMT_REJOINED:
135 pNewMessage = new CRejoinedMessage;
136 break;
138 case NMT_KICKED:
139 pNewMessage = new CKickedMessage;
140 break;
142 case NMT_CLIENT_TIMEOUT:
143 pNewMessage = new CClientTimeoutMessage;
144 break;
146 case NMT_CLIENT_PERFORMANCE:
147 pNewMessage = new CClientPerformanceMessage;
148 break;
150 case NMT_CLIENTS_LOADING:
151 pNewMessage = new CClientsLoadingMessage;
152 break;
154 case NMT_CLIENT_PAUSED:
155 pNewMessage = new CClientPausedMessage;
156 break;
158 case NMT_LOADED_GAME:
159 pNewMessage = new CLoadedGameMessage;
160 break;
162 case NMT_SERVER_HANDSHAKE:
163 pNewMessage = new CSrvHandshakeMessage;
164 break;
166 case NMT_SERVER_HANDSHAKE_RESPONSE:
167 pNewMessage = new CSrvHandshakeResponseMessage;
168 break;
170 case NMT_CLIENT_HANDSHAKE:
171 pNewMessage = new CCliHandshakeMessage;
172 break;
174 case NMT_AUTHENTICATE:
175 pNewMessage = new CAuthenticateMessage;
176 break;
178 case NMT_AUTHENTICATE_RESULT:
179 pNewMessage = new CAuthenticateResultMessage;
180 break;
182 case NMT_GAME_START:
183 pNewMessage = new CGameStartMessage;
184 break;
186 case NMT_END_COMMAND_BATCH:
187 pNewMessage = new CEndCommandBatchMessage;
188 break;
190 case NMT_SYNC_CHECK:
191 pNewMessage = new CSyncCheckMessage;
192 break;
194 case NMT_SYNC_ERROR:
195 pNewMessage = new CSyncErrorMessage;
196 break;
198 case NMT_CHAT:
199 pNewMessage = new CChatMessage;
200 break;
202 case NMT_READY:
203 pNewMessage = new CReadyMessage;
204 break;
206 case NMT_SIMULATION_COMMAND:
207 pNewMessage = new CSimulationMessage(scriptInterface);
208 break;
210 case NMT_CLEAR_ALL_READY:
211 pNewMessage = new CClearAllReadyMessage;
212 break;
214 case NMT_ASSIGN_PLAYER:
215 pNewMessage = new CAssignPlayerMessage;
216 break;
218 default:
219 LOGERROR("CNetMessageFactory::CreateMessage(): Unknown message type '%d' received", header.GetType());
220 break;
223 if (pNewMessage)
224 pNewMessage->Deserialize((const u8*)pData, (const u8*)pData + dataSize);
226 return pNewMessage;