R-Type 0.1
Loading...
Searching...
No Matches
rtype::client::NetworkClient Class Reference

UDP client that performs the handshake, sends inputs and decodes server messages. More...

#include <NetworkClient.hpp>

Public Member Functions

 NetworkClient ()
 Creates a client with a closed socket.
 
 ~NetworkClient ()
 Stops the client and joins the network thread.
 
 NetworkClient (const NetworkClient &)=delete
 
NetworkClient & operator= (const NetworkClient &)=delete
 
bool connect (const std::string &host, const std::string &port)
 Resolves the server address, opens the socket and starts the network thread.
 
void sendHello ()
 Sends a Hello handshake if not connected yet.
 
void sendInput (std::uint32_t clientTick, rtype::protocol::InputAction actions)
 Sends an Input message with the next input sequence number.
 
void sendQueryEntity (std::uint32_t networkId)
 Asks the server to describe an unknown entity.
 
void send (const std::vector< std::uint8_t > &data)
 Sends raw bytes to the server.
 
bool pollEvent (NetworkEvent &event)
 Pops the oldest connection event or raw message.
 
bool pollReplicationEvent (ReplicationEvent &event)
 Pops the oldest replication message.
 
bool isConnected () const
 Tells whether the server accepted the handshake.
 
bool isRunning () const
 Tells whether the network thread is running.
 
std::uint32_t getConnectionId () const
 Returns the id given by the server.
 
void stop ()
 Closes the socket and joins the network thread.
 

Detailed Description

UDP client that performs the handshake, sends inputs and decodes server messages.

I/O runs on a dedicated thread, and received data is handed to the game loop through thread safe queues read with pollEvent() and pollReplicationEvent().

Member Function Documentation

◆ connect()

bool rtype::client::NetworkClient::connect ( const std::string & host,
const std::string & port )

Resolves the server address, opens the socket and starts the network thread.

Parameters
hostServer host name or IPv4 address.
portServer UDP port.
Returns
True on success, false if already running or resolution failed.

◆ sendInput()

void rtype::client::NetworkClient::sendInput ( std::uint32_t clientTick,
rtype::protocol::InputAction actions )

Sends an Input message with the next input sequence number.

Parameters
clientTickCurrent client frame counter.
actionsActions currently held.

◆ sendQueryEntity()

void rtype::client::NetworkClient::sendQueryEntity ( std::uint32_t networkId)

Asks the server to describe an unknown entity.

Parameters
networkIdNetwork id of the entity.

◆ send()

void rtype::client::NetworkClient::send ( const std::vector< std::uint8_t > & data)

Sends raw bytes to the server.

Parameters
dataDatagram, ignored if empty or larger than MAX_PAYLOAD_SIZE.

◆ pollEvent()

bool rtype::client::NetworkClient::pollEvent ( NetworkEvent & event)

Pops the oldest connection event or raw message.

Parameters
eventReceives the event.
Returns
True if an event was popped.

◆ pollReplicationEvent()

bool rtype::client::NetworkClient::pollReplicationEvent ( ReplicationEvent & event)

Pops the oldest replication message.

Parameters
eventReceives the event.
Returns
True if an event was popped.

◆ isConnected()

bool rtype::client::NetworkClient::isConnected ( ) const

Tells whether the server accepted the handshake.

Returns
True once Welcome was received.

◆ isRunning()

bool rtype::client::NetworkClient::isRunning ( ) const

Tells whether the network thread is running.

Returns
True between connect() and stop().

◆ getConnectionId()

std::uint32_t rtype::client::NetworkClient::getConnectionId ( ) const

Returns the id given by the server.

Returns
The connection id, or 0 if not connected.