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
20namespace NNet {
21
30class TAddress {
31public:
42 TAddress(const std::string& addr, int port);
48 TAddress(sockaddr_in addr);
54 TAddress(sockaddr_in6 addr);
65 TAddress(sockaddr* addr, socklen_t len);
74 TAddress() = default;
83 const std::variant<sockaddr_in, sockaddr_in6>& Addr() const;
94 std::pair<const sockaddr*, int> RawAddr() const;
104 bool operator == (const TAddress& other) const;
112 int Domain() const;
122 TAddress WithPort(int port) const;
132 std::string ToString() const;
133
134private:
135 std::variant<sockaddr_in, sockaddr_in6> Addr_ = {};
136};
137
138} // 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.