generated from Persson-dev/Godot-Xmake
add blitz files
This commit is contained in:
49
src/blitz/protocol/ByteBuffer.cpp
Normal file
49
src/blitz/protocol/ByteBuffer.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <blitz/protocol/ByteBuffer.h>
|
||||
|
||||
#include <blitz/protocol/PacketData.h>
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
|
||||
ByteBuffer& ByteBuffer::operator>>(PlayerInfo& a_Data) {
|
||||
*this >> a_Data.m_PlayerId >> a_Data.m_PlayerName;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer& ByteBuffer::operator<<(const PlayerInfo& a_Data) {
|
||||
*this << a_Data.m_PlayerId << a_Data.m_PlayerName;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer& ByteBuffer::operator<<(const godot::Vector3& a_Data) {
|
||||
*this << a_Data.x << a_Data.y << a_Data.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer& ByteBuffer::operator>>(godot::Vector3& a_Data) {
|
||||
*this >> a_Data.x >> a_Data.y >> a_Data.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer& ByteBuffer::operator>>(godot::String& a_Data) {
|
||||
int nullPos = m_Buffer.find(0, m_ReadOffset);
|
||||
// TODO: error handling
|
||||
if (nullPos < 0)
|
||||
return *this;
|
||||
|
||||
godot::PackedByteArray stringBuffer = m_Buffer.slice(m_ReadOffset, nullPos);
|
||||
a_Data = stringBuffer.get_string_from_utf8();
|
||||
m_ReadOffset = nullPos + 1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ByteBuffer& ByteBuffer::operator<<(const godot::String& a_Data) {
|
||||
godot::PackedByteArray stringBuffer = a_Data.to_utf8_buffer();
|
||||
m_Buffer.append_array(stringBuffer);
|
||||
// ends the string
|
||||
*this << static_cast<std::uint8_t>(0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
37
src/blitz/protocol/PacketDispatcher.cpp
Normal file
37
src/blitz/protocol/PacketDispatcher.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <blitz/protocol/PacketDispatcher.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <blitz/protocol/PacketHandler.h>
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
|
||||
void PacketDispatcher::RegisterHandler(PacketType type, PacketHandler& handler) {
|
||||
auto found = std::find(m_Handlers[type].begin(), m_Handlers[type].end(), &handler);
|
||||
if (found == m_Handlers[type].end())
|
||||
m_Handlers[type].push_back(&handler);
|
||||
}
|
||||
|
||||
void PacketDispatcher::UnregisterHandler(PacketType type, PacketHandler& handler) {
|
||||
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
|
||||
}
|
||||
|
||||
void PacketDispatcher::UnregisterHandler(PacketHandler& handler) {
|
||||
for (auto& pair : m_Handlers) {
|
||||
if (pair.second.empty())
|
||||
continue;
|
||||
|
||||
PacketType type = pair.first;
|
||||
|
||||
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
|
||||
}
|
||||
}
|
||||
|
||||
void PacketDispatcher::Dispatch(const Packet& packet) {
|
||||
PacketType type = packet.GetType();
|
||||
for (PacketHandler* handler : m_Handlers[type])
|
||||
handler->Check(packet);
|
||||
}
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
32
src/blitz/protocol/PacketFactory.cpp
Normal file
32
src/blitz/protocol/PacketFactory.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <blitz/protocol/PacketFactory.h>
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
namespace PacketFactory {
|
||||
|
||||
using PacketCreator = std::function<std::unique_ptr<Packet>()>;
|
||||
|
||||
#define DeclarePacket(PacketName, ...) std::make_unique<packets::PacketName>(),
|
||||
|
||||
static std::array<std::unique_ptr<Packet>, static_cast<std::size_t>(PacketType::PACKET_COUNT)> Packets;
|
||||
|
||||
void Init() {
|
||||
Packets = {
|
||||
|
||||
DeclareAllPacket()
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
const std::unique_ptr<Packet>& CreateReadOnlyPacket(PacketType a_Type) {
|
||||
assert(a_Type < PacketType::PACKET_COUNT);
|
||||
return Packets[static_cast<std::size_t>(a_Type)];
|
||||
}
|
||||
|
||||
} // namespace PacketFactory
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
14
src/blitz/protocol/PacketHandler.cpp
Normal file
14
src/blitz/protocol/PacketHandler.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <blitz/protocol/PacketHandler.h>
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
|
||||
#define DeclarePacket(PacketName, ...) \
|
||||
void PacketHandler::Visit(const packets::PacketName& a_Packet) { \
|
||||
HandlePacket(a_Packet); \
|
||||
}
|
||||
|
||||
DeclareAllPacket()
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
294
src/blitz/protocol/PacketSerializer.cpp
Normal file
294
src/blitz/protocol/PacketSerializer.cpp
Normal file
@@ -0,0 +1,294 @@
|
||||
#include <blitz/protocol/PacketSerializer.h>
|
||||
|
||||
#include <blitz/protocol/ByteBuffer.h>
|
||||
#include <blitz/protocol/PacketFactory.h>
|
||||
#include <blitz/protocol/PacketVisitor.h>
|
||||
|
||||
#include <godot_cpp/variant/utility_functions.hpp>
|
||||
#include <godot_cpp/variant/variant.hpp>
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
namespace PacketSerializer {
|
||||
|
||||
#define DeclarePacket(PacketName, ...) \
|
||||
void Visit(const packets::PacketName& a_Packet) override { \
|
||||
const auto& packetData = a_Packet.m_Data; \
|
||||
SerializePacketData(packetData); \
|
||||
} \
|
||||
\
|
||||
void SerializePacketData(const packets::PacketName::PacketDataType& a_Packet);
|
||||
|
||||
|
||||
|
||||
|
||||
class Serializer : public PacketVisitor {
|
||||
private:
|
||||
ByteBuffer& m_Buffer;
|
||||
|
||||
public:
|
||||
Serializer(ByteBuffer& a_Buffer) : m_Buffer(a_Buffer) {}
|
||||
|
||||
void Serialize(const Packet& a_Packet) {
|
||||
m_Buffer << static_cast<PacketID>(a_Packet.GetType());
|
||||
Check(a_Packet);
|
||||
}
|
||||
|
||||
DeclareAllPacket()
|
||||
};
|
||||
|
||||
#undef DeclarePacket
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define DeclarePacket(PacketName, ...) \
|
||||
void Visit(const packets::PacketName& a_Packet) override { \
|
||||
auto packetPtr = PacketFactory::CreatePacket<packets::PacketName>(); \
|
||||
auto& packetData = packetPtr->m_Data; \
|
||||
\
|
||||
DeserializePacketData(packetData); \
|
||||
\
|
||||
m_Packet = std::move(packetPtr); \
|
||||
} \
|
||||
\
|
||||
void DeserializePacketData(packets::PacketName::PacketDataType& a_Packet);
|
||||
|
||||
|
||||
|
||||
class Deserializer : public PacketVisitor {
|
||||
private:
|
||||
ByteBuffer& m_Buffer;
|
||||
PacketPtr m_Packet;
|
||||
|
||||
public:
|
||||
Deserializer(ByteBuffer&& a_Buffer) : m_Buffer(a_Buffer) {}
|
||||
|
||||
bool Deserialize(const PacketPtr& a_Packet) {
|
||||
try {
|
||||
Check(*a_Packet.get());
|
||||
} catch (std::exception& e) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
PacketPtr& GetPacket() {
|
||||
return m_Packet;
|
||||
}
|
||||
|
||||
DeclareAllPacket()
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
godot::PackedByteArray Serialize(const Packet& a_Packet) {
|
||||
ByteBuffer stream;
|
||||
|
||||
Serializer serializer(stream);
|
||||
serializer.Serialize(a_Packet);
|
||||
|
||||
return stream.GetByteArray();
|
||||
}
|
||||
|
||||
std::unique_ptr<Packet> Deserialize(godot::PackedByteArray& a_Data) {
|
||||
ByteBuffer stream(std::move(a_Data));
|
||||
|
||||
PacketID packetId;
|
||||
stream >> packetId;
|
||||
|
||||
if (packetId >= static_cast<PacketID>(PacketType::PACKET_COUNT))
|
||||
return nullptr;
|
||||
|
||||
PacketType packetType = PacketType(packetId);
|
||||
|
||||
// for double-dispatch
|
||||
const PacketPtr& emptyPacket = PacketFactory::CreateReadOnlyPacket(packetType);
|
||||
|
||||
Deserializer deserializer(std::move(stream));
|
||||
if (deserializer.Deserialize(emptyPacket)) {
|
||||
PacketPtr packet = std::move(deserializer.GetPacket());
|
||||
return packet;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------
|
||||
// Packet serializer implementation
|
||||
//----------------------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::PlayerLogin& a_Packet) {
|
||||
m_Buffer << a_Packet.m_PlayerName;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::PlayerLogin& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_PlayerName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::UpdateHealth& a_Packet) {
|
||||
m_Buffer << a_Packet.m_NewHealth;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::UpdateHealth& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_NewHealth;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::LoggingSuccess& a_Packet) {
|
||||
m_Buffer << a_Packet.m_PlayerId;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::LoggingSuccess& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_PlayerId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::PlayerDeath& a_Packet) {}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::PlayerDeath& a_Packet) {}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::PlayerJoin& a_Packet) {
|
||||
m_Buffer << a_Packet.m_Player;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::PlayerJoin& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_Player;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::PlayerLeave& a_Packet) {
|
||||
m_Buffer << a_Packet.m_PlayerId;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::PlayerLeave& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_PlayerId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::PlayerList& a_Packet) {
|
||||
m_Buffer << a_Packet.m_Players;
|
||||
// m_Buffer << static_cast<std::uint8_t>(a_Packet.m_Players.size());
|
||||
// for (auto player : a_Packet.m_Players) {
|
||||
// m_Buffer << player.m_PlayerId << player.m_PlayerName;
|
||||
// }
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::PlayerList& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_Players;
|
||||
// std::uint8_t playerCount;
|
||||
// m_Buffer >> playerCount;
|
||||
// for (std::uint8_t i = 0; i < playerCount; i++) {
|
||||
// PlayerInfo player;
|
||||
// m_Buffer >> player.m_PlayerId >> player.m_PlayerName;
|
||||
// a_Packet.m_Players.push_back(player);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::PlayerStats& a_Packet) {}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::PlayerStats& a_Packet) {}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::ServerConfig& a_Packet) {}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::ServerConfig& a_Packet) {}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::ServerTps& a_Packet) {}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::ServerTps& a_Packet) {}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::UpdateGameState& a_Packet) {}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::UpdateGameState& a_Packet) {}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::KeepAlive& a_Packet) {
|
||||
m_Buffer << a_Packet.m_KeepAliveId;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::KeepAlive& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_KeepAliveId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::Disconnect& a_Packet) {}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::Disconnect& a_Packet) {}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::ChatMessage& a_Packet) {
|
||||
m_Buffer << a_Packet.m_Text;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::ChatMessage& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_Text;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::PlayerPositionAndRotation& a_Packet) {
|
||||
m_Buffer << a_Packet.m_Player << a_Packet.m_Position << a_Packet.m_Rotation;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::PlayerPositionAndRotation& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_Player >> a_Packet.m_Position >> a_Packet.m_Rotation;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const data::PlayerShoot& a_Packet) {}
|
||||
|
||||
void Deserializer::DeserializePacketData(data::PlayerShoot& a_Packet) {}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace PacketSerializer
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
11
src/blitz/protocol/PacketVisitor.cpp
Normal file
11
src/blitz/protocol/PacketVisitor.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <blitz/protocol/PacketVisitor.h>
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
|
||||
void PacketVisitor::Check(const Packet& a_Packet) {
|
||||
a_Packet.Accept(*this);
|
||||
}
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
18
src/blitz/protocol/Packets.cpp
Normal file
18
src/blitz/protocol/Packets.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#define BLITZ_INSTANCIATE_PACKETS
|
||||
#include <blitz/protocol/Packets.h>
|
||||
|
||||
#include <blitz/protocol/PacketVisitor.h>
|
||||
|
||||
namespace blitz {
|
||||
namespace protocol {
|
||||
|
||||
template <PacketType PT, typename Data>
|
||||
packets::ConcretePacket<PT, Data>::ConcretePacket(const PacketDataType& a_Data) : m_Data(a_Data) {}
|
||||
|
||||
template <PacketType PT, typename Data>
|
||||
void packets::ConcretePacket<PT, Data>::Accept(PacketVisitor& a_Visitor) const {
|
||||
a_Visitor.Visit(*this);
|
||||
}
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace blitz
|
||||
Reference in New Issue
Block a user