Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
ServerNetwork.hpp
Go to the documentation of this file.
1
8#ifndef RTYPE_SERVER_NETWORK_HPP_
9 #define RTYPE_SERVER_NETWORK_HPP_
10
15 #include "RType/Logger.hpp"
16
17 #include <asio.hpp>
18 #include <memory>
19 #include <unordered_map>
20 #include <thread>
21 #include <mutex>
22 #include <queue>
23 #include <optional>
24
29namespace rtp::server {
30
36 public:
41 ServerNetwork(uint16_t port);
42
46 ~ServerNetwork() override;
47
52 void start(void) override;
53
58 void stop(void) override;
59
66 void sendPacket(uint32_t sessionId, const net::Packet &packet, net::NetworkMode mode);
67
73 void broadcastPacket(const net::Packet &packet, net::NetworkMode mode);
74
79 std::optional<net::NetworkEvent> pollEvent(void) override;
80
85 void publishEvent(net::NetworkEvent event) override;
86
87 private:
92 void acceptConnection();
93
98 void receiveUdpPacket();
99
100 private:
101 asio::io_context _ioContext;
102 asio::ip::tcp::acceptor _acceptor;
103 asio::ip::udp::socket _udpSocket;
104 std::thread _ioThread;
106 std::unordered_map<uint32_t,
107 std::shared_ptr<net::Session>> _sessions;
108 std::mutex _sessionsMutex;
110 std::queue<net::NetworkEvent> _eventQueue;
111 std::mutex _eventQueueMutex;
113 uint32_t _nextSessionId;
115 std::unordered_map<asio::ip::udp::endpoint,
117 std::mutex _udpMapMutex;
119 std::array<char, 4096> _udpBuffer;
120 asio::ip::udp::endpoint _udpRemoteEndpoint;
121 };
122}
123
124#endif /* !RTYPE_SERVER_NETWORK_HPP_ */
Logger declaration with support for multiple log levels.
Network packet implementation for R-Type protocol.
Interface for network event handling.
Interface for network operations.
Definition INetwork.hpp:46
Network packet with header and serializable body.
Definition Packet.hpp:471
Implementation ASIO du serveur réseau (TCP + UDP)
void start(void) override
Start the network server.
void sendPacket(uint32_t sessionId, const net::Packet &packet, net::NetworkMode mode)
Send a packet to a specific session.
std::optional< net::NetworkEvent > pollEvent(void) override
Poll for a network event.
std::mutex _sessionsMutex
Mutex for protecting access to the sessions map.
uint32_t _nextSessionId
Next session ID to assign to a new connection.
std::unordered_map< asio::ip::udp::endpoint, uint32_t > _udpEndpointToSessionId
Map of UDP endpoints to session IDs.
std::mutex _udpMapMutex
Mutex for protecting access to the UDP endpoint map.
void broadcastPacket(const net::Packet &packet, net::NetworkMode mode)
Broadcast a packet to all connected sessions.
asio::ip::tcp::acceptor _acceptor
TCP acceptor for incoming connections.
std::queue< net::NetworkEvent > _eventQueue
Queue of network events to be processed.
std::unordered_map< uint32_t, std::shared_ptr< net::Session > > _sessions
Map of active sessions indexed by session ID.
void publishEvent(net::NetworkEvent event) override
Publish a network event to be processed.
std::array< char, 4096 > _udpBuffer
Buffer for receiving UDP packets.
std::mutex _eventQueueMutex
Mutex for protecting access to the event queue.
asio::ip::udp::endpoint _udpRemoteEndpoint
Remote endpoint for the last received UDP packet.
~ServerNetwork() override
Destructor for ServerNetwork.
asio::ip::udp::socket _udpSocket
UDP socket for receiving and sending datagrams.
void acceptConnection()
Accept incoming TCP connections.
std::thread _ioThread
Thread running the ASIO I/O context.
void receiveUdpPacket()
Receive incoming UDP packets.
asio::io_context _ioContext
ASIO I/O context for managing asynchronous operations.
void stop(void) override
Stop the network server.
NetworkMode
Enum representing network transmission modes.
Definition INetwork.hpp:25
File : GameManager.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
Represents a network event containing session ID and packet data.
Definition INetwork.hpp:34