17 uint32_t outgoingBandwidth) {
18 _host = enet_host_create(
nullptr, maxConnections, maxChannels, incomingBandwidth, outgoingBandwidth);
20 throw std::runtime_error(
"Failed to create ENet client host");
23 enet_host_compress_with_range_coder(
_host);
28 uint32_t incomingBandwidth, uint32_t outgoingBandwidth) {
31 throw std::invalid_argument(
"Address must be an ENetAddressWrapper");
34 const ENetAddress &nativeAddr = enetAddr->getNativeAddress();
35 _host = enet_host_create(&nativeAddr, maxConnections, maxChannels, incomingBandwidth, outgoingBandwidth);
38 uint16_t port = ENET_NET_TO_HOST_16(nativeAddr.port);
39 std::string errorMsg =
40 "Failed to create ENet server host on port " + std::to_string(port) +
". Possible causes:\n" +
41 " - Port already in use (another server instance running?)\n" +
42 " - Insufficient permissions (try a port > 1024)\n" +
" - Invalid network configuration";
43 throw std::runtime_error(errorMsg);
46 enet_host_compress_with_range_coder(
_host);
51 enet_host_destroy(
_host);
58 throw std::invalid_argument(
"Address must be an ENetAddressWrapper");
61 const ENetAddress &nativeAddr = enetAddr->getNativeAddress();
62 ENetPeer *peer = enet_host_connect(
_host, &nativeAddr, channelCount, data);
64 throw std::runtime_error(
"Failed to connect to host");
68 auto peerWrapper = std::make_unique<ENetPeerWrapper>(peer);
69 IPeer *result = peerWrapper.get();
70 _peers[peer] = std::move(peerWrapper);
77 int result = enet_host_service(
_host, &event, timeout);
84 case ENET_EVENT_TYPE_CONNECT:
88 auto peerWrapper = std::make_unique<ENetPeerWrapper>(event.peer);
89 netEvent.
peer = peerWrapper.get();
90 _peers[
event.peer] = std::move(peerWrapper);
96 enet_peer_timeout(event.peer, 5, 1000, 3000);
102 case ENET_EVENT_TYPE_DISCONNECT:
109 case ENET_EVENT_TYPE_RECEIVE:
114 netEvent.
packet = std::make_unique<ENetPacketWrapper>(event.packet);
134 throw std::invalid_argument(
"Packet must be an ENetPacketWrapper");
137 ENetPacket *nativePacket = enetPacket->getNativePacket();
138 enet_host_broadcast(
_host, channelID, nativePacket);
140 (void)packet.release();
145 enet_host_flush(
_host);
155 throw std::runtime_error(
"Host is null");
@ RECEIVE
A packet was received.
@ CONNECT
A peer has connected.
@ DISCONNECT
A peer has disconnected.
std::map< ENetPeer *, std::unique_ptr< ENetPeerWrapper > > _peers
void broadcast(std::unique_ptr< IPacket > packet, uint8_t channelID) override
Broadcast a packet to all connected peers.
IPeer * connect(const IAddress &address, size_t channelCount, uint32_t data) override
Connect to a remote host.
std::optional< HostNetworkEvent > service(uint32_t timeout) override
Service the host, processing network events.
const IAddress & getAddress() const override
Get the address this host is bound to.
~ENetHostWrapper() override
std::unique_ptr< ENetAddressWrapper > _cachedAddress
void flush() override
Send all queued packets immediately.
ENetHostWrapper(size_t maxConnections=1, size_t maxChannels=2, uint32_t incomingBandwidth=0, uint32_t outgoingBandwidth=0)
size_t getPeerCount() const override
Get the number of connected peers.
Interface representing a network address (IP + port).
Interface representing a remote peer in the network.
Represents a network event (connection, disconnection, or received data).
NetworkEventType type
Type of the event.
std::unique_ptr< IPacket > packet
Packet received (only for RECEIVE events).
IPeer * peer
Peer associated with the event.
uint8_t channelID
Channel on which the event occurred.