Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
endpoint.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/net/export.hpp>
5
6#include <array>
7#include <cstddef>
8#include <cstdint>
9#include <functional>
10#include <optional>
11#include <span>
12#include <string>
13#include <string_view>
14
15namespace aegis2d::net {
16
22class AEGIS2D_NET_EXPORT Endpoint {
23public:
25 Endpoint() noexcept = default;
26
31 [[nodiscard]] static Endpoint v4(std::array<std::uint8_t, 4> address,
32 std::uint16_t port) noexcept;
33
38 [[nodiscard]] static Endpoint v6(std::array<std::uint8_t, 16> address,
39 std::uint16_t port) noexcept;
40
44 [[nodiscard]] static Endpoint loopback(std::uint16_t port) noexcept;
45
50 [[nodiscard]] static std::optional<Endpoint> parse(std::string_view address,
51 std::uint16_t port);
52
55 [[nodiscard]] bool is_v6() const noexcept;
56
59 [[nodiscard]] std::span<const std::uint8_t> address_bytes() const noexcept;
60
63 [[nodiscard]] std::uint16_t port() const noexcept;
64
67 [[nodiscard]] std::string address() const;
68
71 [[nodiscard]] std::string to_string() const;
72
75 bool operator==(const Endpoint&) const = default;
76
77private:
78 std::array<std::uint8_t, 16> bytes_{};
79 std::uint16_t port_ = 0;
80 bool v6_ = false;
81};
82
83} // namespace aegis2d::net
84
85namespace std {
86
88template <>
89struct hash<aegis2d::net::Endpoint> {
93 std::size_t operator()(const aegis2d::net::Endpoint& endpoint) const noexcept {
94 std::uint64_t hash = 0xCBF29CE484222325ULL;
95 const auto mix = [&hash](std::uint64_t byte) { hash = (hash ^ byte) * 0x100000001B3ULL; };
96 mix(endpoint.is_v6() ? 6U : 4U);
97 for (const std::uint8_t byte : endpoint.address_bytes()) {
98 mix(byte);
99 }
100 mix(endpoint.port() & 0xFFU);
101 mix(endpoint.port() >> 8U);
102 return static_cast<std::size_t>(hash);
103 }
104};
105
106} // namespace std
IPv4 or IPv6 address and port of a peer.
Definition endpoint.hpp:22
bool is_v6() const noexcept
Tells whether the address is IPv6.
std::span< const std::uint8_t > address_bytes() const noexcept
Bytes of the address, most significant first.
Endpoint() noexcept=default
The unspecified IPv4 endpoint, 0.0.0.0:0.
std::uint16_t port() const noexcept
Port number.
Binary serialization, UDP/TCP sockets and the network thread.
Definition binary.hpp:14
Root namespace of the engine.
Definition audio.hpp:17
Definition endpoint.hpp:85