R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
IHost.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** IHost.hpp
6*/
7
8#pragma once
9
10#include <cstdint>
11#include <memory>
12#include <optional>
13#include "IAddress.hpp"
14#include "IPacket.hpp"
15#include "IPeer.hpp"
16
21enum class NetworkEventType {
22 NONE,
23 CONNECT,
25 RECEIVE
26};
27
34 IPeer *peer = nullptr;
35 std::unique_ptr<IPacket> packet;
36 uint8_t channelID = 0;
37 uint32_t data = 0;
38};
39
49class IHost {
50 public:
51 virtual ~IHost() = default;
52
61 virtual IPeer *connect(const IAddress &address, size_t channelCount = 1, uint32_t data = 0) = 0;
62
69 virtual std::optional<HostNetworkEvent> service(uint32_t timeout = 0) = 0;
70
77 virtual void broadcast(std::unique_ptr<IPacket> packet, uint8_t channelID = 0) = 0;
78
82 virtual void flush() = 0;
83
88 [[nodiscard]] virtual size_t getPeerCount() const = 0;
89
94 [[nodiscard]] virtual const IAddress &getAddress() const = 0;
95};
NetworkEventType
Type of network event.
Definition IHost.hpp:21
@ RECEIVE
A packet was received.
@ NONE
No event occurred.
@ CONNECT
A peer has connected.
@ DISCONNECT
A peer has disconnected.
Interface representing a network address (IP + port).
Definition IAddress.hpp:21
Interface representing a network host (server or client endpoint).
Definition IHost.hpp:49
virtual std::optional< HostNetworkEvent > service(uint32_t timeout=0)=0
Service the host, processing network events.
virtual size_t getPeerCount() const =0
Get the number of connected peers.
virtual ~IHost()=default
virtual const IAddress & getAddress() const =0
Get the address this host is bound to.
virtual void flush()=0
Send all queued packets immediately.
virtual IPeer * connect(const IAddress &address, size_t channelCount=1, uint32_t data=0)=0
Connect to a remote host.
virtual void broadcast(std::unique_ptr< IPacket > packet, uint8_t channelID=0)=0
Broadcast a packet to all connected peers.
Interface representing a remote peer in the network.
Definition IPeer.hpp:40
Represents a network event (connection, disconnection, or received data).
Definition IHost.hpp:32
NetworkEventType type
Type of the event.
Definition IHost.hpp:33
uint32_t data
Additional data (e.g., disconnect reason).
Definition IHost.hpp:37
std::unique_ptr< IPacket > packet
Packet received (only for RECEIVE events).
Definition IHost.hpp:35
IPeer * peer
Peer associated with the event.
Definition IHost.hpp:34
uint8_t channelID
Channel on which the event occurred.
Definition IHost.hpp:36