R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
NetworkMessages Namespace Reference

Network protocol with unified message format. More...

Enumerations

enum class  MessageType : uint16_t {
  HANDSHAKE_REQUEST = 0x0001 , HANDSHAKE_RESPONSE = 0x0002 , REGISTER_REQUEST = 0x0007 , REGISTER_RESPONSE = 0x0008 ,
  LOGIN_REQUEST = 0x0009 , LOGIN_RESPONSE = 0x000A , DISCONNECT = 0x0003 , KICK = 0x0004 ,
  PING = 0x0005 , PONG = 0x0006 , C2S_PLAYER_INPUT = 0x0100 , C2S_JOIN_GAME = 0x0101 ,
  C2S_LIST_ROOMS = 0x0300 , C2S_CREATE_ROOM = 0x0301 , C2S_JOIN_ROOM = 0x0302 , C2S_LEAVE_ROOM = 0x0306 ,
  C2S_AUTO_MATCHMAKING = 0x0308 , C2S_UPDATE_AUTO_MM_PREF = 0x0309 , C2S_START_MATCHMAKING = 0x0303 , C2S_CANCEL_MATCHMAKING = 0x0304 ,
  C2S_START_GAME = 0x0305 , C2S_CHAT_MESSAGE = 0x0307 , S2C_GAME_STATE = 0x0200 , S2C_GAME_START = 0x0201 ,
  S2C_ENTITY_DESTROYED = 0x0202 , S2C_GAME_OVER = 0x0203 , S2C_GAMERULE_UPDATE = 0x0204 , S2C_ROOM_LIST = 0x0400 ,
  S2C_ROOM_CREATED = 0x0401 , S2C_JOINED_ROOM = 0x0402 , S2C_MATCHMAKING_STARTED = 0x0403 , S2C_MATCH_FOUND = 0x0404 ,
  S2C_ROOM_STATE = 0x0405 , S2C_CHAT_MESSAGE = 0x0406 , S2C_LEFT_ROOM = 0x0407 , UNKNOWN = 0xFFFF
}
 All message types in the R-Type protocol. More...
 

Functions

std::vector< uint8_t > createMessage (MessageType type, const std::vector< uint8_t > &payload)
 Create a message with type and payload.
 
MessageType getMessageType (const std::vector< uint8_t > &packet)
 Get message type from packet.
 
std::vector< uint8_t > getPayload (const std::vector< uint8_t > &packet)
 Get payload from packet (without header)
 
std::vector< uint8_t > serializeString (const std::string &str)
 Serialize a string to bytes.
 
std::string deserializeString (const std::vector< uint8_t > &bytes, size_t &offset)
 Deserialize bytes to string.
 
std::vector< uint8_t > createConnectRequest (const std::string &playerName, bool isSpectator=false)
 Create HANDSHAKE_REQUEST message.
 
std::string parseConnectRequest (const std::vector< uint8_t > &packet, bool &isSpectator)
 Parse HANDSHAKE_REQUEST message.
 
std::string parseConnectRequest (const std::vector< uint8_t > &packet)
 Parse HANDSHAKE_REQUEST message (legacy - without spectator flag)
 
std::vector< uint8_t > createConnectResponse (const std::string &message)
 Create HANDSHAKE_RESPONSE message.
 
std::string parseConnectResponse (const std::vector< uint8_t > &packet)
 Parse HANDSHAKE_RESPONSE message.
 

Detailed Description

Network protocol with unified message format.

Protocol format: [2 bytes: MessageType][4 bytes: payload_length][N bytes: payload]

This is modular, secure, and scalable:

  • Type checking: Every message has a type identifier
  • Validation: Size checks prevent buffer overflows
  • Extensible: Add new message types without changing the protocol

Example: auto packet = createMessage(MessageType::CONNECT_REQUEST, playerName); auto type = getMessageType(packet); auto payload = getPayload(packet);

Enumeration Type Documentation

◆ MessageType

enum class NetworkMessages::MessageType : uint16_t
strong

All message types in the R-Type protocol.

Add new types here as you need them. Each type is 2 bytes (uint16_t).

Message type mapping to Cap'n Proto wrappers:

Enumerator
HANDSHAKE_REQUEST 
HANDSHAKE_RESPONSE 
REGISTER_REQUEST 
REGISTER_RESPONSE 
LOGIN_REQUEST 
LOGIN_RESPONSE 
DISCONNECT 
KICK 
PING 
PONG 
C2S_PLAYER_INPUT 
C2S_JOIN_GAME 
C2S_LIST_ROOMS 
C2S_CREATE_ROOM 
C2S_JOIN_ROOM 
C2S_LEAVE_ROOM 
C2S_AUTO_MATCHMAKING 
C2S_UPDATE_AUTO_MM_PREF 
C2S_START_MATCHMAKING 
C2S_CANCEL_MATCHMAKING 
C2S_START_GAME 
C2S_CHAT_MESSAGE 
S2C_GAME_STATE 
S2C_GAME_START 
S2C_ENTITY_DESTROYED 
S2C_GAME_OVER 
S2C_GAMERULE_UPDATE 
S2C_ROOM_LIST 
S2C_ROOM_CREATED 
S2C_JOINED_ROOM 
S2C_MATCHMAKING_STARTED 
S2C_MATCH_FOUND 
S2C_ROOM_STATE 
S2C_CHAT_MESSAGE 
S2C_LEFT_ROOM 
UNKNOWN 

Definition at line 52 of file NetworkMessages.hpp.

Function Documentation

◆ createConnectRequest()

std::vector< uint8_t > NetworkMessages::createConnectRequest ( const std::string &  playerName,
bool  isSpectator = false 
)
inline

Create HANDSHAKE_REQUEST message.

Parameters
playerNamePlayer's name
isSpectatorWhether this is a spectator connection
Deprecated:
Use RType::Messages::Connection::HandshakeRequest wrapper instead

Definition at line 249 of file NetworkMessages.hpp.

References createMessage(), HANDSHAKE_REQUEST, and serializeString().

Here is the call graph for this function:

◆ createConnectResponse()

std::vector< uint8_t > NetworkMessages::createConnectResponse ( const std::string &  message)
inline

Create HANDSHAKE_RESPONSE message.

Parameters
messageWelcome message
Deprecated:
Use RType::Messages::Connection::HandshakeResponse wrapper instead

Definition at line 305 of file NetworkMessages.hpp.

References createMessage(), HANDSHAKE_RESPONSE, and serializeString().

Referenced by Server::_handleHandshakeRequest().

Here is the call graph for this function:

◆ createMessage()

◆ deserializeString()

std::string NetworkMessages::deserializeString ( const std::vector< uint8_t > &  bytes,
size_t &  offset 
)
inline

Deserialize bytes to string.

Definition at line 205 of file NetworkMessages.hpp.

Referenced by parseConnectRequest(), and parseConnectResponse().

◆ getMessageType()

MessageType NetworkMessages::getMessageType ( const std::vector< uint8_t > &  packet)
inline

Get message type from packet.

Parameters
packetComplete packet with header
Returns
Message type

Definition at line 137 of file NetworkMessages.hpp.

References UNKNOWN.

Referenced by GameLoop::handleNetworkMessage(), Server::handlePacket(), Replicator::networkThreadLoop(), parseConnectRequest(), parseConnectResponse(), and Replicator::processMessages().

◆ getPayload()

std::vector< uint8_t > NetworkMessages::getPayload ( const std::vector< uint8_t > &  packet)
inline

◆ parseConnectRequest() [1/2]

std::string NetworkMessages::parseConnectRequest ( const std::vector< uint8_t > &  packet)
inline

Parse HANDSHAKE_REQUEST message (legacy - without spectator flag)

Parameters
packetComplete packet
Returns
Player name (empty if invalid or wrong type)
Deprecated:
Use parseConnectRequest with isSpectator parameter instead

Definition at line 294 of file NetworkMessages.hpp.

References parseConnectRequest().

Here is the call graph for this function:

◆ parseConnectRequest() [2/2]

std::string NetworkMessages::parseConnectRequest ( const std::vector< uint8_t > &  packet,
bool &  isSpectator 
)
inline

Parse HANDSHAKE_REQUEST message.

Parameters
packetComplete packet
[out]isSpectatorSet to true if this is a spectator connection
Returns
Player name (empty if invalid or wrong type)
Deprecated:
Use RType::Messages::Connection::HandshakeRequest wrapper instead

Definition at line 267 of file NetworkMessages.hpp.

References deserializeString(), getMessageType(), getPayload(), and HANDSHAKE_REQUEST.

Referenced by parseConnectRequest().

Here is the call graph for this function:

◆ parseConnectResponse()

std::string NetworkMessages::parseConnectResponse ( const std::vector< uint8_t > &  packet)
inline

Parse HANDSHAKE_RESPONSE message.

Parameters
packetComplete packet
Returns
Welcome message (empty if invalid or wrong type)
Deprecated:
Use RType::Messages::Connection::HandshakeResponse wrapper instead

Definition at line 317 of file NetworkMessages.hpp.

References deserializeString(), getMessageType(), getPayload(), and HANDSHAKE_RESPONSE.

Here is the call graph for this function:

◆ serializeString()

std::vector< uint8_t > NetworkMessages::serializeString ( const std::string &  str)
inline

Serialize a string to bytes.

Definition at line 186 of file NetworkMessages.hpp.

Referenced by createConnectRequest(), and createConnectResponse().