R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ENetHost.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by IamSwan on 06/12/2025.
4** File description:
5** ENetHost.hpp
6*/
7
8#pragma once
9
10#include <enet/enet.h>
11#include <map>
12#include <memory>
13#include "ENetAddress.hpp"
14#include "IHost.hpp"
15
16class ENetPeerWrapper;
17
18class ENetHostWrapper final : public IHost {
19 public:
20 // Client constructor
21 explicit ENetHostWrapper(size_t maxConnections = 1, size_t maxChannels = 2,
22 uint32_t incomingBandwidth = 0, uint32_t outgoingBandwidth = 0);
23
24 // Server constructor
25 ENetHostWrapper(const IAddress &address, size_t maxConnections, size_t maxChannels = 2,
26 uint32_t incomingBandwidth = 0, uint32_t outgoingBandwidth = 0);
27
28 ~ENetHostWrapper() override;
29
32
33 IPeer *connect(const IAddress &address, size_t channelCount, uint32_t data) override;
34 std::optional<HostNetworkEvent> service(uint32_t timeout) override;
35 void broadcast(std::unique_ptr<IPacket> packet, uint8_t channelID) override;
36 void flush() override;
37
38 [[nodiscard]] size_t getPeerCount() const override;
39 [[nodiscard]] const IAddress &getAddress() const override;
40
41 private:
42 ENetHost *_host;
43 std::map<ENetPeer *, std::unique_ptr<ENetPeerWrapper>> _peers;
44 mutable std::unique_ptr<ENetAddressWrapper> _cachedAddress;
45};
std::map< ENetPeer *, std::unique_ptr< ENetPeerWrapper > > _peers
Definition ENetHost.hpp:43
void broadcast(std::unique_ptr< IPacket > packet, uint8_t channelID) override
Broadcast a packet to all connected peers.
Definition ENetHost.cpp:127
IPeer * connect(const IAddress &address, size_t channelCount, uint32_t data) override
Connect to a remote host.
Definition ENetHost.cpp:55
std::optional< HostNetworkEvent > service(uint32_t timeout) override
Service the host, processing network events.
Definition ENetHost.cpp:75
const IAddress & getAddress() const override
Get the address this host is bound to.
Definition ENetHost.cpp:153
ENetHostWrapper & operator=(const ENetHostWrapper &)=delete
~ENetHostWrapper() override
Definition ENetHost.cpp:49
std::unique_ptr< ENetAddressWrapper > _cachedAddress
Definition ENetHost.hpp:44
ENetHostWrapper(const ENetHostWrapper &)=delete
void flush() override
Send all queued packets immediately.
Definition ENetHost.cpp:143
ENetHost * _host
Definition ENetHost.hpp:42
size_t getPeerCount() const override
Get the number of connected peers.
Definition ENetHost.cpp:149
Interface representing a network address (IP + port).
Definition IAddress.hpp:21
Interface representing a network host (server or client endpoint).
Definition IHost.hpp:49
Interface representing a remote peer in the network.
Definition IPeer.hpp:40