18 asio::ip::tcp::socket socket,
19 asio::ip::udp::socket& serverUdpSocket,
22 _socket(
std::move(socket)),
23 _serverUdpSocket(serverUdpSocket),
24 _publisher(publisher),
25 _timer(_socket.get_executor())
34 asio::co_spawn(
_socket.get_executor(),
35 [self = shared_from_this()] {
return self->reader(); },
38 asio::co_spawn(
_socket.get_executor(),
39 [self = shared_from_this()] {
return self->writer(); },
58 auto pkt = std::make_shared<Packet>(packet);
59 auto buffers = pkt->getBufferSequence();
61 [](
const asio::error_code&, std::size_t){});
74 return _socket.remote_endpoint();
90 co_await asio::async_read(
_socket, asio::buffer(&header,
sizeof(
Header)), asio::use_awaitable);
105 co_await asio::async_read(
_socket, asio::buffer(packet.
body), asio::use_awaitable);
110 }
catch (std::exception&) {
121 _timer.expires_at(std::chrono::steady_clock::time_point::max());
122 co_await _timer.async_wait(redirect_error(asio::use_awaitable, ec));
133 }
catch (std::exception&) {
Logger declaration with support for multiple log levels.
Interface for network event handling.
virtual void publishEvent(NetworkEvent event)=0
Get buffer sequence for network transmission.
Network packet with header and serializable body.
BufferSequence getBufferSequence(void) const
Get buffer sequence for network transmission.
std::vector< uint8_t > body
Packet body/payload.
Header header
Packet header.
static T from_network(T value)
Converts a primitive type (integer, float) from Big-Endian (network) to machine endianness.
asio::ip::udp::socket & _serverUdpSocket
Reference to the server's UDP socket.
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.
uint32_t getId() const
Get the unique identifier for the session.
bool _stopped
Flag indicating if the session is stopped.
std::mutex _writeMutex
Mutex for synchronizing write operations.
Session(uint32_t id, asio::ip::tcp::socket socket, asio::ip::udp::socket &serverUdpSocket, IEventPublisher &publisher)
Constructor for Session.
asio::steady_timer _timer
Timer for managing write operations.
bool _hasUdp
Flag indicating if UDP endpoint is set.
asio::ip::tcp::socket _socket
TCP socket associated with the session.
void setId(uint32_t id)
Set the unique identifier for the session.
void send(const Packet &packet, NetworkMode mode)
Send a packet to the client.
void stop(void)
Stop the session's operations.
asio::awaitable< void > writer(void)
Asynchronous writer coroutine for the session.
std::deque< Packet > _writeQueue
Queue of packets to be written to the TCP socket.
uint32_t _id
Unique identifier for the session.
~Session()
Destructor for Session.
IEventPublisher & _publisher
Reference to the event publisher for network events.
asio::ip::udp::endpoint _udpEndpoint
UDP endpoint associated with the session.
void start(void)
Start the session's read and write operations.
asio::awaitable< void > reader(void)
Asynchronous reader coroutine for the session.
File : IEventPublisher.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
constexpr uint16_t MAGIC_NUMBER
Magic number for packet validation.
@ Disconnect
Disconnect notification.
NetworkMode
Enum representing network transmission modes.