Add NetworkMessage read/write functions (WIP)

This commit is contained in:
Damian Korczowski
2021-03-12 20:01:03 +01:00
parent 082d56f89a
commit 5afe51d1b4
3 changed files with 118 additions and 17 deletions

View File

@@ -15,15 +15,3 @@ void NetworkMessage::ReadBytes(uint8* bytes, const int numBytes)
Platform::MemoryCopy(bytes, Buffer + Position, numBytes);
Position += numBytes;
}
void NetworkMessage::WriteUInt32(uint32 value)
{
WriteBytes(reinterpret_cast<uint8*>(&value), sizeof(uint32));
}
uint32 NetworkMessage::ReadUInt32()
{
uint32 value = 0;
ReadBytes(reinterpret_cast<uint8*>(&value), sizeof(uint32));
return value;
}

View File

@@ -0,0 +1,29 @@
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
namespace FlaxEngine.Networking
{
public unsafe partial struct NetworkMessage
{
public void WriteBytes(byte* bytes, int length)
{
// TODO
}
public void ReadBytes(byte* buffer, int length)
{
// TODO
}
public void WriteUInt32(uint value)
{
WriteBytes((byte*)&value, sizeof(uint));
}
public uint ReadUInt32()
{
uint value = 0;
ReadBytes((byte*)&value, sizeof(uint));
return value;
}
}
}

View File

@@ -2,6 +2,11 @@
#pragma once
#include "Engine/Core/Math/Vector2.h"
#include "Engine/Core/Math/Vector3.h"
#include "Engine/Core/Math/Vector4.h"
#include "Engine/Core/Math/Quaternion.h"
#include "Engine/Core/Types/String.h"
#include "Engine/Scripting/ScriptingType.h"
API_STRUCT(Namespace="FlaxEngine.Networking") struct FLAXENGINE_API NetworkMessage
@@ -12,7 +17,7 @@ public:
uint8* Buffer = nullptr;
API_FIELD()
uint32 MessageId = 0; // TODO: Make it read-only
uint32 MessageId = 0;
API_FIELD()
uint32 BufferSize = 0;
@@ -33,12 +38,91 @@ public:
~NetworkMessage() = default;
public:
void WriteBytes(uint8* bytes, int numBytes);
void ReadBytes(uint8* bytes, int numBytes);
FORCE_INLINE void WriteBytes(uint8* bytes, int numBytes);
FORCE_INLINE void ReadBytes(uint8* bytes, int numBytes);
#define DECL_READWRITE(type, name) \
FORCE_INLINE void Write##name(type value) { WriteBytes(reinterpret_cast<uint8*>(&value), sizeof(type)); } \
FORCE_INLINE type Read##name() { type value = 0; ReadBytes(reinterpret_cast<uint8*>(&value), sizeof(type)); return value; }
public:
void WriteUInt32(uint32 value); // TODO: Macro the shit out of this
uint32 ReadUInt32();
DECL_READWRITE(int8, Int8)
DECL_READWRITE(uint8, UInt8)
DECL_READWRITE(int16, Int16)
DECL_READWRITE(uint16, UInt16)
DECL_READWRITE(int32, Int32)
DECL_READWRITE(uint32, UInt32)
DECL_READWRITE(int64, Int64)
DECL_READWRITE(uint64, UInt64)
DECL_READWRITE(float, Single)
DECL_READWRITE(double, Double)
DECL_READWRITE(bool, Boolean)
public:
FORCE_INLINE void WriteVector2(const Vector2& value)
{
WriteSingle(value.X);
WriteSingle(value.Y);
}
FORCE_INLINE Vector2 ReadVector2()
{
return Vector2(ReadSingle(), ReadSingle());
}
FORCE_INLINE void WriteVector3(const Vector3& value)
{
WriteSingle(value.X);
WriteSingle(value.Y);
WriteSingle(value.Z);
}
FORCE_INLINE Vector3 ReadVector3()
{
return Vector3(ReadSingle(), ReadSingle(), ReadSingle());
}
FORCE_INLINE void WriteVector4(const Vector4& value)
{
WriteSingle(value.X);
WriteSingle(value.Y);
WriteSingle(value.Z);
WriteSingle(value.W);
}
FORCE_INLINE Vector4 ReadVector4()
{
return Vector4(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
}
FORCE_INLINE void WriteQuaternion(const Quaternion& value)
{
WriteSingle(value.X);
WriteSingle(value.Y);
WriteSingle(value.Z);
WriteSingle(value.W);
}
FORCE_INLINE Quaternion ReadQuaternion()
{
return Quaternion(ReadSingle(), ReadSingle(), ReadSingle(), ReadSingle());
}
public:
FORCE_INLINE void WriteQuaternion(const String& value)
{
WriteUInt16(value.Length()); // TODO: Use 1-byte length when possible
WriteBytes((uint8*)value.Get(), value.Length() * 2);
}
FORCE_INLINE String ReadString()
{
uint16 length = ReadUInt16();
String value;
value.Resize(length);
ReadBytes((uint8*)value.Get(), value.Length() * 2);
return value;
}
public:
bool IsValid() const