Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
rtp::client::ClientNetwork Class Reference

#include <ClientNetwork.hpp>

Inheritance diagram for rtp::client::ClientNetwork:
Collaboration diagram for rtp::client::ClientNetwork:

Public Member Functions

 ClientNetwork (const std::string &serverIp, uint16_t serverPort)
 Constructor for Client Network.
 
 ~ClientNetwork () override
 Destructor for Client Network.
 
void start (void) override
 Start the network client.
 
void stop (void) override
 Stop the network client.
 
void sendPacket (const net::Packet &packet, net::NetworkMode mode)
 Send a packet to the server.
 
std::optional< net::NetworkEventpollEvent (void) override
 Poll for a network event.
 
void publishEvent (net::NetworkEvent event)
 Publish a network event.
 
std::vector< net::PackethasPendingPackets (void)
 Check if there are pending packets in the event queue.
 
net::Packet popPacket (void)
 Pop a packet from the event queue.
 
void readTcpHeader (void)
 Read TCP packet header asynchronously.
 
void readTcpBody (void)
 Read TCP packet body asynchronously.
 
void readUdp (void)
 Read UDP packets asynchronously.
 
void sendUdpHandshake (void)
 Send UDP handshake to the server.
 
bool isUdpReady (void) const
 Check if UDP socket is ready.
 
- Public Member Functions inherited from rtp::net::INetwork
virtual ~INetwork ()=default
 Virtual destructor.
 
- Public Member Functions inherited from rtp::net::IEventPublisher
virtual ~IEventPublisher ()=default
 Virtual default destructor.
 

Private Attributes

uint16_t _serverPort
 Server port number.
 
std::string _serverIp
 Server IP address.
 
asio::io_context _ioContext
 ASIO I/O context.
 
asio::ip::tcp::socket _tcpSocket
 TCP socket for communication.
 
asio::ip::udp::socket _udpSocket
 UDP socket for communication.
 
asio::ip::udp::endpoint _serverEndpoint
 Server UDP endpoint.
 
std::thread _ioThread
 Thread for running the I/O context.
 
std::mutex _eventQueueMutex
 Mutex for event queue synchronization.
 
std::deque< net::NetworkEvent_eventQueue
 Queue of network events.
 
net::Header _tcpHeader
 TCP packet header.
 
net::Header _udpHeader
 UDP packet header.
 
std::vector< uint8_t > _tcpBody
 TCP packet body.
 
uint32_t _sessionId = 0
 Client session ID.
 
bool _udpBound = false
 Flag indicating if UDP is bound.
 
std::array< char, 65536 > _udpBuffer
 Buffer for UDP packets.
 
asio::ip::udp::endpoint _udpSenderEndpoint
 Endpoint of the UDP sender.
 

Detailed Description

Definition at line 38 of file ClientNetwork.hpp.

Constructor & Destructor Documentation

◆ ClientNetwork()

rtp::client::ClientNetwork::ClientNetwork ( const std::string &  serverIp,
uint16_t  serverPort 
)

Constructor for Client Network.

Parameters
serverIpIP address of the server to connect to
serverPortPort number of the server to connect to
Note
Initializes ASIO components and prepares for connection

Definition at line 17 of file ClientNetwork.cpp.

◆ ~ClientNetwork()

rtp::client::ClientNetwork::~ClientNetwork ( )
override

Destructor for Client Network.

Note
Cleans up ASIO resources and stops the I/O context

Definition at line 30 of file ClientNetwork.cpp.

References stop().

Member Function Documentation

◆ hasPendingPackets()

std::vector< net::Packet > rtp::client::ClientNetwork::hasPendingPackets ( void  )

Check if there are pending packets in the event queue.

Returns
Vector of pending packets

Definition at line 122 of file ClientNetwork.cpp.

References _eventQueue, and _eventQueueMutex.

◆ isUdpReady()

bool rtp::client::ClientNetwork::isUdpReady ( void  ) const

Check if UDP socket is ready.

Returns
true if UDP socket is bound and ready, false otherwise

Definition at line 296 of file ClientNetwork.cpp.

Referenced by rtp::client::NetworkSyncSystem::isUdpReady(), and rtp::client::InputSystem::update().

◆ pollEvent()

std::optional< net::NetworkEvent > rtp::client::ClientNetwork::pollEvent ( void  )
overridevirtual

Poll for a network event.

Returns
Optional NetworkEvent if available

Implements rtp::net::INetwork.

Definition at line 103 of file ClientNetwork.cpp.

References _eventQueue, and _eventQueueMutex.

Referenced by rtp::client::NetworkSyncSystem::update().

◆ popPacket()

net::Packet rtp::client::ClientNetwork::popPacket ( void  )

Pop a packet from the event queue.

Returns
Packet popped from the queue

Definition at line 132 of file ClientNetwork.cpp.

References _eventQueue, and _eventQueueMutex.

◆ publishEvent()

void rtp::client::ClientNetwork::publishEvent ( net::NetworkEvent  event)
virtual

Publish a network event.

Parameters
eventNetwork event to be published

Implements rtp::net::IEventPublisher.

Definition at line 116 of file ClientNetwork.cpp.

References _eventQueue, and _eventQueueMutex.

Referenced by readTcpBody(), and readTcpHeader().

◆ readTcpBody()

void rtp::client::ClientNetwork::readTcpBody ( void  )

Read TCP packet body asynchronously.

Definition at line 192 of file ClientNetwork.cpp.

References _sessionId, _tcpBody, _tcpHeader, _tcpSocket, rtp::log::info(), rtp::net::Header::opCode, publishEvent(), readTcpHeader(), sendUdpHandshake(), and rtp::net::Welcome.

Referenced by readTcpHeader().

◆ readTcpHeader()

◆ readUdp()

◆ sendPacket()

◆ sendUdpHandshake()

void rtp::client::ClientNetwork::sendUdpHandshake ( void  )

Send UDP handshake to the server.

Definition at line 281 of file ClientNetwork.cpp.

References rtp::net::Packet::header, rtp::net::Hello, rtp::log::info(), rtp::net::Header::sessionId, and rtp::net::UDP.

Referenced by readTcpBody().

◆ start()

void rtp::client::ClientNetwork::start ( void  )
overridevirtual

Start the network client.

Note
Begins connecting to the server and starts the I/O context thread

Implements rtp::net::INetwork.

Definition at line 35 of file ClientNetwork.cpp.

References _ioContext, _ioThread, _serverEndpoint, _serverIp, _serverPort, _tcpSocket, _udpSocket, rtp::log::error(), rtp::net::Hello, rtp::log::info(), readTcpHeader(), readUdp(), sendPacket(), stop(), and rtp::net::TCP.

◆ stop()

void rtp::client::ClientNetwork::stop ( void  )
overridevirtual

Stop the network client.

Note
Closes connections and stops the I/O context

Implements rtp::net::INetwork.

Definition at line 68 of file ClientNetwork.cpp.

References _ioContext, _ioThread, _tcpSocket, _udpSocket, and rtp::log::info().

Referenced by ~ClientNetwork(), readTcpHeader(), and start().

Member Data Documentation

◆ _eventQueue

std::deque<net::NetworkEvent> rtp::client::ClientNetwork::_eventQueue
private

Queue of network events.

Definition at line 135 of file ClientNetwork.hpp.

Referenced by hasPendingPackets(), pollEvent(), popPacket(), and publishEvent().

◆ _eventQueueMutex

std::mutex rtp::client::ClientNetwork::_eventQueueMutex
private

Mutex for event queue synchronization.

Definition at line 134 of file ClientNetwork.hpp.

Referenced by hasPendingPackets(), pollEvent(), popPacket(), and publishEvent().

◆ _ioContext

asio::io_context rtp::client::ClientNetwork::_ioContext
private

ASIO I/O context.

Definition at line 127 of file ClientNetwork.hpp.

Referenced by sendPacket(), start(), and stop().

◆ _ioThread

std::thread rtp::client::ClientNetwork::_ioThread
private

Thread for running the I/O context.

Definition at line 132 of file ClientNetwork.hpp.

Referenced by start(), and stop().

◆ _serverEndpoint

asio::ip::udp::endpoint rtp::client::ClientNetwork::_serverEndpoint
private

Server UDP endpoint.

Definition at line 130 of file ClientNetwork.hpp.

Referenced by sendPacket(), and start().

◆ _serverIp

std::string rtp::client::ClientNetwork::_serverIp
private

Server IP address.

Definition at line 125 of file ClientNetwork.hpp.

Referenced by sendPacket(), and start().

◆ _serverPort

uint16_t rtp::client::ClientNetwork::_serverPort
private

Server port number.

Definition at line 124 of file ClientNetwork.hpp.

Referenced by sendPacket(), and start().

◆ _sessionId

uint32_t rtp::client::ClientNetwork::_sessionId = 0
private

Client session ID.

Definition at line 141 of file ClientNetwork.hpp.

Referenced by readTcpBody().

◆ _tcpBody

std::vector<uint8_t> rtp::client::ClientNetwork::_tcpBody
private

TCP packet body.

Definition at line 139 of file ClientNetwork.hpp.

Referenced by readTcpBody(), and readTcpHeader().

◆ _tcpHeader

net::Header rtp::client::ClientNetwork::_tcpHeader
private

TCP packet header.

Definition at line 137 of file ClientNetwork.hpp.

Referenced by readTcpBody(), and readTcpHeader().

◆ _tcpSocket

asio::ip::tcp::socket rtp::client::ClientNetwork::_tcpSocket
private

TCP socket for communication.

Definition at line 128 of file ClientNetwork.hpp.

Referenced by readTcpBody(), readTcpHeader(), sendPacket(), start(), and stop().

◆ _udpBound

bool rtp::client::ClientNetwork::_udpBound = false
private

Flag indicating if UDP is bound.

Definition at line 142 of file ClientNetwork.hpp.

◆ _udpBuffer

std::array<char, 65536> rtp::client::ClientNetwork::_udpBuffer
private

Buffer for UDP packets.

Definition at line 144 of file ClientNetwork.hpp.

◆ _udpHeader

net::Header rtp::client::ClientNetwork::_udpHeader
private

UDP packet header.

Definition at line 138 of file ClientNetwork.hpp.

◆ _udpSenderEndpoint

asio::ip::udp::endpoint rtp::client::ClientNetwork::_udpSenderEndpoint
private

Endpoint of the UDP sender.

Definition at line 145 of file ClientNetwork.hpp.

◆ _udpSocket

asio::ip::udp::socket rtp::client::ClientNetwork::_udpSocket
private

UDP socket for communication.

Definition at line 129 of file ClientNetwork.hpp.

Referenced by sendPacket(), start(), and stop().


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