r-type  0.0.0
R-Type main
Loading...
Searching...
No Matches
eng::AsioClient Class Referencefinal

High-performance UDP client implementation using ASIO library. More...

#include <AsioClient.hpp>

+ Inheritance diagram for eng::AsioClient:
+ Collaboration diagram for eng::AsioClient:

Public Member Functions

 AsioClient ()
 Constructor.
 
 ~AsioClient () override
 Destructor.
 
void connect (const std::string &host, std::uint16_t port) override
 Connect to game server.
 
void disconnect () override
 Disconnect from server.
 
void update () override
 Update client state (called each frame)
 
const std::string getName () const override
 
utl::PluginType getType () const override
 
bool isConnected () const override
 Check if client is connected to server.
 
ConnectionState getConnectionState () const override
 Get current connection state.
 
std::uint32_t getSessionId () const override
 Get assigned session ID.
 
std::uint16_t getServerTickRate () const override
 Get server tick rate.
 
std::uint32_t getLatency () const override
 Get current latency to server.
 
void setPlayerName (const std::string &playerName) override
 Set player name for connection.
 
void setClientCapabilities (std::uint32_t caps) override
 Set client capability flags.
 
void processBusEvent ()
 Process events from event bus.
 
void sendToServer (const std::vector< std::uint8_t > &data, bool reliable=false) override
 Send custom packet to server.
 
const ConnectionStatsgetStats () const
 Get connection statistics.
 
void requestLobbyList ()
 Request list of available lobbies from server.
 
void createLobby (rnp::PacketLobbyCreate lobbyCreate)
 Create a new lobby on server.
 
void joinLobby (std::uint32_t lobbyId)
 Join an existing lobby.
 
void leaveLobby ()
 Leave current lobby.
 
void requestStartGame (std::uint32_t lobbyId)
 Request to start the game (host only)
 
std::uint32_t getCurrentLobbyId () const
 Get current lobby ID.
 
bool isInLobby () const
 Check if player is in a lobby.
 
void setOnLobbyListReceived (std::function< void(const std::vector< rnp::LobbyInfo > &)> callback)
 Set callback for lobby list received.
 
void setOnLobbyCreated (std::function< void(std::uint32_t, bool, rnp::ErrorCode)> callback)
 Set callback for lobby creation response.
 
void setOnLobbyJoined (std::function< void(std::uint32_t, bool, rnp::ErrorCode, const rnp::LobbyInfo *)> callback)
 Set callback for lobby join response.
 
void setOnLobbyUpdated (std::function< void(const rnp::LobbyInfo &)> callback)
 Set callback for lobby updates.
 
void setOnGameStart (std::function< void(std::uint32_t, std::uint32_t)> callback)
 Set callback for game start notification.
 
- Public Member Functions inherited from eng::INetworkClient
virtual ~INetworkClient ()=default
 
virtual void connect (const std::string &host, uint16_t port)=0
 
- Public Member Functions inherited from utl::IPlugin
virtual ~IPlugin ()=default
 

Private Member Functions

void setupPacketHandlers ()
 Setup packet handlers for all RNP packet types.
 
void startReceive ()
 Start asynchronous receive operation.
 
void handleReceive (std::size_t bytesReceived)
 Handle received packet data.
 
void networkThreadLoop () const
 Main network thread loop.
 
void sendPacketImmediate (const std::vector< std::uint8_t > &data)
 Send packet immediately via UDP socket.
 
rnp::HandlerResult handleConnectAccept (const rnp::PacketConnectAccept &packet, const rnp::PacketContext &context)
 Handle CONNECT_ACCEPT packet.
 
rnp::HandlerResult handleDisconnect (const rnp::PacketDisconnect &packet, const rnp::PacketContext &context)
 Handle DISCONNECT packet.
 
rnp::HandlerResult handlePong (const rnp::PacketPingPong &packet, const rnp::PacketContext &context)
 Handle PONG packet.
 
rnp::HandlerResult handlePing (const rnp::PacketPingPong &packet, const rnp::PacketContext &context)
 Handle PING packet.
 
rnp::HandlerResult handleWorldState (const rnp::PacketWorldState &packet, const rnp::PacketContext &context) const
 Handle WORLD_STATE packet.
 
rnp::HandlerResult handleEntityEvent (const std::vector< rnp::EventRecord > &events, const rnp::PacketContext &context) const
 Handle ENTITY_EVENT packet.
 
rnp::HandlerResult handleLobbyListResponse (const rnp::PacketLobbyListResponse &packet, const rnp::PacketContext &context) const
 Handle LOBBY_LIST_RESPONSE packet.
 
rnp::HandlerResult handleLobbyCreateResponse (const rnp::PacketLobbyCreateResponse &packet, const rnp::PacketContext &context)
 Handle LOBBY_CREATE_RESPONSE packet.
 
rnp::HandlerResult handleLobbyJoinResponse (const rnp::PacketLobbyJoinResponse &packet, const rnp::PacketContext &context)
 Handle LOBBY_JOIN_RESPONSE packet.
 
rnp::HandlerResult handleLobbyUpdate (const rnp::PacketLobbyUpdate &packet, const rnp::PacketContext &context) const
 Handle LOBBY_UPDATE packet.
 
rnp::HandlerResult handleGameStart (const rnp::PacketGameStart &packet, const rnp::PacketContext &context) const
 Handle GAME_START packet.
 
void sendConnect ()
 Send CONNECT packet to initiate connection.
 
void sendDisconnect ()
 Send DISCONNECT packet to terminate connection.
 
void sendPing ()
 Send PING packet for latency measurement.
 
void sendPong (std::uint32_t nonce)
 Send PONG response to server PING.
 
void processSendQueue ()
 Process outgoing packet queue.
 
void updateConnectionManagement ()
 Update connection management state.
 

Static Private Member Functions

static rnp::HandlerResult handleError (const rnp::PacketError &packet, const rnp::PacketContext &context)
 Handle ERROR packet.
 
static std::uint32_t generatePingNonce ()
 Generate random ping nonce.
 

Private Attributes

std::unique_ptr< asio::io_context > m_ioContext
 ASIO I/O context for async operations.
 
std::unique_ptr< asio::ip::udp::socket > m_socket
 UDP socket for network communication.
 
std::unique_ptr< std::thread > m_networkThread
 Dedicated network thread.
 
std::string m_serverHost
 Server hostname or IP address.
 
std::uint16_t m_serverPort
 Server port number.
 
asio::ip::udp::endpoint m_serverEndpoint
 Resolved server endpoint.
 
std::atomic< ConnectionStatem_connectionState
 Current connection state (atomic)
 
std::uint32_t m_sessionId
 Assigned session ID from server.
 
std::uint16_t m_serverTickRate
 Server tick rate in Hz.
 
std::uint32_t m_clientCaps
 Client capability flags.
 
std::string m_playerName
 Player display name.
 
std::atomic< bool > m_running
 Network thread running state (atomic)
 
std::unique_ptr< rnp::HandlerPacketm_packetHandler
 RNP packet handler instance.
 
std::queue< QueuedPacketm_sendQueue
 Queue of packets waiting to be sent.
 
std::mutex m_sendQueueMutex
 Mutex for send queue access.
 
std::array< std::uint8_t, 1024 > m_recvBuffer
 Buffer for receiving UDP packets.
 
asio::ip::udp::endpoint m_senderEndpoint
 Endpoint of last packet sender.
 
std::uint32_t m_lastPingNonce
 Nonce of last PING sent.
 
std::chrono::steady_clock::time_point m_lastPingTime
 Timestamp of last PING.
 
std::uint32_t m_latency
 Round-trip time in milliseconds.
 
std::chrono::milliseconds m_pingInterval
 Interval between pings (5000ms)
 
std::chrono::steady_clock::time_point m_lastServerResponse
 Timestamp of last server packet.
 
std::chrono::milliseconds m_connectionTimeout
 Timeout duration (15000ms)
 
ConnectionStats m_stats
 Connection statistics.
 
std::uint32_t m_currentLobbyId
 Current lobby ID, 0 if not in lobby.
 
std::function< void(const std::vector< rnp::LobbyInfo > &)> m_onLobbyListReceived
 Lobby list callback.
 
std::function< void(std::uint32_t, bool, rnp::ErrorCode)> m_onLobbyCreated
 Lobby created callback.
 
std::function< void(std::uint32_t, bool, rnp::ErrorCode, const rnp::LobbyInfo *)> m_onLobbyJoined
 Lobby joined callback.
 
std::function< void(const rnp::LobbyInfo &)> m_onLobbyUpdated
 Lobby updated callback.
 
std::function< void(std::uint32_t, std::uint32_t)> m_onGameStart
 Game start callback.
 
utl::EventBusm_eventBus
 Event bus reference.
 
std::uint32_t m_componentId = utl::NETWORK_CLIENT
 Component ID for event bus.
 

Detailed Description

High-performance UDP client implementation using ASIO library.

This class provides a complete multiplayer game client implementation with:

  • Asynchronous UDP networking
  • Server connection management
  • Lobby system integration
  • Packet routing and handling
  • Event bus integration
  • Thread-safe operations
  • Latency measurement

The client runs its network operations on a separate thread and communicates with the game engine via an event bus system.

Definition at line 84 of file AsioClient.hpp.

Constructor & Destructor Documentation

◆ AsioClient()

◆ ~AsioClient()

eng::AsioClient::~AsioClient ( )
override

Destructor.

Definition at line 46 of file asioClient.cpp.

References disconnect(), utl::INFO, and utl::Logger::log().

+ Here is the call graph for this function:

Member Function Documentation

◆ connect()

void eng::AsioClient::connect ( const std::string & host,
std::uint16_t port )
override

Connect to game server.

Parameters
hostServer hostname or IP address
portServer port number

Definition at line 52 of file asioClient.cpp.

References eng::CONNECTING, eng::DISCONNECTED, utl::INFO, utl::Logger::log(), m_connectionState, m_ioContext, m_lastServerResponse, m_networkThread, m_running, m_serverEndpoint, m_serverHost, m_serverPort, m_socket, networkThreadLoop(), sendConnect(), startReceive(), and utl::WARNING.

Referenced by processBusEvent(), and sendConnect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ createLobby()

void eng::AsioClient::createLobby ( rnp::PacketLobbyCreate lobbyCreate)

Create a new lobby on server.

Parameters
lobbyCreateLobby creation parameters (name, max players, game mode)

Sends LOBBY_CREATE packet. Response handled via callback.

Definition at line 813 of file asioClient.cpp.

References eng::CONNECTED, rnp::PacketLobbyCreate::gameMode, rnp::Serializer::getData(), rnp::Serializer::getSize(), utl::INFO, rnp::PacketHeader::length, rnp::LOBBY_CREATE, rnp::PacketLobbyCreate::lobbyName, utl::Logger::log(), m_connectionState, m_currentLobbyId, m_sessionId, rnp::PacketLobbyCreate::maxPlayers, rnp::PacketLobbyCreate::nameLen, sendToServer(), rnp::Serializer::serializeHeader(), rnp::Serializer::serializeLobbyCreate(), rnp::PacketHeader::sessionId, rnp::PacketHeader::type, and utl::WARNING.

Referenced by processBusEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ disconnect()

void eng::AsioClient::disconnect ( )
overridevirtual

Disconnect from server.

Sends DISCONNECT packet and closes connection

Implements eng::INetworkClient.

Definition at line 98 of file asioClient.cpp.

References eng::DISCONNECTED, eng::DISCONNECTING, utl::INFO, utl::Logger::log(), m_connectionState, m_ioContext, m_latency, m_networkThread, m_running, m_sendQueue, m_sendQueueMutex, m_serverTickRate, m_sessionId, m_socket, and sendDisconnect().

Referenced by processBusEvent(), sendDisconnect(), and ~AsioClient().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generatePingNonce()

std::uint32_t eng::AsioClient::generatePingNonce ( )
staticprivate

Generate random ping nonce.

Returns
New cryptographically random nonce

Definition at line 651 of file asioClient.cpp.

Referenced by sendPing().

+ Here is the caller graph for this function:

◆ getConnectionState()

ConnectionState eng::AsioClient::getConnectionState ( ) const
nodiscardoverridevirtual

Get current connection state.

Returns
Connection state enum

Implements eng::INetworkClient.

Definition at line 174 of file asioClient.cpp.

References m_connectionState.

◆ getCurrentLobbyId()

std::uint32_t eng::AsioClient::getCurrentLobbyId ( ) const
inline

Get current lobby ID.

Returns
Current lobby ID (0 if not in lobby)

Definition at line 218 of file AsioClient.hpp.

References m_currentLobbyId.

◆ getLatency()

std::uint32_t eng::AsioClient::getLatency ( ) const
nodiscardoverridevirtual

Get current latency to server.

Returns
Round-trip time in milliseconds

Implements eng::INetworkClient.

Definition at line 180 of file asioClient.cpp.

References m_latency.

◆ getName()

const std::string eng::AsioClient::getName ( ) const
inlinenodiscardoverridevirtual

Implements utl::IPlugin.

Definition at line 116 of file AsioClient.hpp.

◆ getServerTickRate()

std::uint16_t eng::AsioClient::getServerTickRate ( ) const
nodiscardoverridevirtual

Get server tick rate.

Returns
Server tick rate in Hz

Implements eng::INetworkClient.

Definition at line 178 of file asioClient.cpp.

References m_serverTickRate.

◆ getSessionId()

std::uint32_t eng::AsioClient::getSessionId ( ) const
nodiscardoverridevirtual

Get assigned session ID.

Returns
Session ID (0 if not connected)

Implements eng::INetworkClient.

Definition at line 176 of file asioClient.cpp.

References m_sessionId.

◆ getStats()

const ConnectionStats & eng::AsioClient::getStats ( ) const
inline

Get connection statistics.

Returns
Current connection stats

Definition at line 178 of file AsioClient.hpp.

References m_stats.

◆ getType()

utl::PluginType eng::AsioClient::getType ( ) const
inlinenodiscardoverridevirtual

Implements utl::IPlugin.

Definition at line 117 of file AsioClient.hpp.

References utl::NETWORK_CLIENT.

◆ handleConnectAccept()

rnp::HandlerResult eng::AsioClient::handleConnectAccept ( const rnp::PacketConnectAccept & packet,
const rnp::PacketContext & context )
private

Handle CONNECT_ACCEPT packet.

Parameters
packetConnect accept packet
contextPacket context
Returns
Handler result

Definition at line 376 of file asioClient.cpp.

References eng::CONNECTED, utl::CONNECTION_ACCEPTED, rnp::Serializer::getData(), utl::INFO, utl::Logger::log(), m_componentId, m_connectionState, m_eventBus, m_serverTickRate, m_sessionId, utl::EventBus::publish(), utl::RENDERING_ENGINE, rnp::PacketConnectAccept::sessionId, rnp::SUCCESS, and rnp::PacketConnectAccept::tickRateHz.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleDisconnect()

rnp::HandlerResult eng::AsioClient::handleDisconnect ( const rnp::PacketDisconnect & packet,
const rnp::PacketContext & context )
private

Handle DISCONNECT packet.

Parameters
packetDisconnect packet
contextPacket context
Returns
Handler result

Definition at line 393 of file asioClient.cpp.

References rnp::BANNED, rnp::CLIENT_REQUEST, eng::DISCONNECTED, utl::INFO, utl::Logger::log(), m_connectionState, m_sessionId, rnp::PROTOCOL_ERROR, rnp::PacketDisconnect::reasonCode, rnp::SERVER_FULL, rnp::SERVER_SHUTDOWN, rnp::SUCCESS, rnp::TIMEOUT, and rnp::UNSPECIFIED.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleEntityEvent()

rnp::HandlerResult eng::AsioClient::handleEntityEvent ( const std::vector< rnp::EventRecord > & events,
const rnp::PacketContext & context ) const
private

Handle ENTITY_EVENT packet.

Parameters
eventsVector of event records
contextPacket context
Returns
Handler result

Definition at line 777 of file asioClient.cpp.

References utl::ENTITY_EVENT_RECEIVED, utl::INFO, utl::Logger::log(), m_componentId, m_eventBus, utl::EventBus::publish(), and rnp::SUCCESS.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleError()

rnp::HandlerResult eng::AsioClient::handleError ( const rnp::PacketError & packet,
const rnp::PacketContext & context )
staticprivate

Handle ERROR packet.

Parameters
packetError packet
contextPacket context
Returns
Handler result

Definition at line 454 of file asioClient.cpp.

References rnp::PacketError::description, rnp::PacketError::errorCode, rnp::INTERNAL_ERROR, rnp::INVALID_PAYLOAD, utl::Logger::log(), rnp::RATE_LIMITED, rnp::SUCCESS, rnp::UNAUTHORIZED_SESSION, and utl::WARNING.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleGameStart()

rnp::HandlerResult eng::AsioClient::handleGameStart ( const rnp::PacketGameStart & packet,
const rnp::PacketContext & context ) const
private

Handle GAME_START packet.

Parameters
packetGame start packet
contextPacket context
Returns
Handler result

Definition at line 1046 of file asioClient.cpp.

References utl::Event::data, utl::GAME_START, utl::INFO, rnp::PacketGameStart::lobbyId, utl::Logger::log(), m_componentId, m_eventBus, m_onGameStart, utl::EventBus::publish(), rnp::PacketContext::sessionId, and rnp::SUCCESS.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleLobbyCreateResponse()

rnp::HandlerResult eng::AsioClient::handleLobbyCreateResponse ( const rnp::PacketLobbyCreateResponse & packet,
const rnp::PacketContext & context )
private

Handle LOBBY_CREATE_RESPONSE packet.

Parameters
packetLobby create response packet
contextPacket context
Returns
Handler result

Definition at line 983 of file asioClient.cpp.

References rnp::PacketLobbyCreateResponse::errorCode, utl::INFO, utl::LOBBY_CREATE_RESPONSE, rnp::PacketLobbyCreateResponse::lobbyId, utl::Logger::log(), m_componentId, m_currentLobbyId, m_eventBus, m_onLobbyCreated, utl::EventBus::publish(), rnp::SUCCESS, and rnp::PacketLobbyCreateResponse::success.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleLobbyJoinResponse()

rnp::HandlerResult eng::AsioClient::handleLobbyJoinResponse ( const rnp::PacketLobbyJoinResponse & packet,
const rnp::PacketContext & context )
private

Handle LOBBY_JOIN_RESPONSE packet.

Parameters
packetLobby join response packet
contextPacket context
Returns
Handler result

Definition at line 1005 of file asioClient.cpp.

References rnp::PacketLobbyJoinResponse::errorCode, utl::INFO, utl::LOBBY_JOIN_RESPONSE, rnp::PacketLobbyJoinResponse::lobbyId, rnp::PacketLobbyJoinResponse::lobbyInfo, utl::Logger::log(), m_componentId, m_currentLobbyId, m_eventBus, m_onLobbyJoined, utl::EventBus::publish(), rnp::SUCCESS, and rnp::PacketLobbyJoinResponse::success.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleLobbyListResponse()

rnp::HandlerResult eng::AsioClient::handleLobbyListResponse ( const rnp::PacketLobbyListResponse & packet,
const rnp::PacketContext & context ) const
private

Handle LOBBY_LIST_RESPONSE packet.

Parameters
packetLobby list response packet
contextPacket context
Returns
Handler result

Definition at line 968 of file asioClient.cpp.

References utl::INFO, rnp::PacketLobbyListResponse::lobbies, utl::LOBBY_LIST_RESPONSE, rnp::PacketLobbyListResponse::lobbyCount, utl::Logger::log(), m_componentId, m_eventBus, m_onLobbyListReceived, utl::EventBus::publish(), and rnp::SUCCESS.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleLobbyUpdate()

rnp::HandlerResult eng::AsioClient::handleLobbyUpdate ( const rnp::PacketLobbyUpdate & packet,
const rnp::PacketContext & context ) const
private

Handle LOBBY_UPDATE packet.

Parameters
packetLobby update packet
contextPacket context
Returns
Handler result

Definition at line 1029 of file asioClient.cpp.

References utl::INFO, utl::LOBBY_UPDATE, rnp::LobbyInfo::lobbyId, rnp::PacketLobbyUpdate::lobbyInfo, utl::Logger::log(), m_componentId, m_eventBus, m_onLobbyUpdated, utl::EventBus::publish(), and rnp::SUCCESS.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handlePing()

rnp::HandlerResult eng::AsioClient::handlePing ( const rnp::PacketPingPong & packet,
const rnp::PacketContext & context )
private

Handle PING packet.

Parameters
packetPing packet
contextPacket context
Returns
Handler result

Definition at line 447 of file asioClient.cpp.

References rnp::PacketPingPong::nonce, sendPong(), and rnp::SUCCESS.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handlePong()

rnp::HandlerResult eng::AsioClient::handlePong ( const rnp::PacketPingPong & packet,
const rnp::PacketContext & context )
private

Handle PONG packet.

Parameters
packetPong packet
contextPacket context
Returns
Handler result

Definition at line 432 of file asioClient.cpp.

References utl::INFO, utl::Logger::log(), m_lastPingNonce, m_lastPingTime, m_latency, rnp::PacketPingPong::nonce, and rnp::SUCCESS.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ handleReceive()

void eng::AsioClient::handleReceive ( std::size_t bytesReceived)
private

◆ handleWorldState()

rnp::HandlerResult eng::AsioClient::handleWorldState ( const rnp::PacketWorldState & packet,
const rnp::PacketContext & context ) const
private

Handle WORLD_STATE packet.

Parameters
packetWorld state packet
contextPacket context
Returns
Handler result

Definition at line 483 of file asioClient.cpp.

References rnp::PacketWorldState::entityCount, utl::INFO, utl::Logger::log(), m_componentId, m_eventBus, utl::EventBus::publish(), rnp::PacketWorldState::serverTick, rnp::SUCCESS, and utl::WORLD_STATE_RECEIVED.

Referenced by setupPacketHandlers().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isConnected()

bool eng::AsioClient::isConnected ( ) const
nodiscardoverridevirtual

Check if client is connected to server.

Returns
True if connected

Implements eng::INetworkClient.

Definition at line 172 of file asioClient.cpp.

References eng::CONNECTED, and m_connectionState.

◆ isInLobby()

bool eng::AsioClient::isInLobby ( ) const
inline

Check if player is in a lobby.

Returns
True if in lobby

Definition at line 224 of file AsioClient.hpp.

References m_currentLobbyId.

◆ joinLobby()

void eng::AsioClient::joinLobby ( std::uint32_t lobbyId)

Join an existing lobby.

Parameters
lobbyIdTarget lobby identifier

Sends LOBBY_JOIN packet. Response handled via callback.

Definition at line 852 of file asioClient.cpp.

References eng::CONNECTED, rnp::Serializer::getData(), utl::INFO, rnp::PacketHeader::length, rnp::LOBBY_JOIN, rnp::PacketLobbyJoin::lobbyId, utl::Logger::log(), m_connectionState, m_currentLobbyId, m_sessionId, sendToServer(), rnp::Serializer::serializeHeader(), rnp::Serializer::serializeLobbyJoin(), rnp::PacketHeader::sessionId, rnp::PacketHeader::type, and utl::WARNING.

Referenced by processBusEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ leaveLobby()

void eng::AsioClient::leaveLobby ( )

Leave current lobby.

Sends LOBBY_LEAVE packet and resets local lobby state

Definition at line 884 of file asioClient.cpp.

References eng::CONNECTED, rnp::Serializer::getData(), utl::INFO, rnp::PacketHeader::length, rnp::LOBBY_LEAVE, utl::Logger::log(), m_connectionState, m_currentLobbyId, m_sessionId, sendToServer(), rnp::Serializer::serializeHeader(), rnp::PacketHeader::sessionId, rnp::PacketHeader::type, and utl::WARNING.

Referenced by processBusEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ networkThreadLoop()

void eng::AsioClient::networkThreadLoop ( ) const
private

Main network thread loop.

Runs the ASIO io_context and handles network events

Definition at line 324 of file asioClient.cpp.

References utl::INFO, utl::Logger::log(), m_ioContext, m_running, and utl::WARNING.

Referenced by connect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ processBusEvent()

◆ processSendQueue()

void eng::AsioClient::processSendQueue ( )
private

Process outgoing packet queue.

Sends all queued packets via UDP socket

Definition at line 597 of file asioClient.cpp.

References eng::QueuedPacket::data, utl::INFO, utl::Logger::log(), m_sendQueue, m_sendQueueMutex, and sendPacketImmediate().

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ requestLobbyList()

void eng::AsioClient::requestLobbyList ( )

Request list of available lobbies from server.

Sends LOBBY_LIST_REQUEST packet

Definition at line 790 of file asioClient.cpp.

References eng::CONNECTED, rnp::Serializer::getData(), rnp::Serializer::getSize(), utl::INFO, rnp::PacketHeader::length, rnp::LOBBY_LIST_REQUEST, utl::Logger::log(), m_connectionState, m_sessionId, sendToServer(), rnp::Serializer::serializeHeader(), rnp::PacketHeader::sessionId, rnp::PacketHeader::type, and utl::WARNING.

Referenced by processBusEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ requestStartGame()

void eng::AsioClient::requestStartGame ( std::uint32_t lobbyId)

Request to start the game (host only)

Parameters
lobbyIdLobby ID to start

Sends START_GAME_REQUEST packet. Only valid for lobby host.

Definition at line 914 of file asioClient.cpp.

References eng::CONNECTED, rnp::Serializer::getData(), utl::INFO, rnp::PacketHeader::length, rnp::PacketStartGameRequest::lobbyId, utl::Logger::log(), m_connectionState, m_sessionId, sendToServer(), rnp::Serializer::serializeHeader(), rnp::Serializer::serializeStartGameRequest(), rnp::PacketHeader::sessionId, rnp::START_GAME_REQUEST, rnp::PacketHeader::type, and utl::WARNING.

Referenced by processBusEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendConnect()

void eng::AsioClient::sendConnect ( )
private

Send CONNECT packet to initiate connection.

Sends player name and client capabilities

Definition at line 497 of file asioClient.cpp.

References rnp::CONNECT, connect(), rnp::Serializer::getData(), utl::INFO, utl::Logger::log(), m_clientCaps, m_playerName, rnp::PacketConnect::nameLen, sendPacketImmediate(), rnp::Serializer::serializeConnect(), rnp::Serializer::serializeHeader(), and rnp::PacketHeader::type.

Referenced by connect(), and updateConnectionManagement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendDisconnect()

void eng::AsioClient::sendDisconnect ( )
private

Send DISCONNECT packet to terminate connection.

Includes disconnect reason code

Definition at line 522 of file asioClient.cpp.

References rnp::CLIENT_REQUEST, rnp::DISCONNECT, disconnect(), rnp::Serializer::getData(), utl::INFO, utl::Logger::log(), m_sessionId, rnp::PacketDisconnect::reasonCode, sendPacketImmediate(), rnp::Serializer::serializeDisconnect(), rnp::Serializer::serializeHeader(), and rnp::PacketHeader::type.

Referenced by disconnect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendPacketImmediate()

void eng::AsioClient::sendPacketImmediate ( const std::vector< std::uint8_t > & data)
private

Send packet immediately via UDP socket.

Parameters
dataSerialized packet data

Bypasses send queue for immediate transmission

Definition at line 350 of file asioClient.cpp.

References eng::ConnectionStats::bytesTransferred, utl::INFO, utl::Logger::log(), m_serverEndpoint, m_socket, m_stats, eng::ConnectionStats::packetsSent, and utl::WARNING.

Referenced by processSendQueue(), sendConnect(), sendDisconnect(), sendPing(), and sendPong().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendPing()

void eng::AsioClient::sendPing ( )
private

Send PING packet for latency measurement.

Generates and stores nonce for RTT calculation

Definition at line 545 of file asioClient.cpp.

References generatePingNonce(), rnp::Serializer::getData(), m_lastPingNonce, m_lastPingTime, m_sessionId, rnp::PacketPingPong::nonce, rnp::PING, sendPacketImmediate(), rnp::Serializer::serializeHeader(), rnp::Serializer::serializePingPong(), and rnp::PacketHeader::type.

Referenced by updateConnectionManagement().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendPong()

void eng::AsioClient::sendPong ( std::uint32_t nonce)
private

Send PONG response to server PING.

Parameters
noncePing nonce to echo back

Definition at line 572 of file asioClient.cpp.

References rnp::Serializer::getData(), m_sessionId, rnp::PacketPingPong::nonce, rnp::PONG, sendPacketImmediate(), rnp::Serializer::serializeHeader(), rnp::Serializer::serializePingPong(), and rnp::PacketHeader::type.

Referenced by handlePing().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ sendToServer()

void eng::AsioClient::sendToServer ( const std::vector< std::uint8_t > & data,
bool reliable = false )
overridevirtual

Send custom packet to server.

Parameters
dataSerialized packet data
reliableWhether packet requires reliable delivery (default: false)

Implements eng::INetworkClient.

Definition at line 194 of file asioClient.cpp.

References eng::CONNECTED, utl::Logger::log(), m_connectionState, m_sendQueue, m_sendQueueMutex, and utl::WARNING.

Referenced by createLobby(), joinLobby(), leaveLobby(), processBusEvent(), requestLobbyList(), and requestStartGame().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setClientCapabilities()

void eng::AsioClient::setClientCapabilities ( std::uint32_t caps)
overridevirtual

Set client capability flags.

Parameters
capsCapability flags (reserved)

Implements eng::INetworkClient.

Definition at line 188 of file asioClient.cpp.

References utl::INFO, utl::Logger::log(), and m_clientCaps.

+ Here is the call graph for this function:

◆ setOnGameStart()

void eng::AsioClient::setOnGameStart ( std::function< void(std::uint32_t, std::uint32_t)> callback)

Set callback for game start notification.

Parameters
callbackFunction(lobbyId, sessionId) called when GAME_START is received

Definition at line 963 of file asioClient.cpp.

References m_onGameStart.

◆ setOnLobbyCreated()

void eng::AsioClient::setOnLobbyCreated ( std::function< void(std::uint32_t, bool, rnp::ErrorCode)> callback)

Set callback for lobby creation response.

Parameters
callbackFunction(lobbyId, success, errorCode) called when LOBBY_CREATE_RESPONSE is received

Definition at line 947 of file asioClient.cpp.

References m_onLobbyCreated.

◆ setOnLobbyJoined()

void eng::AsioClient::setOnLobbyJoined ( std::function< void(std::uint32_t, bool, rnp::ErrorCode, const rnp::LobbyInfo *)> callback)

Set callback for lobby join response.

Parameters
callbackFunction(lobbyId, success, errorCode, lobbyInfo*) called when LOBBY_JOIN_RESPONSE is received

Definition at line 952 of file asioClient.cpp.

References m_onLobbyJoined.

◆ setOnLobbyListReceived()

void eng::AsioClient::setOnLobbyListReceived ( std::function< void(const std::vector< rnp::LobbyInfo > &)> callback)

Set callback for lobby list received.

Parameters
callbackFunction(vector<LobbyInfo>) called when LOBBY_LIST_RESPONSE is received

Definition at line 942 of file asioClient.cpp.

References m_onLobbyListReceived.

◆ setOnLobbyUpdated()

void eng::AsioClient::setOnLobbyUpdated ( std::function< void(const rnp::LobbyInfo &)> callback)

Set callback for lobby updates.

Parameters
callbackFunction(LobbyInfo) called when LOBBY_UPDATE is received (player join/leave, status change)

Definition at line 958 of file asioClient.cpp.

References m_onLobbyUpdated.

◆ setPlayerName()

void eng::AsioClient::setPlayerName ( const std::string & playerName)
overridevirtual

Set player name for connection.

Parameters
playerNameDisplay name

Implements eng::INetworkClient.

Definition at line 182 of file asioClient.cpp.

References utl::INFO, utl::Logger::log(), and m_playerName.

Referenced by processBusEvent().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setupPacketHandlers()

◆ startReceive()

void eng::AsioClient::startReceive ( )
private

Start asynchronous receive operation.

Initiates async_receive_from on the UDP socket

Definition at line 264 of file asioClient.cpp.

References m_recvBuffer, m_running, m_senderEndpoint, and m_socket.

Referenced by connect().

+ Here is the caller graph for this function:

◆ update()

void eng::AsioClient::update ( )
overridevirtual

Update client state (called each frame)

Processes send queue, checks timeouts, sends pings

Implements eng::INetworkClient.

Definition at line 152 of file asioClient.cpp.

References eng::CONNECTED, m_connectionState, processBusEvent(), processSendQueue(), and updateConnectionManagement().

+ Here is the call graph for this function:

◆ updateConnectionManagement()

void eng::AsioClient::updateConnectionManagement ( )
private

Update connection management state.

Checks for timeouts and sends periodic pings

Definition at line 614 of file asioClient.cpp.

References eng::CONNECTED, eng::CONNECTING, eng::DISCONNECTED, utl::Logger::log(), m_connectionState, m_connectionTimeout, m_lastPingTime, m_lastServerResponse, m_pingInterval, m_sessionId, sendConnect(), sendPing(), and utl::WARNING.

Referenced by update().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_clientCaps

std::uint32_t eng::AsioClient::m_clientCaps
private

Client capability flags.

Definition at line 275 of file AsioClient.hpp.

Referenced by sendConnect(), and setClientCapabilities().

◆ m_componentId

◆ m_connectionState

◆ m_connectionTimeout

std::chrono::milliseconds eng::AsioClient::m_connectionTimeout
private

Timeout duration (15000ms)

Definition at line 300 of file AsioClient.hpp.

Referenced by updateConnectionManagement().

◆ m_currentLobbyId

std::uint32_t eng::AsioClient::m_currentLobbyId
private

Current lobby ID, 0 if not in lobby.

Definition at line 306 of file AsioClient.hpp.

Referenced by createLobby(), getCurrentLobbyId(), handleLobbyCreateResponse(), handleLobbyJoinResponse(), isInLobby(), joinLobby(), and leaveLobby().

◆ m_eventBus

◆ m_ioContext

std::unique_ptr<asio::io_context> eng::AsioClient::m_ioContext
private

ASIO I/O context for async operations.

Definition at line 262 of file AsioClient.hpp.

Referenced by connect(), disconnect(), and networkThreadLoop().

◆ m_lastPingNonce

std::uint32_t eng::AsioClient::m_lastPingNonce
private

Nonce of last PING sent.

Definition at line 293 of file AsioClient.hpp.

Referenced by handlePong(), and sendPing().

◆ m_lastPingTime

std::chrono::steady_clock::time_point eng::AsioClient::m_lastPingTime
private

Timestamp of last PING.

Definition at line 294 of file AsioClient.hpp.

Referenced by handlePong(), sendPing(), and updateConnectionManagement().

◆ m_lastServerResponse

std::chrono::steady_clock::time_point eng::AsioClient::m_lastServerResponse
private

Timestamp of last server packet.

Definition at line 299 of file AsioClient.hpp.

Referenced by connect(), handleReceive(), and updateConnectionManagement().

◆ m_latency

std::uint32_t eng::AsioClient::m_latency
private

Round-trip time in milliseconds.

Definition at line 295 of file AsioClient.hpp.

Referenced by disconnect(), getLatency(), and handlePong().

◆ m_networkThread

std::unique_ptr<std::thread> eng::AsioClient::m_networkThread
private

Dedicated network thread.

Definition at line 264 of file AsioClient.hpp.

Referenced by connect(), and disconnect().

◆ m_onGameStart

std::function<void(std::uint32_t, std::uint32_t)> eng::AsioClient::m_onGameStart
private

Game start callback.

Definition at line 314 of file AsioClient.hpp.

Referenced by handleGameStart(), and setOnGameStart().

◆ m_onLobbyCreated

std::function<void(std::uint32_t, bool, rnp::ErrorCode)> eng::AsioClient::m_onLobbyCreated
private

Lobby created callback.

Definition at line 310 of file AsioClient.hpp.

Referenced by handleLobbyCreateResponse(), and setOnLobbyCreated().

◆ m_onLobbyJoined

std::function<void(std::uint32_t, bool, rnp::ErrorCode, const rnp::LobbyInfo *)> eng::AsioClient::m_onLobbyJoined
private

Lobby joined callback.

Definition at line 312 of file AsioClient.hpp.

Referenced by handleLobbyJoinResponse(), and setOnLobbyJoined().

◆ m_onLobbyListReceived

std::function<void(const std::vector<rnp::LobbyInfo> &)> eng::AsioClient::m_onLobbyListReceived
private

Lobby list callback.

Definition at line 309 of file AsioClient.hpp.

Referenced by handleLobbyListResponse(), and setOnLobbyListReceived().

◆ m_onLobbyUpdated

std::function<void(const rnp::LobbyInfo &)> eng::AsioClient::m_onLobbyUpdated
private

Lobby updated callback.

Definition at line 313 of file AsioClient.hpp.

Referenced by handleLobbyUpdate(), and setOnLobbyUpdated().

◆ m_packetHandler

std::unique_ptr<rnp::HandlerPacket> eng::AsioClient::m_packetHandler
private

RNP packet handler instance.

Definition at line 284 of file AsioClient.hpp.

Referenced by handleReceive(), and setupPacketHandlers().

◆ m_pingInterval

std::chrono::milliseconds eng::AsioClient::m_pingInterval
private

Interval between pings (5000ms)

Definition at line 296 of file AsioClient.hpp.

Referenced by updateConnectionManagement().

◆ m_playerName

std::string eng::AsioClient::m_playerName
private

Player display name.

Definition at line 278 of file AsioClient.hpp.

Referenced by sendConnect(), and setPlayerName().

◆ m_recvBuffer

std::array<std::uint8_t, 1024> eng::AsioClient::m_recvBuffer
private

Buffer for receiving UDP packets.

Definition at line 289 of file AsioClient.hpp.

Referenced by handleReceive(), and startReceive().

◆ m_running

std::atomic<bool> eng::AsioClient::m_running
private

Network thread running state (atomic)

Definition at line 281 of file AsioClient.hpp.

Referenced by connect(), disconnect(), networkThreadLoop(), and startReceive().

◆ m_senderEndpoint

asio::ip::udp::endpoint eng::AsioClient::m_senderEndpoint
private

Endpoint of last packet sender.

Definition at line 290 of file AsioClient.hpp.

Referenced by handleReceive(), and startReceive().

◆ m_sendQueue

std::queue<QueuedPacket> eng::AsioClient::m_sendQueue
private

Queue of packets waiting to be sent.

Definition at line 285 of file AsioClient.hpp.

Referenced by disconnect(), processSendQueue(), and sendToServer().

◆ m_sendQueueMutex

std::mutex eng::AsioClient::m_sendQueueMutex
private

Mutex for send queue access.

Definition at line 286 of file AsioClient.hpp.

Referenced by disconnect(), processSendQueue(), and sendToServer().

◆ m_serverEndpoint

asio::ip::udp::endpoint eng::AsioClient::m_serverEndpoint
private

Resolved server endpoint.

Definition at line 269 of file AsioClient.hpp.

Referenced by connect(), and sendPacketImmediate().

◆ m_serverHost

std::string eng::AsioClient::m_serverHost
private

Server hostname or IP address.

Definition at line 267 of file AsioClient.hpp.

Referenced by connect().

◆ m_serverPort

std::uint16_t eng::AsioClient::m_serverPort
private

Server port number.

Definition at line 268 of file AsioClient.hpp.

Referenced by connect().

◆ m_serverTickRate

std::uint16_t eng::AsioClient::m_serverTickRate
private

Server tick rate in Hz.

Definition at line 274 of file AsioClient.hpp.

Referenced by disconnect(), getServerTickRate(), and handleConnectAccept().

◆ m_sessionId

std::uint32_t eng::AsioClient::m_sessionId
private

◆ m_socket

std::unique_ptr<asio::ip::udp::socket> eng::AsioClient::m_socket
private

UDP socket for network communication.

Definition at line 263 of file AsioClient.hpp.

Referenced by connect(), disconnect(), sendPacketImmediate(), and startReceive().

◆ m_stats

ConnectionStats eng::AsioClient::m_stats
private

Connection statistics.

Definition at line 303 of file AsioClient.hpp.

Referenced by AsioClient(), getStats(), handleReceive(), and sendPacketImmediate().


The documentation for this class was generated from the following files: