.
This commit is contained in:
@@ -1,13 +1,101 @@
|
||||
#pragma once
|
||||
#include "precomp.hpp"
|
||||
#include <format>
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
struct Address {
|
||||
Address() {
|
||||
zeroFill();
|
||||
}
|
||||
|
||||
Address(int type, gsl::czstring presentationAddr, std::uint16_t port) {
|
||||
set(type, presentationAddr, port);
|
||||
}
|
||||
|
||||
void zeroFill() {
|
||||
memset(&addr_in6, 0, sizeof(addr_in6));
|
||||
}
|
||||
|
||||
void set(int type, gsl::czstring presentationAddr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET) {
|
||||
addr_in.sin_family = AF_INET;
|
||||
::inet_pton(AF_INET, presentationAddr, &addr_in.sin_addr);
|
||||
addr_in.sin_port = htons(port);
|
||||
length = sizeof(sockaddr_in);
|
||||
} else if (type == AF_INET6) {
|
||||
addr_in6.sin6_family = AF_INET6;
|
||||
::inet_pton(AF_INET6, presentationAddr, &addr_in6.sin6_addr);
|
||||
addr_in6.sin6_port = htons(port);
|
||||
length = sizeof(sockaddr_in6);
|
||||
}
|
||||
}
|
||||
|
||||
void set(int type, in_addr_t addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET) {
|
||||
addr_in.sin_family = AF_INET;
|
||||
addr_in.sin_addr.s_addr = htonl(addr);
|
||||
addr_in.sin_port = htons(port);
|
||||
length = sizeof(sockaddr_in);
|
||||
}
|
||||
}
|
||||
|
||||
void set(int type, in_addr addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET) {
|
||||
addr_in.sin_family = AF_INET;
|
||||
addr_in.sin_addr = addr;
|
||||
addr_in.sin_port = htons(port);
|
||||
length = sizeof(sockaddr_in);
|
||||
}
|
||||
}
|
||||
|
||||
void set(int type, in6_addr addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET6) {
|
||||
addr_in6.sin6_family = AF_INET6;
|
||||
addr_in6.sin6_addr = addr;
|
||||
addr_in6.sin6_port = htons(port);
|
||||
length = sizeof(sockaddr_in6);
|
||||
}
|
||||
}
|
||||
|
||||
operator std::string() {
|
||||
std::optional<std::uint16_t> port = getPort();
|
||||
|
||||
if (!port)
|
||||
return std::string();
|
||||
|
||||
if (length == sizeof(addr_in)) {
|
||||
char addrStr[INET_ADDRSTRLEN];
|
||||
::inet_ntop(AF_INET, &addr_in.sin_addr, addrStr, sizeof(addrStr));
|
||||
|
||||
return std::format("{}:{}", addrStr, port.value());
|
||||
} else if (length == sizeof(addr_in6)) {
|
||||
char addrStr[INET6_ADDRSTRLEN];
|
||||
::inet_ntop(AF_INET6, &addr_in6.sin6_addr, addrStr, sizeof(addrStr));
|
||||
|
||||
return std::format("{}:{}", addrStr, port.value());
|
||||
}
|
||||
|
||||
return std::string();
|
||||
}
|
||||
|
||||
std::optional<std::uint16_t> getPort() {
|
||||
if (length == sizeof(addr_in))
|
||||
return ntohs(addr_in.sin_port);
|
||||
else if (length == sizeof(addr_in6))
|
||||
return ntohs(addr_in6.sin6_port);
|
||||
else
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
union {
|
||||
struct sockaddr addr;
|
||||
struct sockaddr_in addr_in;
|
||||
|
||||
@@ -11,9 +11,11 @@ public:
|
||||
~Socket();
|
||||
|
||||
void init(int domain, int type, int protocol);
|
||||
void destruct();
|
||||
|
||||
operator SOCKET() const;
|
||||
void move(const SOCKET);
|
||||
Socket& move();
|
||||
|
||||
void bind(sockaddr *__addr);
|
||||
void bind(sockaddr *__addr, socklen_t __len);
|
||||
@@ -27,7 +29,9 @@ public:
|
||||
void bind(sockaddr_in6 *__addr, socklen_t __len);
|
||||
|
||||
Socket(const Socket&) = delete;
|
||||
Socket(Socket&&);
|
||||
Socket& operator=(const Socket&) = delete;
|
||||
Socket& operator=(Socket&&);
|
||||
|
||||
struct Address bindAddr = {};
|
||||
protected:
|
||||
|
||||
@@ -11,7 +11,7 @@ public:
|
||||
void init(int domain);
|
||||
void listen(int __n);
|
||||
void accept(TCPSocket& newSock, Address& addr);
|
||||
void connect(Chattr::Address serveraddr);
|
||||
void connect(Chattr::Address& serveraddr);
|
||||
};
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user