Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
ClientNetwork.hpp
Go to the documentation of this file.
1
8#ifndef RTYPE_NETWORK_NETWORK_HPP_
9 #define RTYPE_NETWORK_NETWORK_HPP_
10
14
15 #include <memory>
16 #include <optional>
17 #include <deque>
18 #include <mutex>
19 #include <asio.hpp>
20 #include <thread>
21 #include <array>
22
27namespace rtp::client {
28
39 public:
46 ClientNetwork(const std::string &serverIp, uint16_t serverPort);
47
52 ~ClientNetwork() override;
53
58 void start(void) override;
59
64 void stop(void) override;
65
71 void sendPacket(const net::Packet &packet, net::NetworkMode mode);
72
77 std::optional<net::NetworkEvent> pollEvent(void) override;
78
84
89 std::vector<net::Packet> hasPendingPackets(void);
90
96
100 void readTcpHeader(void);
101
105 void readTcpBody(void);
106
110 void readUdp(void);
111
115 void sendUdpHandshake(void);
116
121 bool isUdpReady(void) const;
122
123 private:
124 uint16_t _serverPort;
125 std::string _serverIp;
127 asio::io_context _ioContext;
128 asio::ip::tcp::socket _tcpSocket;
129 asio::ip::udp::socket _udpSocket;
130 asio::ip::udp::endpoint _serverEndpoint;
132 std::thread _ioThread;
134 std::mutex _eventQueueMutex;
135 std::deque<net::NetworkEvent> _eventQueue;
139 std::vector<uint8_t> _tcpBody;
141 uint32_t _sessionId = 0;
142 bool _udpBound = false;
144 std::array<char, 65536> _udpBuffer;
145 asio::ip::udp::endpoint _udpSenderEndpoint;
147 };
148}; // namespace rtp::client
149
150#endif /* !RTYPE_NETWORK_NETWORK_HPP_ */
Network packet implementation for R-Type protocol.
std::vector< net::Packet > hasPendingPackets(void)
Check if there are pending packets in the event queue.
void readUdp(void)
Read UDP packets asynchronously.
asio::ip::tcp::socket _tcpSocket
TCP socket for communication.
void sendPacket(const net::Packet &packet, net::NetworkMode mode)
Send a packet to the server.
net::Header _udpHeader
UDP packet header.
std::string _serverIp
Server IP address.
bool _udpBound
Flag indicating if UDP is bound.
std::array< char, 65536 > _udpBuffer
Buffer for UDP packets.
void sendUdpHandshake(void)
Send UDP handshake to the server.
~ClientNetwork() override
Destructor for Client Network.
std::mutex _eventQueueMutex
Mutex for event queue synchronization.
net::Packet popPacket(void)
Pop a packet from the event queue.
bool isUdpReady(void) const
Check if UDP socket is ready.
std::deque< net::NetworkEvent > _eventQueue
Queue of network events.
uint32_t _sessionId
Client session ID.
void stop(void) override
Stop the network client.
net::Header _tcpHeader
TCP packet header.
std::thread _ioThread
Thread for running the I/O context.
uint16_t _serverPort
Server port number.
std::vector< uint8_t > _tcpBody
TCP packet body.
asio::ip::udp::endpoint _serverEndpoint
Server UDP endpoint.
void readTcpBody(void)
Read TCP packet body asynchronously.
void start(void) override
Start the network client.
asio::io_context _ioContext
ASIO I/O context.
asio::ip::udp::socket _udpSocket
UDP socket for communication.
void readTcpHeader(void)
Read TCP packet header asynchronously.
void publishEvent(net::NetworkEvent event)
Publish a network event.
std::optional< net::NetworkEvent > pollEvent(void) override
Poll for a network event.
asio::ip::udp::endpoint _udpSenderEndpoint
Endpoint of the UDP sender.
Interface for network event handling.
Interface for network operations.
Definition INetwork.hpp:46
Network packet with header and serializable body.
Definition Packet.hpp:471
R-Type client namespace.
NetworkMode
Enum representing network transmission modes.
Definition INetwork.hpp:25
Packet header with sequencing and acknowledgment support.
Definition Packet.hpp:130
Represents a network event containing session ID and packet data.
Definition INetwork.hpp:34