declare packets + commands

This commit is contained in:
2024-10-13 15:30:57 +02:00
parent 77317df56c
commit 0354ce776b
18 changed files with 461 additions and 273 deletions

View File

@@ -1,94 +0,0 @@
#pragma once
#include <godot_cpp/variant/packed_byte_array.hpp>
#include <godot_cpp/variant/string.hpp>
#include <stdexcept>
#include <vector>
namespace td {
namespace protocol {
class PlayerInfo;
class ByteBuffer {
private:
godot::PackedByteArray m_Buffer;
std::size_t m_ReadOffset;
public:
class ReadError : public std::runtime_error {
public:
ReadError(const std::string& msg) : std::runtime_error(msg) {}
};
ByteBuffer(godot::PackedByteArray&& a_Buffer) : m_Buffer(std::move(a_Buffer)), m_ReadOffset(0) {}
ByteBuffer() : m_ReadOffset(0) {
m_Buffer.resize(0);
}
const godot::PackedByteArray& GetByteArray() const {
return m_Buffer;
}
godot::PackedByteArray& GetByteArray() {
return m_Buffer;
}
// Integers
ByteBuffer& operator<<(int8_t a_Data);
ByteBuffer& operator>>(int8_t& a_Data);
ByteBuffer& operator<<(uint8_t a_Data);
ByteBuffer& operator>>(uint8_t& a_Data);
ByteBuffer& operator<<(int16_t a_Data);
ByteBuffer& operator>>(int16_t& a_Data);
ByteBuffer& operator<<(uint16_t a_Data);
ByteBuffer& operator>>(uint16_t& a_Data);
ByteBuffer& operator<<(int32_t a_Data);
ByteBuffer& operator>>(int32_t& a_Data);
ByteBuffer& operator<<(uint32_t a_Data);
ByteBuffer& operator>>(uint32_t& a_Data);
ByteBuffer& operator<<(int64_t a_Data);
ByteBuffer& operator>>(int64_t& a_Data);
ByteBuffer& operator<<(uint64_t a_Data);
ByteBuffer& operator>>(uint64_t& a_Data);
ByteBuffer& operator<<(float a_Data);
ByteBuffer& operator>>(float& a_Data);
ByteBuffer& operator<<(double a_Data);
ByteBuffer& operator>>(double& a_Data);
ByteBuffer& operator<<(const godot::String& a_Data);
ByteBuffer& operator>>(godot::String& a_Data);
ByteBuffer& operator<<(const godot::Vector3& a_Data);
ByteBuffer& operator>>(godot::Vector3& a_Data);
template <typename T>
ByteBuffer& operator<<(const std::vector<T>& a_Data) {
*this << static_cast<std::uint32_t>(a_Data.size());
for (const T& data : a_Data) {
*this << data;
}
return *this;
}
template <typename T>
ByteBuffer& operator>>(std::vector<T>& a_Data) {
std::uint32_t arraySize;
*this >> arraySize;
a_Data.resize(arraySize);
for (std::uint32_t i = 0; i < arraySize; i++) {
*this >> a_Data[i];
}
return *this;
}
ByteBuffer& operator<<(const PlayerInfo& a_Data);
ByteBuffer& operator>>(PlayerInfo& a_Data);
};
} // namespace protocol
} // namespace td

View File

@@ -2,25 +2,11 @@
#include <string>
#include <td/Types.h>
#include <array>
namespace td {
namespace protocol {
#define LOCKSTEP_BUFFER_SIZE 10
struct LockStepCommand {};
struct LockStep {
std::uint8_t m_CommandNumber;
std::vector<LockStepCommand> m_Commands;
};
struct LockSteps {
std::uint16_t m_FirstFrameNumber;
std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
};
namespace cdata {
@@ -58,6 +44,8 @@ struct PlayerJoin {
std::string m_Name;
};
struct End {};
} // namespace cdata

View File

@@ -6,15 +6,16 @@ namespace protocol {
/**
* \def DeclareAllPacket
* \brief Avoids repetitive operations on packets
* \brief Avoids repetitive operations on commands
*/
#define DeclareAllCommand() \
DeclareCommand(End) \
DeclareCommand(PlaceTower) \
DeclareCommand(UpgradeTower) \
DeclareCommand(PlayerJoin) \
DeclareCommand(SpawnTroop) \
DeclareCommand(UseItem) \
DeclareCommand(TeamChange) \
DeclareCommand(PlayerJoin)
DeclareCommand(UpgradeTower) \
DeclareCommand(UseItem)
} // namespace protocol

View File

@@ -6,13 +6,12 @@
*/
#include <td/protocol/Dispatcher.h>
#include <td/protocol/command/Commands.h>
namespace td {
namespace protocol {
class CommandHandler;
using CommandDispatcher = Dispatcher<CommandType, CommandHandler, Command>;
using CommandDispatcher = Dispatcher<CommandType, CommandVisitor, Command>;
} // namespace protocol
} // namespace td

View File

@@ -1,33 +0,0 @@
#pragma once
/**
* \file CommandHandler.h
* \brief File containing the td::protocol::CommandHandler class
*/
#include <td/protocol/command/CommandVisitor.h>
#include <td/protocol/command/Commands.h>
namespace td {
namespace protocol {
#define DeclareCommand(CommandName, ...) \
virtual void Visit(const commands::CommandName&); \
virtual void HandleCommand(const commands::CommandName&) {}
/**
* \class CommandHandler
* \brief Class used to handle packets
*/
class CommandHandler : public CommandVisitor {
public:
CommandHandler() {}
~CommandHandler() {}
DeclareAllCommand()
};
#undef DeclareCommand
} // namespace protocol
} // namespace td

View File

@@ -9,6 +9,7 @@
#include <td/common/NonCopyable.h>
#include <td/protocol/command/CommandData.h>
#include <td/protocol/command/CommandDeclare.h>
#include <memory>
namespace td {
namespace protocol {
@@ -104,5 +105,7 @@ DeclareAllCommand()
} // namespace commands
using LockStep = std::vector<std::shared_ptr<protocol::Command>>;
} // namespace protocol
} // namespace td

View File

@@ -2,6 +2,10 @@
#include <td/Types.h>
#include <vector>
#include <td/protocol/command/Commands.h>
// Make it dynamic ?
#define LOCKSTEP_BUFFER_SIZE 10
namespace td {
namespace protocol {
@@ -11,64 +15,60 @@ struct PlayerInfo {
std::string m_PlayerName;
};
struct MapData {
};
namespace pdata {
/** Client attempts to login (very basic) */
struct PlayerLogin {
std::string m_PlayerName;
};
struct UpdateHealth {
float m_NewHealth;
};
/** Server indicates success */
struct LoggingSuccess {
PlayerID m_PlayerId;
};
struct PlayerDeath {};
/** Player joins the lobby */
struct PlayerJoin {
PlayerInfo m_Player;
};
/** Player leaves the lobby */
struct PlayerLeave {
PlayerID m_PlayerId;
};
struct PlayerStats {};
struct PlayerList {
std::vector<PlayerInfo> m_Players;
};
struct ServerConfig {};
struct ServerTps {};
struct UpdateGameState {};
/** Keep alive */
struct KeepAlive {
std::uint64_t m_KeepAliveId;
};
struct Disconnect {};
/** Can be used by both client and server */
struct Disconnect {
std::string m_Reason;
};
/** Chat message */
struct ChatMessage {
std::string m_Text;
};
struct PlayerPositionAndRotation {
PlayerID m_Player;
// godot::Vector3 m_Position;
// godot::Vector3 m_Rotation;
// godot::Vector3 m_Velocity;
// TODO: handle players joining in the first second
struct BeginGame {
MapData m_Map;
std::vector<PlayerInfo> m_BlueTeam;
std::vector<PlayerInfo> m_RedTeam;
// optional, used for players joining mid game
std::vector<LockStep> m_FirstLocksteps;
};
struct PlayerShoot {
PlayerID m_Sender;
// godot::Vector3 m_Position;
// godot::Vector3 m_Rotation;
// godot::Vector3 m_Velocity;
struct LockSteps {
std::uint16_t m_FirstFrameNumber;
std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
};
} // namespace pdata

View File

@@ -34,21 +34,14 @@ enum class PacketSendType {
*/
#define DeclareAllPacket() \
DeclarePacket(ChatMessage, Reliable, Both) \
DeclarePacket(BeginGame, Reliable, Server) \
DeclarePacket(Disconnect, Reliable, Both) \
DeclarePacket(KeepAlive, Reliable, Both) \
DeclarePacket(LockSteps, Unreliable, Both) \
DeclarePacket(LoggingSuccess, Reliable, Server) \
DeclarePacket(PlayerDeath, Reliable, Server) \
DeclarePacket(PlayerJoin, Reliable, Server) \
DeclarePacket(PlayerLeave, Reliable, Server) \
DeclarePacket(PlayerList, Reliable, Server) \
DeclarePacket(PlayerLogin, Reliable, Client) \
DeclarePacket(PlayerPositionAndRotation, Unreliable, Both) \
DeclarePacket(PlayerShoot, Reliable, Both) \
DeclarePacket(PlayerStats, Reliable, Server) \
DeclarePacket(ServerConfig, Reliable, Server) \
DeclarePacket(ServerTps, Reliable, Server) \
DeclarePacket(UpdateGameState, Reliable, Server) \
DeclarePacket(UpdateHealth, Reliable, Client)
} // namespace protocol

View File

@@ -6,13 +6,12 @@
*/
#include <td/protocol/Dispatcher.h>
#include <td/protocol/packet/Packets.h>
namespace td {
namespace protocol {
class PacketHandler;
using PacketDispatcher = Dispatcher<PacketType, PacketHandler, Packet>;
using PacketDispatcher = Dispatcher<PacketType, PacketVisitor, Packet>;
} // namespace protocol
} // namespace td

View File

@@ -1,33 +0,0 @@
#pragma once
/**
* \file PacketHandler.h
* \brief File containing the td::protocol::PacketHandler class
*/
#include <td/protocol/packet/PacketVisitor.h>
#include <td/protocol/packet/Packets.h>
namespace td {
namespace protocol {
#define DeclarePacket(PacketName, ...) \
virtual void Visit(const packets::PacketName&); \
virtual void HandlePacket(const packets::PacketName&) {}
/**
* \class PacketHandler
* \brief Class used to handle packets
*/
class PacketHandler : public PacketVisitor {
public:
PacketHandler() {}
~PacketHandler() {}
DeclareAllPacket()
};
#undef DeclarePacket
} // namespace protocol
} // namespace td