Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::net::Endpoint Class Reference

IPv4 or IPv6 address and port of a peer. More...

#include <aegis2d/net/endpoint.hpp>

Public Member Functions

 Endpoint () noexcept=default
 The unspecified IPv4 endpoint, 0.0.0.0:0.
 
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.
 
std::uint16_t port () const noexcept
 Port number.
 
std::string address () const
 Address in text form.
 
std::string to_string () const
 Address and port in text form, for logs.
 
bool operator== (const Endpoint &) const =default
 Same address family, address and port.
 

Static Public Member Functions

static Endpoint v4 (std::array< std::uint8_t, 4 > address, std::uint16_t port) noexcept
 IPv4 endpoint from the four bytes of the address.
 
static Endpoint v6 (std::array< std::uint8_t, 16 > address, std::uint16_t port) noexcept
 IPv6 endpoint from the sixteen bytes of the address.
 
static Endpoint loopback (std::uint16_t port) noexcept
 IPv4 loopback endpoint, 127.0.0.1.
 
static std::optional< Endpoint > parse (std::string_view address, std::uint16_t port)
 Endpoint from a numeric address.
 

Detailed Description

IPv4 or IPv6 address and port of a peer.

A plain value, comparable and hashable, so it can key the sessions of a server or per-address counters. It never resolves host names: parse() only accepts numeric addresses. An IPv4-mapped IPv6 address received on an IPv6 socket is stored as IPv4, so a peer has one endpoint whatever the socket.

Constructor & Destructor Documentation

◆ Endpoint()

aegis2d::net::Endpoint::Endpoint ( )
defaultnoexcept

The unspecified IPv4 endpoint, 0.0.0.0:0.

Member Function Documentation

◆ address()

std::string aegis2d::net::Endpoint::address ( ) const

Address in text form.

Returns
"127.0.0.1", "::1"...

◆ address_bytes()

std::span< const std::uint8_t > aegis2d::net::Endpoint::address_bytes ( ) const
noexcept

Bytes of the address, most significant first.

Returns
4 bytes for IPv4, 16 for IPv6.

◆ is_v6()

bool aegis2d::net::Endpoint::is_v6 ( ) const
noexcept

Tells whether the address is IPv6.

Returns
true for IPv6, false for IPv4.

◆ loopback()

static Endpoint aegis2d::net::Endpoint::loopback ( std::uint16_t  port)
staticnoexcept

IPv4 loopback endpoint, 127.0.0.1.

Parameters
portPort number.
Returns
The endpoint.

◆ operator==()

bool aegis2d::net::Endpoint::operator== ( const Endpoint &  ) const
default

Same address family, address and port.

Returns
true if both endpoints designate the same peer.

◆ parse()

static std::optional< Endpoint > aegis2d::net::Endpoint::parse ( std::string_view  address,
std::uint16_t  port 
)
static

Endpoint from a numeric address.

Parameters
address"192.168.1.10", "::1"...; host names are not resolved.
portPort number.
Returns
The endpoint, or nothing if address is not a numeric IPv4 or IPv6 address.

◆ port()

std::uint16_t aegis2d::net::Endpoint::port ( ) const
noexcept

Port number.

Returns
The port.

◆ to_string()

std::string aegis2d::net::Endpoint::to_string ( ) const

Address and port in text form, for logs.

Returns
"127.0.0.1:4242", or "[::1]:4242" for IPv6.

◆ v4()

static Endpoint aegis2d::net::Endpoint::v4 ( std::array< std::uint8_t, 4 >  address,
std::uint16_t  port 
)
staticnoexcept

IPv4 endpoint from the four bytes of the address.

Parameters
addressAddress bytes, most significant first: {127, 0, 0, 1}.
portPort number.
Returns
The endpoint.

◆ v6()

static Endpoint aegis2d::net::Endpoint::v6 ( std::array< std::uint8_t, 16 >  address,
std::uint16_t  port 
)
staticnoexcept

IPv6 endpoint from the sixteen bytes of the address.

Parameters
addressAddress bytes, most significant first.
portPort number.
Returns
The endpoint.

The documentation for this class was generated from the following file: