Files
DarkRP2DServer/src/network/Shared.h
2021-05-21 14:14:55 +02:00

128 lines
2.9 KiB
C++

#ifndef SHARED_H
#define SHARED_H
#include <yojimbo.h>
// two channels, one for each type that Yojimbo supports
enum class GameChannel
{
RELIABLE,
UNRELIABLE,
COUNT
};
// the client and server config
struct GameConnectionConfig : yojimbo::ClientServerConfig
{
GameConnectionConfig()
{
numChannels = 2;
channel[(int)GameChannel::RELIABLE].type = yojimbo::CHANNEL_TYPE_RELIABLE_ORDERED;
channel[(int)GameChannel::UNRELIABLE].type = yojimbo::CHANNEL_TYPE_UNRELIABLE_UNORDERED;
}
};
// message types
enum class GameMessageType
{
TEST,
SETNAME,
PHYSICSTATEMESSAGE,
INPUTSTATEMESSAGE,
COUNT
};
class TestMessage : public yojimbo::Message
{
public:
int m_data;
TestMessage() : m_data(0) {}
template <typename Stream>
bool Serialize(Stream &stream)
{
serialize_int(stream, m_data, 0, 512);
return true;
}
YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
};
class SetNameMessage : public yojimbo::Message
{
public:
char buffer[yojimbo::MaxAddressLength];
SetNameMessage() {}
template <typename Stream>
bool Serialize(Stream &stream)
{
serialize_string(stream, buffer, sizeof(buffer));
return true;
}
YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
};
class PhysicStateMessage : public yojimbo::Message
{
public:
int m_objectID;
float m_posX;
float m_posY;
bool isActive;
PhysicStateMessage() : m_objectID(0), m_posX(0), m_posY(0), isActive(false) {}
template <typename Stream>
bool Serialize(Stream &stream)
{
serialize_int(stream, m_objectID, 0, 512);
serialize_float(stream, m_posX);
serialize_float(stream, m_posY);
serialize_bool(stream, isActive);
return true;
}
YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
};
class InputStateMessage : public yojimbo::Message
{
public:
float m_vel_x, m_vel_y;
InputStateMessage() : m_vel_x(0), m_vel_y(0) {}
template <typename Stream>
bool Serialize(Stream &stream)
{
serialize_float(stream, m_vel_x);
serialize_float(stream, m_vel_y);
return true;
}
YOJIMBO_VIRTUAL_SERIALIZE_FUNCTIONS();
};
// the message factory
YOJIMBO_MESSAGE_FACTORY_START(GameMessageFactory, (int)GameMessageType::COUNT);
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::TEST, TestMessage);
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::SETNAME, SetNameMessage);
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::PHYSICSTATEMESSAGE, PhysicStateMessage);
YOJIMBO_DECLARE_MESSAGE_TYPE((int)GameMessageType::INPUTSTATEMESSAGE, InputStateMessage);
YOJIMBO_MESSAGE_FACTORY_FINISH();
// the adapter
class SharedAdapter : public yojimbo::Adapter
{
public:
yojimbo::MessageFactory *CreateMessageFactory(yojimbo::Allocator &allocator) override
{
return YOJIMBO_NEW(allocator, GameMessageFactory, allocator);
}
};
static const uint8_t DEFAULT_PRIVATE_KEY[yojimbo::KeyBytes] = {0};
#endif //SHARED_H