Changeset View
Changeset View
Standalone View
Standalone View
source/network/NetMessageBuffer.cpp
- This file was added.
/* Copyright (C) 2017 Wildfire Games. | |||||
* This file is part of 0 A.D. | |||||
* | |||||
* 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 | |||||
* the Free Software Foundation, either version 2 of the License, or | |||||
* (at your option) any later version. | |||||
* | |||||
* 0 A.D. is distributed in the hope that it will be useful, | |||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
* GNU General Public License for more details. | |||||
* | |||||
* You should have received a copy of the GNU General Public License | |||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | |||||
*/ | |||||
#include "precompiled.h" | |||||
#include "NetMessageBuffer.h" | |||||
#include "ps/CStr.h" | |||||
#include <simulation2/serialization/BinarySerializer.h> | |||||
leper: `""` not `<>` this isn't a system include. | |||||
#include <simulation2/serialization/StdDeserializer.h> | |||||
CNetMessageBuffer::CNetMessageBuffer(const std::vector<u8>& buffer) : | |||||
m_Buffer(buffer), | |||||
m_Position(0), | |||||
m_Type((NetMessageType)Read()) | |||||
{ | |||||
} | |||||
CNetMessageBuffer::CNetMessageBuffer(u8* buffer, u32 size) : | |||||
m_Buffer(), | |||||
m_Position(0), | |||||
m_Type() | |||||
{ | |||||
m_Buffer.resize(size); | |||||
std::memcpy(m_Buffer.data(), buffer, size); | |||||
m_Type = (NetMessageType)Read(); | |||||
} | |||||
NetMessageType CNetMessageBuffer::GetType() const | |||||
{ | |||||
return m_Type; | |||||
} | |||||
std::vector<u8> CNetMessageBuffer::GetBuffer() const | |||||
{ | |||||
return m_Buffer; | |||||
} | |||||
void CNetMessageBuffer::ToPosition(u32 pos) | |||||
{ | |||||
if (pos >= m_Buffer.size()) | |||||
m_Buffer.resize(pos + 1); | |||||
m_Position = pos; | |||||
} | |||||
void CNetMessageBuffer::ToPosition(u32 pos) const | |||||
{ | |||||
ENSURE(pos < m_Buffer.size()); | |||||
Done Inline ActionsThese three could just be in the header. leper: These three could just be in the header. | |||||
m_Position = pos; | |||||
} | |||||
void CNetMessageBuffer::Write(u8 byte) | |||||
{ | |||||
ENSURE(m_Position < std::numeric_limits<u32>::max()); | |||||
if (m_Position >= m_Buffer.size()) | |||||
m_Buffer.resize(m_Position + 1); | |||||
m_Buffer[m_Position++] = byte; | |||||
} | |||||
u8 CNetMessageBuffer::Read() const | |||||
{ | |||||
ENSURE(m_Position < m_Buffer.size()); | |||||
return m_Buffer[m_Position++]; | |||||
} | |||||
class CBufferBinarySerializerImpl | |||||
{ | |||||
public: | |||||
CBufferBinarySerializerImpl() : | |||||
m_Buffer() | |||||
{ | |||||
} | |||||
void Put(const char*, const u8* data, size_t len) | |||||
Not Done Inline ActionsWhat if this overflows? Yes, this is unlikely to occur, but this is in network code. leper: What if this overflows?
Yes, this is unlikely to occur, but this is in network code. | |||||
{ | |||||
size_t pos = m_Buffer.size(); | |||||
m_Buffer.resize(m_Buffer.size() + len); | |||||
std::memcpy(m_Buffer.data() + pos, data, len); | |||||
} | |||||
std::vector<u8> m_Buffer; | |||||
}; | |||||
class CBufferBinarySerializer : public CBinarySerializer<CBufferBinarySerializerImpl> | |||||
{ | |||||
public: | |||||
Not Done Inline ActionsWhy the first param? leper: Why the first param? | |||||
Not Done Inline ActionsIt is required for the CBinarySerializer, but I don't need it here. Guess I could use UNUSED() here. echotangoecho: It is required for the CBinarySerializer, but I don't need it here. Guess I could use UNUSED()… | |||||
Done Inline ActionsUNUSED would make make this obvious. leper: `UNUSED` would make make this obvious. | |||||
CBufferBinarySerializer(ScriptInterface& scriptInterface) : | |||||
CBinarySerializer<CBufferBinarySerializerImpl>(scriptInterface) | |||||
{ | |||||
Done Inline Actionsconst? leper: const? | |||||
Done Inline ActionsCould this be const std::vector<u8>&? (Would also make the few users of this use the same type, avoiding the need to copy.) leper: Could this be `const std::vector<u8>&`? (Would also make the few users of this use the same… | |||||
} | |||||
std::vector<u8> GetBuffer() | |||||
{ | |||||
return m_Impl.m_Buffer; | |||||
} | |||||
}; | |||||
template<> | |||||
void CNetMessageBuffer::Serialize(const CStr& object) | |||||
{ | |||||
SerializePrimitive((u32)object.size()); | |||||
for (u32 i = 0; i < object.size(); ++i) | |||||
{ | |||||
SerializePrimitive(object[i]); | |||||
} | |||||
} | |||||
template<> | |||||
void CNetMessageBuffer::Serialize(const CStrW& object) | |||||
{ | |||||
SerializePrimitive((u32)object.size()); | |||||
for (u32 i = 0; i < object.size(); ++i) | |||||
{ | |||||
SerializePrimitive((u16)object[i]); | |||||
} | |||||
} | |||||
template<> | |||||
void CNetMessageBuffer::Serialize(const SerializableJSValue& object) | |||||
{ | |||||
Not Done Inline ActionsSee the comment marked as done about also having users of GetBuffer use const std::vector<u8>&. leper: See the comment marked as done about also having users of `GetBuffer` use `const std… | |||||
CBufferBinarySerializer serializer(object.m_ScriptInterface); | |||||
serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&object.m_Data)); | |||||
std::vector<u8> buffer = serializer.GetBuffer(); | |||||
SerializePrimitive(buffer); | |||||
} | |||||
template<> | |||||
void CNetMessageBuffer::Deserialize(CStr& object) const | |||||
{ | |||||
u32 size; | |||||
DeserializePrimitive(size); | |||||
object.resize(size); | |||||
for (u32 i = 0; i < size; ++i) | |||||
{ | |||||
DeserializePrimitive(object[i]); | |||||
} | |||||
} | |||||
template<> | |||||
void CNetMessageBuffer::Deserialize(CStrW& object) const | |||||
{ | |||||
u32 size; | |||||
DeserializePrimitive(size); | |||||
object.resize(size); | |||||
for (u32 i = 0; i < size; ++i) | |||||
{ | |||||
u16 tmp; | |||||
DeserializePrimitive(tmp); | |||||
object[i] = tmp; | |||||
} | |||||
} | |||||
template<> | |||||
void CNetMessageBuffer::Deserialize(SerializableJSValue& object) const | |||||
{ | |||||
std::vector<u8> buffer; | |||||
DeserializePrimitive(buffer); | |||||
std::istringstream stream(std::string(buffer.begin(), buffer.end())); | |||||
CStdDeserializer deserializer(object.m_ScriptInterface, stream); | |||||
deserializer.ScriptVal("command", &object.m_Data); | |||||
} |
Wildfire Games · Phabricator
"" not <> this isn't a system include.