|
Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
|
#include <ClientNetwork.hpp>
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::NetworkEvent > | pollEvent (void) override |
| Poll for a network event. | |
| void | publishEvent (net::NetworkEvent event) |
| Publish a network event. | |
| std::vector< net::Packet > | hasPendingPackets (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. | |
Definition at line 38 of file ClientNetwork.hpp.
| rtp::client::ClientNetwork::ClientNetwork | ( | const std::string & | serverIp, |
| uint16_t | serverPort | ||
| ) |
Constructor for Client Network.
| serverIp | IP address of the server to connect to |
| serverPort | Port number of the server to connect to |
Definition at line 17 of file ClientNetwork.cpp.
|
override |
Destructor for Client Network.
Definition at line 30 of file ClientNetwork.cpp.
References stop().
| std::vector< net::Packet > rtp::client::ClientNetwork::hasPendingPackets | ( | void | ) |
Check if there are pending packets in the event queue.
Definition at line 122 of file ClientNetwork.cpp.
References _eventQueue, and _eventQueueMutex.
| bool rtp::client::ClientNetwork::isUdpReady | ( | void | ) | const |
Check if UDP socket is ready.
Definition at line 296 of file ClientNetwork.cpp.
Referenced by rtp::client::NetworkSyncSystem::isUdpReady(), and rtp::client::InputSystem::update().
|
overridevirtual |
Poll for a network event.
Implements rtp::net::INetwork.
Definition at line 103 of file ClientNetwork.cpp.
References _eventQueue, and _eventQueueMutex.
Referenced by rtp::client::NetworkSyncSystem::update().
| net::Packet rtp::client::ClientNetwork::popPacket | ( | void | ) |
Pop a packet from the event queue.
Definition at line 132 of file ClientNetwork.cpp.
References _eventQueue, and _eventQueueMutex.
|
virtual |
Publish a network event.
| event | Network 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().
| 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().
| void rtp::client::ClientNetwork::readTcpHeader | ( | void | ) |
Read TCP packet header asynchronously.
Definition at line 143 of file ClientNetwork.cpp.
References _tcpBody, _tcpHeader, _tcpSocket, rtp::net::Header::ackId, rtp::net::Header::bodySize, rtp::log::error(), rtp::net::Packet::from_network(), rtp::net::Packet::header, rtp::log::info(), rtp::net::Header::magic, rtp::net::MAGIC_NUMBER, rtp::net::Header::opCode, publishEvent(), readTcpBody(), readTcpHeader(), rtp::net::Header::sequenceId, stop(), and rtp::net::Welcome.
Referenced by readTcpBody(), readTcpHeader(), and start().
| void rtp::client::ClientNetwork::readUdp | ( | void | ) |
Read UDP packets asynchronously.
Definition at line 227 of file ClientNetwork.cpp.
References rtp::net::Header::ackId, rtp::net::Packet::body, rtp::net::Header::bodySize, rtp::log::error(), rtp::net::Packet::from_network(), rtp::net::Packet::header, rtp::net::Header::magic, rtp::net::MAGIC_NUMBER, rtp::net::Header::sequenceId, and rtp::net::Header::sessionId.
Referenced by start().
| void rtp::client::ClientNetwork::sendPacket | ( | const net::Packet & | packet, |
| net::NetworkMode | mode | ||
| ) |
Send a packet to the server.
| packet | Packet to be sent |
| mode | Network mode (TCP or UDP) |
Definition at line 83 of file ClientNetwork.cpp.
References _ioContext, _serverEndpoint, _serverIp, _serverPort, _tcpSocket, _udpSocket, rtp::log::error(), rtp::net::Packet::getBufferSequence(), rtp::net::TCP, and rtp::net::UDP.
Referenced by rtp::client::NetworkSyncSystem::requestListRooms(), rtp::client::NetworkSyncSystem::sendSelectedWeapon(), start(), rtp::client::NetworkSyncSystem::tryCreateRoom(), rtp::client::NetworkSyncSystem::tryJoinRoom(), rtp::client::NetworkSyncSystem::tryLeaveRoom(), rtp::client::NetworkSyncSystem::tryLogin(), rtp::client::NetworkSyncSystem::tryRegister(), rtp::client::NetworkSyncSystem::trySendMessage(), rtp::client::NetworkSyncSystem::trySetReady(), rtp::client::NetworkSyncSystem::update(), and rtp::client::InputSystem::update().
| 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().
|
overridevirtual |
Start the network client.
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.
|
overridevirtual |
Stop the network client.
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().
|
private |
Queue of network events.
Definition at line 135 of file ClientNetwork.hpp.
Referenced by hasPendingPackets(), pollEvent(), popPacket(), and publishEvent().
|
private |
Mutex for event queue synchronization.
Definition at line 134 of file ClientNetwork.hpp.
Referenced by hasPendingPackets(), pollEvent(), popPacket(), and publishEvent().
|
private |
ASIO I/O context.
Definition at line 127 of file ClientNetwork.hpp.
Referenced by sendPacket(), start(), and stop().
|
private |
Thread for running the I/O context.
Definition at line 132 of file ClientNetwork.hpp.
|
private |
Server UDP endpoint.
Definition at line 130 of file ClientNetwork.hpp.
Referenced by sendPacket(), and start().
|
private |
Server IP address.
Definition at line 125 of file ClientNetwork.hpp.
Referenced by sendPacket(), and start().
|
private |
Server port number.
Definition at line 124 of file ClientNetwork.hpp.
Referenced by sendPacket(), and start().
|
private |
|
private |
TCP packet body.
Definition at line 139 of file ClientNetwork.hpp.
Referenced by readTcpBody(), and readTcpHeader().
|
private |
TCP packet header.
Definition at line 137 of file ClientNetwork.hpp.
Referenced by readTcpBody(), and readTcpHeader().
|
private |
TCP socket for communication.
Definition at line 128 of file ClientNetwork.hpp.
Referenced by readTcpBody(), readTcpHeader(), sendPacket(), start(), and stop().
|
private |
Flag indicating if UDP is bound.
Definition at line 142 of file ClientNetwork.hpp.
|
private |
Buffer for UDP packets.
Definition at line 144 of file ClientNetwork.hpp.
|
private |
UDP packet header.
Definition at line 138 of file ClientNetwork.hpp.
|
private |
Endpoint of the UDP sender.
Definition at line 145 of file ClientNetwork.hpp.
|
private |
UDP socket for communication.
Definition at line 129 of file ClientNetwork.hpp.
Referenced by sendPacket(), start(), and stop().