Changeset View
Changeset View
Standalone View
Standalone View
source/network/NetMessage.cpp
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2018 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 40 Lines | u8* CNetMessage::Serialize(u8* pBuffer) const | ||||
return pBuffer; | return pBuffer; | ||||
} | } | ||||
const u8* CNetMessage::Deserialize(const u8* pStart, const u8* pEnd) | const u8* CNetMessage::Deserialize(const u8* pStart, const u8* pEnd) | ||||
{ | { | ||||
if (pStart + 3 > pEnd) | if (pStart + 3 > pEnd) | ||||
{ | { | ||||
LOGERROR("CNetMessage: Corrupt packet (smaller than header)"); | LOGERROR("CNetMessage: Corrupt packet (smaller than header)"); | ||||
return NULL; | return nullptr; | ||||
} | } | ||||
const u8* pBuffer = pStart; | const u8* pBuffer = pStart; | ||||
int type; | int type; | ||||
size_t size; | size_t size; | ||||
Deserialize_int_1(pBuffer, type); | Deserialize_int_1(pBuffer, type); | ||||
Deserialize_int_2(pBuffer, size); | Deserialize_int_2(pBuffer, size); | ||||
m_Type = (NetMessageType)type; | m_Type = (NetMessageType)type; | ||||
if (pStart + size != pEnd) | if (pStart + size != pEnd) | ||||
{ | { | ||||
LOGERROR("CNetMessage: Corrupt packet (incorrect size)"); | LOGERROR("CNetMessage: Corrupt packet (incorrect size)"); | ||||
return NULL; | return nullptr; | ||||
} | } | ||||
return pBuffer; | return pBuffer; | ||||
} | } | ||||
size_t CNetMessage::GetSerializedLength() const | size_t CNetMessage::GetSerializedLength() const | ||||
{ | { | ||||
// By default, return header size | // By default, return header size | ||||
Show All 9 Lines | CStr CNetMessage::ToString() const | ||||
else | else | ||||
return "Unknown Message " + CStr::FromInt(GetType()); | return "Unknown Message " + CStr::FromInt(GetType()); | ||||
} | } | ||||
CNetMessage* CNetMessageFactory::CreateMessage(const void* pData, | CNetMessage* CNetMessageFactory::CreateMessage(const void* pData, | ||||
size_t dataSize, | size_t dataSize, | ||||
const ScriptInterface& scriptInterface) | const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
CNetMessage* pNewMessage = NULL; | CNetMessage* pNewMessage = nullptr; | ||||
CNetMessage header; | CNetMessage header; | ||||
// Figure out message type | // Figure out message type | ||||
header.Deserialize((const u8*)pData, (const u8*)pData + dataSize); | header.Deserialize((const u8*)pData, (const u8*)pData + dataSize); | ||||
switch (header.GetType()) | switch (header.GetType()) | ||||
{ | { | ||||
case NMT_GAME_SETUP: | case NMT_GAME_SETUP: | ||||
▲ Show 20 Lines • Show All 121 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator