R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ENetAddress.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** ENetAddress.cpp
6*/
7
8#include "ENetAddress.hpp"
9#include <cstring>
10#include <stdexcept>
11
12ENetAddressWrapper::ENetAddressWrapper(const std::string &hostname, uint16_t port) {
13 _address.port = port;
14
15 if (enet_address_set_host(&_address, hostname.c_str()) != 0) {
16 throw std::runtime_error("Failed to set ENet host address: " + hostname);
17 }
18}
19
20ENetAddressWrapper::ENetAddressWrapper(const ENetAddress &address) : _address(address) {}
21
22ENetAddressWrapper::ENetAddressWrapper(const ENetAddressWrapper &other) : _address(other._address) {}
23
25 if (this != &other) {
26 _address = other._address;
27 }
28 return *this;
29}
30
31std::string ENetAddressWrapper::getHost() const {
32 char buffer[256];
33 if (enet_address_get_host(&_address, buffer, sizeof(buffer)) == 0) {
34 return std::string(buffer);
35 }
36 return "";
37}
38
40 return _address.port;
41}
42
43const ENetAddress &ENetAddressWrapper::getNativeAddress() const {
44 return _address;
45}
46
48 return _address;
49}
50
51void ENetAddressWrapper::setHost(const std::string &hostname) {
52 if (enet_address_set_host(&_address, hostname.c_str()) != 0) {
53 throw std::runtime_error("Failed to set ENet host address: " + hostname);
54 }
55}
56
57void ENetAddressWrapper::setPort(uint16_t port) {
58 _address.port = port;
59}
void setHost(const std::string &hostname) override
Set the hostname or IP address.
std::string getHost() const override
Get the hostname or IP address as a string.
ENetAddressWrapper & operator=(const ENetAddressWrapper &other)
ENetAddressWrapper(const std::string &hostname, uint16_t port)
ENetAddress _address
uint16_t getPort() const override
Get the port number.
void setPort(uint16_t port) override
Set the port number.
const ENetAddress & getNativeAddress() const