R-Type 0.1
Loading...
Searching...
No Matches
Protocol payloads

Message types, payload layouts and value encodings shared by the client and the server (src/Common/Protocol). More...

Classes

struct  rtype::protocol::BigEndian< T >
 Unsigned integer stored as big-endian bytes, with an alignment of 1. More...
 
struct  rtype::protocol::DisconnectPayload
 Payload of a Disconnect message, 1 byte. More...
 
struct  rtype::protocol::CreateEntityPayload
 Payload of a CreateEntity message, 16 bytes, sent by the server to spawn an entity on a client. More...
 
struct  rtype::protocol::DestroyEntityPayload
 Payload of a DestroyEntity message, 9 bytes, sent by the server to remove an entity from a client. More...
 
struct  rtype::protocol::QueryEntityPayload
 Payload of a QueryEntity message, 4 bytes, sent by a client that received a snapshot for an entity it does not know. More...
 
struct  rtype::protocol::InputPayload
 Payload of an Input message, 9 bytes, sent by a client every frame with the actions currently held. More...
 
struct  rtype::protocol::SnapshotHeader
 Fixed part of a Snapshot message, 6 bytes. More...
 
struct  rtype::protocol::SnapshotEntityState
 State of one entity inside a Snapshot message, 11 bytes. More...
 
struct  rtype::protocol::SnapshotPayload
 Decoded Snapshot message: a SnapshotHeader followed by entityCount SnapshotEntityState records. More...
 

Typedefs

using rtype::protocol::be16 = BigEndian<std::uint16_t>
 16-bit unsigned integer stored in big-endian order.
 
using rtype::protocol::be32 = BigEndian<std::uint32_t>
 32-bit unsigned integer stored in big-endian order.
 
using rtype::protocol::NetworkEntityId = std::uint32_t
 Server assigned identifier of a replicated entity, 0 means no entity.
 

Enumerations

enum class  rtype::protocol::DisconnectReason : std::uint8_t { DisconnectReason::ClientQuit = 0x01 , DisconnectReason::Timeout = 0x02 , DisconnectReason::ProtocolError = 0x03 , DisconnectReason::ServerShutdown = 0x04 }
 Reason carried by a Disconnect message. More...
 
enum class  rtype::protocol::DestroyReason : std::uint8_t {
  DestroyReason::Unknown = 0x00 , DestroyReason::Collision = 0x01 , DestroyReason::OutOfBounds = 0x02 , DestroyReason::PlayerDisconnected = 0x03 ,
  DestroyReason::GameEnded = 0x04
}
 Reason carried by a DestroyEntity message. More...
 
enum class  rtype::protocol::InputAction : std::uint8_t {
  InputAction::NoneAction = 0x00 , InputAction::Up = 0x01 , InputAction::Down = 0x02 , InputAction::Left = 0x04 ,
  InputAction::Right = 0x08 , InputAction::Shoot = 0x10
}
 Bit flags of the actions held by a player. More...
 
enum class  rtype::protocol::MessageType : std::uint16_t {
  MessageType::Invalid = 0 , MessageType::Input = 0x02 , MessageType::Ping = 0x03 , MessageType::Disconnect = 0x04 ,
  MessageType::QueryEntity = 0x13 , MessageType::Snapshot = 0x83 , MessageType::CreateEntity = 0x84 , MessageType::DestroyEntity = 0x85 ,
  MessageType::Pong = 0x86
}
 Type identifier of every message exchanged on a connection. More...
 

Functions

constexpr bool rtype::protocol::isValid (const DisconnectPayload &payload)
 Checks that the reason is a known value.
 
constexpr bool rtype::protocol::isValid (const CreateEntityPayload &payload)
 Checks that the entity id is not 0.
 
constexpr bool rtype::protocol::isValid (const DestroyEntityPayload &payload)
 Checks the entity id and the destroy reason.
 
constexpr bool rtype::protocol::isValid (const QueryEntityPayload &payload)
 Checks that the entity id is not 0.
 
CreateEntityPayload rtype::protocol::createEntityPayload (std::uint32_t serverTick, std::uint32_t networkId, std::uint16_t entityType, float x, float y, float rotation=0.0f)
 Builds a CreateEntity payload from world values.
 
DestroyEntityPayload rtype::protocol::destroyEntityPayload (std::uint32_t serverTick, std::uint32_t networkId, DestroyReason reason)
 Builds a DestroyEntity payload.
 
constexpr InputAction rtype::protocol::operator| (InputAction left, InputAction right)
 Combines two action sets.
 
constexpr bool rtype::protocol::hasAction (InputAction actions, InputAction action)
 Tells whether an action set contains an action.
 
constexpr std::uint16_t rtype::protocol::toMessageId (MessageType type)
 Converts a message type to its wire identifier.
 
template<typename T >
requires std::is_trivially_copyable_v<T>
bool rtype::protocol::writePayload (PacketWriter &writer, const T &payload)
 Appends the raw bytes of a padding free payload struct.
 
template<typename T >
requires std::is_trivially_copyable_v<T>
bool rtype::protocol::readPayload (PacketReader &reader, T &payload)
 Reads the raw bytes of a padding free payload struct.
 
be16 rtype::protocol::encodePosition (float position)
 Encodes a position as a 16-bit fixed point value.
 
float rtype::protocol::decodePosition (be16 encoded)
 Decodes a position encoded by encodePosition().
 
be16 rtype::protocol::encodeRotation (float radians)
 Encodes an angle as a fraction of a full turn on 16 bits.
 
float rtype::protocol::decodeRotation (be16 encoded)
 Decodes an angle encoded by encodeRotation().
 
bool rtype::protocol::writeSnapshot (PacketWriter &writer, const SnapshotPayload &payload)
 Encodes a snapshot.
 
bool rtype::protocol::readSnapshot (PacketReader &reader, SnapshotPayload &payload)
 Decodes and validates a snapshot.
 

Variables

constexpr float rtype::protocol::POSITION_SCALE = 4.0f
 Number of encoded steps per pixel, giving a 0.25 pixel resolution.
 
constexpr float rtype::protocol::POSITION_OFFSET = 8192.0f
 Offset added before encoding so that negative positions fit.
 
constexpr float rtype::protocol::MIN_POSITION = -POSITION_OFFSET
 Smallest position that can be encoded, in pixels.
 
constexpr float rtype::protocol::MAX_POSITION
 Largest position that can be encoded, in pixels.
 
constexpr std::size_t rtype::protocol::SNAPSHOT_MAX_ENTITIES
 Largest number of entity states that fit in one Snapshot message.
 

Detailed Description

Message types, payload layouts and value encodings shared by the client and the server (src/Common/Protocol).

See Network protocol specification for the full specification.

Enumeration Type Documentation

◆ DisconnectReason

enum class rtype::protocol::DisconnectReason : std::uint8_t
strong

Reason carried by a Disconnect message.

Enumerator
ClientQuit 

The player left the game.

Timeout 

The peer stopped answering.

ProtocolError 

The peer sent invalid data.

ServerShutdown 

The server is stopping.

◆ DestroyReason

enum class rtype::protocol::DestroyReason : std::uint8_t
strong

Reason carried by a DestroyEntity message.

Enumerator
Unknown 

No specific reason.

Collision 

Removed by gameplay, such as a hit.

OutOfBounds 

Left the playable area.

PlayerDisconnected 

Owner of the ship left the game.

GameEnded 

The game is over.

◆ InputAction

enum class rtype::protocol::InputAction : std::uint8_t
strong

Bit flags of the actions held by a player.

Enumerator
NoneAction 

No action held.

Up 

Move up.

Down 

Move down.

Left 

Move left.

Right 

Move right.

Shoot 

Shoot button held.

◆ MessageType

enum class rtype::protocol::MessageType : std::uint16_t
strong

Type identifier of every message exchanged on a connection.

Enumerator
Invalid 

Never sent, marks an unset type.

Input 

Client to server: actions held by the player.

Ping 

Client to server: latency probe.

Disconnect 

Client to server: closes the connection.

QueryEntity 

Client to server: asks for an unknown entity.

Snapshot 

Server to client: positions of the entities.

CreateEntity 

Server to client: spawns an entity.

DestroyEntity 

Server to client: removes an entity.

Pong 

Server to client: answer to a Ping.

Function Documentation

◆ isValid() [1/4]

bool rtype::protocol::isValid ( const DisconnectPayload & payload)
constexpr

Checks that the reason is a known value.

Parameters
payloadDecoded payload.
Returns
True if the payload is valid.

◆ isValid() [2/4]

bool rtype::protocol::isValid ( const CreateEntityPayload & payload)
constexpr

Checks that the entity id is not 0.

Parameters
payloadDecoded payload.
Returns
True if the payload is valid.

◆ isValid() [3/4]

bool rtype::protocol::isValid ( const DestroyEntityPayload & payload)
constexpr

Checks the entity id and the destroy reason.

Parameters
payloadDecoded payload.
Returns
True if the payload is valid.

◆ isValid() [4/4]

bool rtype::protocol::isValid ( const QueryEntityPayload & payload)
constexpr

Checks that the entity id is not 0.

Parameters
payloadDecoded payload.
Returns
True if the payload is valid.

◆ createEntityPayload()

CreateEntityPayload rtype::protocol::createEntityPayload ( std::uint32_t serverTick,
std::uint32_t networkId,
std::uint16_t entityType,
float x,
float y,
float rotation = 0.0f )
inline

Builds a CreateEntity payload from world values.

Parameters
serverTickCurrent server tick.
networkIdIdentifier of the entity.
entityTypeArchetype id of the entity blueprint.
xHorizontal position in pixels.
yVertical position in pixels.
rotationRotation in radians.
Returns
The encoded payload.

◆ destroyEntityPayload()

DestroyEntityPayload rtype::protocol::destroyEntityPayload ( std::uint32_t serverTick,
std::uint32_t networkId,
DestroyReason reason )
inline

Builds a DestroyEntity payload.

Parameters
serverTickCurrent server tick.
networkIdIdentifier of the entity.
reasonWhy the entity is removed.
Returns
The encoded payload.

◆ operator|()

InputAction rtype::protocol::operator| ( InputAction left,
InputAction right )
constexpr

Combines two action sets.

Parameters
leftFirst action set.
rightSecond action set.
Returns
The union of both sets.

◆ hasAction()

bool rtype::protocol::hasAction ( InputAction actions,
InputAction action )
constexpr

Tells whether an action set contains an action.

Parameters
actionsAction set to test.
actionAction to look for.
Returns
True if the action is set.

◆ toMessageId()

std::uint16_t rtype::protocol::toMessageId ( MessageType type)
constexpr

Converts a message type to its wire identifier.

Parameters
typeMessage type.
Returns
The 16-bit identifier written in the message header.

◆ writePayload()

template<typename T >
requires std::is_trivially_copyable_v<T>
bool rtype::protocol::writePayload ( PacketWriter & writer,
const T & payload )

Appends the raw bytes of a padding free payload struct.

Template Parameters
TPayload type, made of single bytes and BigEndian fields.
Parameters
writerDestination buffer.
payloadPayload to write.
Returns
True on success, false if the buffer is full.

◆ readPayload()

template<typename T >
requires std::is_trivially_copyable_v<T>
bool rtype::protocol::readPayload ( PacketReader & reader,
T & payload )

Reads the raw bytes of a padding free payload struct.

Template Parameters
TPayload type, made of single bytes and BigEndian fields.
Parameters
readerSource buffer.
payloadReceives the payload.
Returns
True on success, false if not enough bytes remain.

◆ encodePosition()

be16 rtype::protocol::encodePosition ( float position)
inline

Encodes a position as a 16-bit fixed point value.

Parameters
positionPosition in pixels, clamped to the encodable range.
Returns
round((position + 8192) * 4), or the encoding of 0 if not finite.

◆ decodePosition()

float rtype::protocol::decodePosition ( be16 encoded)
inline

Decodes a position encoded by encodePosition().

Parameters
encodedEncoded value.
Returns
The position in pixels.

◆ encodeRotation()

be16 rtype::protocol::encodeRotation ( float radians)
inline

Encodes an angle as a fraction of a full turn on 16 bits.

Parameters
radiansAngle in radians, any value.
Returns
0 for 0 radians, 32768 for pi, wrapping at 65536.

◆ decodeRotation()

float rtype::protocol::decodeRotation ( be16 encoded)
inline

Decodes an angle encoded by encodeRotation().

Parameters
encodedEncoded value.
Returns
The angle in radians, in the range [0, 2 pi).

◆ writeSnapshot()

bool rtype::protocol::writeSnapshot ( PacketWriter & writer,
const SnapshotPayload & payload )
inline

Encodes a snapshot.

Parameters
writerDestination buffer.
payloadSnapshot with at most SNAPSHOT_MAX_ENTITIES states.
Returns
True on success, false if there are too many states or no room.

◆ readSnapshot()

bool rtype::protocol::readSnapshot ( PacketReader & reader,
SnapshotPayload & payload )
inline

Decodes and validates a snapshot.

Parameters
readerBuffer holding exactly one snapshot payload.
payloadReceives the snapshot, left untouched on failure.
Returns
True if the size, count and entity ids are all valid.

Variable Documentation

◆ MAX_POSITION

float rtype::protocol::MAX_POSITION
inlineconstexpr
Initial value:
=
static_cast<float>(std::numeric_limits<std::uint16_t>::max()) /
POSITION_SCALE -
POSITION_OFFSET

Largest position that can be encoded, in pixels.

◆ SNAPSHOT_MAX_ENTITIES

std::size_t rtype::protocol::SNAPSHOT_MAX_ENTITIES
inlineconstexpr
Initial value:
=
(MAX_MESSAGE_SIZE - MessageHeader::HEADER_SIZE - sizeof(SnapshotHeader)) /
sizeof(SnapshotEntityState)
constexpr std::size_t MAX_MESSAGE_SIZE
Room left for messages after the packet header, in bytes.
Definition NetworkConfig.hpp:22
static constexpr std::size_t HEADER_SIZE
Encoded size of the header, in bytes.
Definition MessageHeader.hpp:55

Largest number of entity states that fit in one Snapshot message.