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

#include <Session.hpp>

Inheritance diagram for rtp::net::Session:
Collaboration diagram for rtp::net::Session:

Public Member Functions

 Session (uint32_t id, asio::ip::tcp::socket socket, asio::ip::udp::socket &serverUdpSocket, IEventPublisher &publisher)
 Constructor for Session.
 
 ~Session ()
 Destructor for Session.
 
void start (void)
 Start the session's read and write operations.
 
void stop (void)
 Stop the session's operations.
 
void send (const Packet &packet, NetworkMode mode)
 Send a packet to the client.
 
void setId (uint32_t id)
 Set the unique identifier for the session.
 
uint32_t getId () const
 Get the unique identifier for the session.
 
asio::ip::tcp::endpoint getTcpEndpoint () const
 Set the TCP endpoint for the session.
 
void setUdpEndpoint (const asio::ip::udp::endpoint &endpoint)
 Set the UDP endpoint for the session.
 
asio::ip::udp::endpoint getUdpEndpoint () const
 Get the UDP endpoint for the session.
 

Private Member Functions

asio::awaitable< void > reader (void)
 Asynchronous reader coroutine for the session.
 
asio::awaitable< void > writer (void)
 Asynchronous writer coroutine for the session.
 

Private Attributes

uint32_t _id
 Unique identifier for the session.
 
bool _stopped = false
 Flag indicating if the session is stopped.
 
asio::ip::tcp::socket _socket
 TCP socket associated with the session.
 
asio::ip::udp::socket & _serverUdpSocket
 Reference to the server's UDP socket.
 
IEventPublisher_publisher
 Reference to the event publisher for network events.
 
asio::ip::udp::endpoint _udpEndpoint {}
 UDP endpoint associated with the session.
 
bool _hasUdp = false
 Flag indicating if UDP endpoint is set.
 
std::mutex _writeMutex
 Mutex for synchronizing write operations.
 
asio::steady_timer _timer
 Timer for managing write operations.
 
std::deque< Packet_writeQueue {}
 Queue of packets to be written to the TCP socket.
 

Detailed Description

Definition at line 22 of file Session.hpp.

Constructor & Destructor Documentation

◆ Session()

rtp::net::Session::Session ( uint32_t  id,
asio::ip::tcp::socket  socket,
asio::ip::udp::socket &  serverUdpSocket,
IEventPublisher publisher 
)

Constructor for Session.

Parameters
idUnique identifier for the session
socketTCP socket associated with the session
serverUdpSocketReference to the server's UDP socket
publisherReference to the event publisher for network events

Definition at line 17 of file Session.cpp.

◆ ~Session()

rtp::net::Session::~Session ( )

Destructor for Session.

Definition at line 29 of file Session.cpp.

References stop().

Member Function Documentation

◆ getId()

uint32_t rtp::net::Session::getId ( ) const

Get the unique identifier for the session.

Returns
Current identifier of the session

Definition at line 69 of file Session.cpp.

References _id.

◆ getTcpEndpoint()

asio::ip::tcp::endpoint rtp::net::Session::getTcpEndpoint ( ) const

Set the TCP endpoint for the session.

Parameters
endpointNew TCP endpoint for the session

Definition at line 73 of file Session.cpp.

References _socket.

◆ getUdpEndpoint()

asio::ip::udp::endpoint rtp::net::Session::getUdpEndpoint ( ) const
inline

Get the UDP endpoint for the session.

Returns
Current UDP endpoint of the session

Definition at line 88 of file Session.hpp.

References _udpEndpoint.

◆ reader()

asio::awaitable< void > rtp::net::Session::reader ( void  )
private

◆ send()

void rtp::net::Session::send ( const Packet packet,
NetworkMode  mode 
)

Send a packet to the client.

Parameters
packetPacket to be sent
modeNetwork mode (TCP or UDP) for sending the packet

Definition at line 51 of file Session.cpp.

References _hasUdp, _serverUdpSocket, _timer, _udpEndpoint, _writeMutex, _writeQueue, rtp::net::TCP, and rtp::net::UDP.

◆ setId()

void rtp::net::Session::setId ( uint32_t  id)

Set the unique identifier for the session.

Parameters
idNew identifier for the session

Definition at line 65 of file Session.cpp.

References _id.

◆ setUdpEndpoint()

void rtp::net::Session::setUdpEndpoint ( const asio::ip::udp::endpoint &  endpoint)

Set the UDP endpoint for the session.

Parameters
endpointNew UDP endpoint for the session

Definition at line 77 of file Session.cpp.

References _hasUdp, and _udpEndpoint.

◆ start()

void rtp::net::Session::start ( void  )

Start the session's read and write operations.

Note
Initiates asynchronous reading from the TCP socket

Definition at line 33 of file Session.cpp.

References _socket.

◆ stop()

void rtp::net::Session::stop ( void  )

Stop the session's operations.

Note
Closes the TCP socket and stops any ongoing operations

Definition at line 43 of file Session.cpp.

References _socket, _stopped, and _timer.

Referenced by ~Session(), reader(), and writer().

◆ writer()

asio::awaitable< void > rtp::net::Session::writer ( void  )
private

Asynchronous writer coroutine for the session.

Note
Continuously writes packets to the TCP socket

Definition at line 116 of file Session.cpp.

References _socket, _stopped, _timer, _writeMutex, _writeQueue, rtp::net::Packet::getBufferSequence(), and stop().

Member Data Documentation

◆ _hasUdp

bool rtp::net::Session::_hasUdp = false
private

Flag indicating if UDP endpoint is set.

Definition at line 112 of file Session.hpp.

Referenced by send(), and setUdpEndpoint().

◆ _id

uint32_t rtp::net::Session::_id
private

Unique identifier for the session.

Definition at line 104 of file Session.hpp.

Referenced by getId(), reader(), and setId().

◆ _publisher

IEventPublisher& rtp::net::Session::_publisher
private

Reference to the event publisher for network events.

Definition at line 109 of file Session.hpp.

Referenced by reader().

◆ _serverUdpSocket

asio::ip::udp::socket& rtp::net::Session::_serverUdpSocket
private

Reference to the server's UDP socket.

Definition at line 108 of file Session.hpp.

Referenced by send().

◆ _socket

asio::ip::tcp::socket rtp::net::Session::_socket
private

TCP socket associated with the session.

Definition at line 107 of file Session.hpp.

Referenced by getTcpEndpoint(), reader(), start(), stop(), and writer().

◆ _stopped

bool rtp::net::Session::_stopped = false
private

Flag indicating if the session is stopped.

Definition at line 105 of file Session.hpp.

Referenced by reader(), stop(), and writer().

◆ _timer

asio::steady_timer rtp::net::Session::_timer
private

Timer for managing write operations.

Definition at line 115 of file Session.hpp.

Referenced by send(), stop(), and writer().

◆ _udpEndpoint

asio::ip::udp::endpoint rtp::net::Session::_udpEndpoint {}
private

UDP endpoint associated with the session.

Definition at line 111 of file Session.hpp.

Referenced by getUdpEndpoint(), send(), and setUdpEndpoint().

◆ _writeMutex

std::mutex rtp::net::Session::_writeMutex
private

Mutex for synchronizing write operations.

Definition at line 114 of file Session.hpp.

Referenced by send(), and writer().

◆ _writeQueue

std::deque<Packet> rtp::net::Session::_writeQueue {}
private

Queue of packets to be written to the TCP socket.

Definition at line 116 of file Session.hpp.

Referenced by send(), and writer().


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