R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ENetPeer.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by IamSwan on 06/12/2025.
4** File description:
5** ENetPeer.cpp
6*/
7
8#include "ENetPeer.hpp"
9#include <stdexcept>
10#include "ENetAddress.hpp"
11#include "ENetPacket.hpp"
12
13ENetPeerWrapper::ENetPeerWrapper(ENetPeer *peer) : _peer(peer) {
14 if (!_peer) {
15 throw std::invalid_argument("ENetPeer cannot be null");
16 }
17}
18
19bool ENetPeerWrapper::send(std::unique_ptr<IPacket> packet, uint8_t channelID) {
20 if (!_peer || !packet) {
21 return false;
22 }
23
24 // Cast to ENetPacketWrapper to get native packet
25 auto *enetPacket = dynamic_cast<ENetPacketWrapper *>(packet.get());
26 if (!enetPacket) {
27 throw std::invalid_argument("Packet must be an ENetPacketWrapper");
28 }
29
30 ENetPacket *nativePacket = enetPacket->getNativePacket();
31 if (enet_peer_send(_peer, channelID, nativePacket) < 0) {
32 return false;
33 }
34
35 // Release ownership since ENet now owns the packet
36 (void)packet.release();
37 return true;
38}
39
40void ENetPeerWrapper::disconnect(uint32_t data) {
41 if (_peer) {
42 enet_peer_disconnect(_peer, data);
43 }
44}
45
46void ENetPeerWrapper::disconnectNow(uint32_t data) {
47 if (_peer) {
48 enet_peer_disconnect_now(_peer, data);
49 }
50}
51
53 if (_peer && _peer->state != ENET_PEER_STATE_DISCONNECTED &&
54 _peer->state != ENET_PEER_STATE_DISCONNECTING && _peer->state != ENET_PEER_STATE_ZOMBIE) {
55 enet_peer_disconnect_later(_peer, data);
56 }
57}
58
60 return _peer;
61}
62
64 if (!_peer) {
66 }
67
68 switch (_peer->state) {
69 case ENET_PEER_STATE_DISCONNECTED:
71 case ENET_PEER_STATE_CONNECTING:
73 case ENET_PEER_STATE_ACKNOWLEDGING_CONNECT:
75 case ENET_PEER_STATE_CONNECTION_PENDING:
77 case ENET_PEER_STATE_CONNECTION_SUCCEEDED:
79 case ENET_PEER_STATE_CONNECTED:
81 case ENET_PEER_STATE_DISCONNECT_LATER:
83 case ENET_PEER_STATE_DISCONNECTING:
85 case ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT:
87 case ENET_PEER_STATE_ZOMBIE:
88 return PeerState::ZOMBIE;
89 default:
91 }
92}
93
95 if (!_peer) {
96 throw std::runtime_error("Cannot get address of null peer");
97 }
98
99 // Create and cache the address wrapper in member variable
100 // This ensures thread-safety and correct address for this specific peer
101 if (!_cachedAddress) {
102 _cachedAddress = std::make_unique<ENetAddressWrapper>(_peer->address);
103 }
104 return *_cachedAddress;
105}
106
107uint32_t ENetPeerWrapper::getID() const {
108 if (!_peer) {
109 return 0;
110 }
111 return _peer->connectID;
112}
113
115 if (!_peer) {
116 return 0;
117 }
118 return _peer->roundTripTime;
119}
120
121void ENetPeerWrapper::setData(void *data) {
122 if (_peer) {
123 _peer->data = data;
124 }
125}
126
128 if (!_peer) {
129 return nullptr;
130 }
131 return _peer->data;
132}
PeerState
Represents the connection state of a peer.
Definition IPeer.hpp:19
@ CONNECTION_SUCCEEDED
Connection has succeeded.
@ DISCONNECT_LATER
Peer is scheduled to disconnect.
@ ACKNOWLEDGING_CONNECT
Connection acknowledgment is being sent.
@ ACKNOWLEDGING_DISCONNECT
Disconnect acknowledgment is being sent.
@ ZOMBIE
Peer is in a zombie state.
@ CONNECTION_PENDING
Connection is pending.
@ DISCONNECTED
Peer is disconnected.
@ CONNECTING
Connection to peer is being established.
@ CONNECTED
Peer is connected.
@ DISCONNECTING
Disconnection is in progress.
void setData(void *data) override
Set application-specific data associated with this peer.
Definition ENetPeer.cpp:121
uint32_t getRoundTripTime() const override
Get the round-trip time (ping) to this peer.
Definition ENetPeer.cpp:114
void * getData() const override
Get application-specific data associated with this peer.
Definition ENetPeer.cpp:127
ENetPeer * getNativePeer() const
Definition ENetPeer.cpp:59
ENetPeerWrapper(ENetPeer *peer)
Definition ENetPeer.cpp:13
PeerState getState() const override
Get the current state of this peer.
Definition ENetPeer.cpp:63
ENetPeer * _peer
Definition ENetPeer.hpp:34
void disconnectNow(uint32_t data) override
Force an immediate disconnect from this peer.
Definition ENetPeer.cpp:46
void disconnectLater(uint32_t data) override
Disconnect from this peer after all queued packets are sent.
Definition ENetPeer.cpp:52
void disconnect(uint32_t data) override
Disconnect from this peer.
Definition ENetPeer.cpp:40
bool send(std::unique_ptr< IPacket > packet, uint8_t channelID) override
Send a packet to this peer.
Definition ENetPeer.cpp:19
std::unique_ptr< ENetAddressWrapper > _cachedAddress
Definition ENetPeer.hpp:35
const IAddress & getAddress() const override
Get the address of this peer.
Definition ENetPeer.cpp:94
uint32_t getID() const override
Get a unique identifier for this peer.
Definition ENetPeer.cpp:107
Interface representing a network address (IP + port).
Definition IAddress.hpp:21