libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
socketaddress.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstdint>
12#include <promeki/core/string.h>
13#include <promeki/core/result.h>
16
17#if defined(PROMEKI_PLATFORM_WINDOWS)
18# include <winsock2.h>
19# include <ws2tcpip.h>
20#elif !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
21# include <sys/socket.h>
22# include <netinet/in.h>
23#endif
24
26
27class TextStream;
28
51 public:
65
74
83
85 SocketAddress() = default;
86
93 : _address(address), _port(port) { }
94
101 : _address(addr), _port(port) { }
102
109 : _address(addr), _port(port) { }
110
112 const NetworkAddress &address() const { return _address; }
113
115 void setAddress(const NetworkAddress &address) { _address = address; }
116
118 uint16_t port() const { return _port; }
119
121 void setPort(uint16_t port) { _port = port; }
122
124 bool isNull() const { return _address.isNull() && _port == 0; }
125
127 bool isIPv4() const { return _address.isIPv4(); }
128
130 bool isIPv6() const { return _address.isIPv6(); }
131
133 bool isLoopback() const { return _address.isLoopback(); }
134
136 bool isMulticast() const { return _address.isMulticast(); }
137
145
146#if !defined(PROMEKI_PLATFORM_EMSCRIPTEN)
154 static Result<SocketAddress> fromSockAddr(const struct sockaddr *addr, size_t len);
155
161 size_t toSockAddr(struct sockaddr_storage *storage) const;
162#endif
163
165 bool operator==(const SocketAddress &other) const {
166 return _address == other._address && _port == other._port;
167 }
168
170 bool operator!=(const SocketAddress &other) const {
171 return !(*this == other);
172 }
173
174 private:
175 NetworkAddress _address;
176 uint16_t _port = 0;
177};
178
181
IPv4 network address.
Definition ipv4address.h:51
static Ipv4Address loopback()
Returns the loopback address (127.0.0.1).
Definition ipv4address.h:74
static Ipv4Address any()
Returns the INADDR_ANY address (0.0.0.0).
Definition ipv4address.h:71
IPv6 network address.
Definition ipv6address.h:52
Dynamic array container wrapping std::vector.
Definition list.h:40
High-level network address that can represent IPv4, IPv6, or an unresolved hostname.
Definition networkaddress.h:64
bool isMulticast() const
Returns true if the address is a multicast address.
bool isNull() const
Returns true if no address is stored.
Definition networkaddress.h:127
bool isLoopback() const
Returns true if the address is a loopback address.
bool isIPv4() const
Returns true if this holds an IPv4 address.
Definition networkaddress.h:130
bool isIPv6() const
Returns true if this holds an IPv6 address.
Definition networkaddress.h:133
Network address with port number.
Definition socketaddress.h:50
SocketAddress(const Ipv4Address &addr, uint16_t port)
Constructs from an IPv4 address and port.
Definition socketaddress.h:100
SocketAddress()=default
Default constructor. Creates a null address with port 0.
SocketAddress(const NetworkAddress &address, uint16_t port)
Constructs from a NetworkAddress and port.
Definition socketaddress.h:92
bool isIPv4() const
Returns true if the address is IPv4.
Definition socketaddress.h:127
void setPort(uint16_t port)
Sets the port number.
Definition socketaddress.h:121
void setAddress(const NetworkAddress &address)
Sets the network address component.
Definition socketaddress.h:115
bool isLoopback() const
Returns true if the address is a loopback address.
Definition socketaddress.h:133
bool isNull() const
Returns true if no address is set and port is 0.
Definition socketaddress.h:124
bool isMulticast() const
Returns true if the address is a multicast address.
Definition socketaddress.h:136
String toString() const
Returns a "host:port" string representation.
uint16_t port() const
Returns the port number.
Definition socketaddress.h:118
static Result< SocketAddress > fromSockAddr(const struct sockaddr *addr, size_t len)
Constructs from a POSIX/Windows sockaddr structure.
static Result< SocketAddress > fromString(const String &hostPort)
Parses a "host:port" string into a SocketAddress.
bool operator!=(const SocketAddress &other) const
Inequality comparison.
Definition socketaddress.h:170
size_t toSockAddr(struct sockaddr_storage *storage) const
Fills a sockaddr_storage with this address and port.
static SocketAddress localhost(uint16_t port)
Returns localhost (127.0.0.1) with the given port.
Definition socketaddress.h:80
static SocketAddress any(uint16_t port)
Returns INADDR_ANY with the given port.
Definition socketaddress.h:71
bool operator==(const SocketAddress &other) const
Equality comparison.
Definition socketaddress.h:165
bool isIPv6() const
Returns true if the address is IPv6.
Definition socketaddress.h:130
SocketAddress(const Ipv6Address &addr, uint16_t port)
Constructs from an IPv6 address and port.
Definition socketaddress.h:108
const NetworkAddress & address() const
Returns the network address component.
Definition socketaddress.h:112
Encoding-aware string class with copy-on-write semantics.
Definition string.h:35
Formatted text I/O with encoding awareness.
Definition textstream.h:54
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19
TextStream & operator<<(TextStream &stream, const SocketAddress &addr)
Writes the socket address to the stream.