add tests
This commit is contained in:
14
src/td/misc/Test.cpp
Normal file
14
src/td/misc/Test.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <td/misc/Format.h>
|
||||
#include <td/misc/Log.h>
|
||||
#include <td/misc/Test.h>
|
||||
|
||||
namespace td {
|
||||
namespace test {
|
||||
|
||||
void LogAssert(const char* expression, const char* file, int line, const char* function) {
|
||||
utils::LOGE(utils::Format("%s:%i: %s: Assertion failed !", file, line, function));
|
||||
utils::LOGE(utils::Format(" %i |\t%s;", line, expression));
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
} // namespace td
|
||||
26
src/td/protocol/command/CommandFactory.cpp
Normal file
26
src/td/protocol/command/CommandFactory.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <td/protocol/command/CommandFactory.h>
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
namespace CommandFactory {
|
||||
|
||||
using CommandCreator = std::function<std::unique_ptr<Command>()>;
|
||||
|
||||
#define DeclareCommand(CommandName, ...) std::make_unique<commands::CommandName>(),
|
||||
|
||||
static std::array<std::unique_ptr<Command>, static_cast<std::size_t>(CommandType::COMMAND_COUNT)> Commands = {
|
||||
DeclareAllCommand()
|
||||
};
|
||||
|
||||
const std::unique_ptr<Command>& CreateReadOnlyCommand(CommandType a_Type) {
|
||||
assert(a_Type < CommandType::COMMAND_COUNT);
|
||||
return Commands[static_cast<std::size_t>(a_Type)];
|
||||
}
|
||||
|
||||
} // namespace CommandFactory
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
11
src/td/protocol/command/CommandVisitor.cpp
Normal file
11
src/td/protocol/command/CommandVisitor.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <td/protocol/command/CommandVisitor.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
void CommandVisitor::Check(const Command& a_Command) {
|
||||
a_Command.Accept(*this);
|
||||
}
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
26
src/td/protocol/packet/PacketFactory.cpp
Normal file
26
src/td/protocol/packet/PacketFactory.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <td/protocol/packet/PacketFactory.h>
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
|
||||
namespace td {
|
||||
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 = {
|
||||
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 td
|
||||
239
src/td/protocol/packet/PacketSerializer.cpp
Normal file
239
src/td/protocol/packet/PacketSerializer.cpp
Normal file
@@ -0,0 +1,239 @@
|
||||
#include <td/protocol/packet/PacketSerializer.h>
|
||||
|
||||
#include <td/protocol/packet/PacketFactory.h>
|
||||
#include <td/protocol/packet/PacketVisitor.h>
|
||||
|
||||
#include <td/misc/Format.h>
|
||||
#include <td/misc/Log.h>
|
||||
|
||||
namespace td {
|
||||
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:
|
||||
DataBuffer& m_Buffer;
|
||||
|
||||
public:
|
||||
Serializer(DataBuffer& 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:
|
||||
DataBuffer& m_Buffer;
|
||||
PacketPtr m_Packet;
|
||||
|
||||
public:
|
||||
Deserializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {}
|
||||
|
||||
bool Deserialize(const PacketPtr& a_Packet) {
|
||||
try {
|
||||
Check(*a_Packet.get());
|
||||
} catch (std::exception& e) {
|
||||
utils::LOGE(utils::Format("[PacketSerializer::Deserializer] %s", e.what()));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
PacketPtr& GetPacket() {
|
||||
return m_Packet;
|
||||
}
|
||||
|
||||
DeclareAllPacket()
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
DataBuffer Serialize(const Packet& a_Packet) {
|
||||
DataBuffer buffer;
|
||||
|
||||
Serializer serializer(buffer);
|
||||
serializer.Serialize(a_Packet);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::unique_ptr<Packet> Deserialize(DataBuffer& a_Data) {
|
||||
PacketID packetId;
|
||||
a_Data >> 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(a_Data);
|
||||
|
||||
if (deserializer.Deserialize(emptyPacket)) {
|
||||
PacketPtr packet = std::move(deserializer.GetPacket());
|
||||
return packet;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------
|
||||
// Packet serializer implementation
|
||||
//----------------------------------------------
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const pdata::PlayerLogin& a_Packet) {
|
||||
m_Buffer << a_Packet.m_PlayerName;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(pdata::PlayerLogin& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_PlayerName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const pdata::LoggingSuccess& a_Packet) {
|
||||
m_Buffer << a_Packet.m_PlayerId;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(pdata::LoggingSuccess& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_PlayerId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const pdata::PlayerJoin& a_Packet) {
|
||||
m_Buffer << a_Packet.m_Player;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(pdata::PlayerJoin& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_Player;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const pdata::PlayerLeave& a_Packet) {
|
||||
m_Buffer << a_Packet.m_PlayerId;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(pdata::PlayerLeave& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_PlayerId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const pdata::KeepAlive& a_Packet) {
|
||||
m_Buffer << a_Packet.m_KeepAliveId;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(pdata::KeepAlive& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_KeepAliveId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const pdata::Disconnect& a_Packet) {
|
||||
m_Buffer << a_Packet.m_Reason;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(pdata::Disconnect& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_Reason;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const pdata::ChatMessage& a_Packet) {
|
||||
m_Buffer << a_Packet.m_Text;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(pdata::ChatMessage& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_Text;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const pdata::LockSteps& a_Packet) {
|
||||
m_Buffer << a_Packet.m_FirstFrameNumber << a_Packet.m_LockSteps;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(pdata::LockSteps& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_FirstFrameNumber >> a_Packet.m_LockSteps;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Serializer::SerializePacketData(const pdata::BeginGame& a_Packet) {
|
||||
m_Buffer << a_Packet.m_Map << a_Packet.m_BlueTeam << a_Packet.m_RedTeam << a_Packet.m_Map;
|
||||
}
|
||||
|
||||
void Deserializer::DeserializePacketData(pdata::BeginGame& a_Packet) {
|
||||
m_Buffer >> a_Packet.m_Map >> a_Packet.m_BlueTeam >> a_Packet.m_RedTeam >> a_Packet.m_Map;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace PacketSerializer
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
11
src/td/protocol/packet/PacketVisitor.cpp
Normal file
11
src/td/protocol/packet/PacketVisitor.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <td/protocol/packet/PacketVisitor.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
void PacketVisitor::Check(const Packet& a_Packet) {
|
||||
a_Packet.Accept(*this);
|
||||
}
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
23
src/td/protocol/packet/Packets.cpp
Normal file
23
src/td/protocol/packet/Packets.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#define TD_INSTANCIATE_PACKETS
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
#include <td/protocol/packet/PacketVisitor.h>
|
||||
|
||||
namespace td {
|
||||
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);
|
||||
}
|
||||
|
||||
#define DeclarePacket(PacketName, packetSendType, packetSenderType) \
|
||||
static_assert(static_cast<unsigned>(PacketSendType::packetSendType) && static_cast<unsigned>(PacketSenderType::packetSenderType));
|
||||
|
||||
DeclareAllPacket()
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
Reference in New Issue
Block a user