COROIO: coroio/address.hpp Source File
COROIO
Loading...
Searching...
No Matches
address.hpp
1#pragma once
2
3#ifdef _WIN32
4#include <winsock2.h>
5#include <ws2tcpip.h>
6#include <io.h>
7#else
8#include <arpa/inet.h>
9#include <sys/stat.h>
10#include <sys/socket.h>
11#include <sys/types.h>
12#include <netinet/in.h>
13#include <unistd.h>
14#include <fcntl.h>
15#endif
16
17#include <string>
18#include <variant>
19
20#ifdef min
21#undef min
22#endif
23
24#ifdef max
25#undef max
26#endif
27
28namespace NNet {
29
38class TAddress {
39public:
50 TAddress(const std::string& addr, int port);
56 TAddress(sockaddr_in addr);
62 TAddress(sockaddr_in6 addr);
73 TAddress(sockaddr* addr, socklen_t len);
82 TAddress() = default;
91 const std::variant<sockaddr_in, sockaddr_in6>& Addr() const;
102 std::pair<const sockaddr*, int> RawAddr() const;
112 bool operator == (const TAddress& other) const;
120 int Domain() const;
130 TAddress WithPort(int port) const;
140 std::string ToString() const;
141
142private:
143 std::variant<sockaddr_in, sockaddr_in6> Addr_ = {};
144};
145
146} // namespace NNet
std::pair< const sockaddr *, int > RawAddr() const
Returns a pointer to the raw sockaddr structure and its size in bytes.
TAddress(sockaddr_in addr)
Constructs an address from an IPv4 sockaddr_in structure.
int Domain() const
Gets the domain (address family) of the stored address.
TAddress()=default
Default constructor. Creates an empty address.
const std::variant< sockaddr_in, sockaddr_in6 > & Addr() const
Retrieves the internal variant storing the address.
TAddress(const std::string &addr, int port)
Constructs an address from a string representation and port.
TAddress(sockaddr *addr, socklen_t len)
Constructs an address from a generic sockaddr pointer.
std::string ToString() const
Converts the address to a human-readable string format.
TAddress WithPort(int port) const
Returns a new TAddress with the same IP but a different port.
TAddress(sockaddr_in6 addr)
Constructs an address from an IPv6 sockaddr_in6 structure.
bool operator==(const TAddress &other) const
Equality operator for TAddress.