iocp 구현하기
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
#include "socket/address.h"
|
||||
|
||||
#include <openssl/bio.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
|
||||
#include <format>
|
||||
|
||||
#include "precomp.h"
|
||||
|
||||
namespace Socket {
|
||||
namespace Network {
|
||||
|
||||
Address::Address() { zeroFill(); }
|
||||
|
||||
@@ -17,55 +19,56 @@ void Address::zeroFill() { memset(&addr_in6, 0, sizeof(addr_in6)); }
|
||||
void Address::set(int type, gsl::czstring presentationAddr,
|
||||
std::uint16_t port) {
|
||||
zeroFill();
|
||||
setType(type);
|
||||
|
||||
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);
|
||||
BIO_ADDRINFO* res;
|
||||
if (!BIO_lookup_ex(presentationAddr, std::to_string(port).c_str(),
|
||||
BIO_LOOKUP_CLIENT, type, SOCK_DGRAM, 0, &res)) {
|
||||
throw std::runtime_error("can't resolve address");
|
||||
}
|
||||
|
||||
int sock = -1;
|
||||
for (const BIO_ADDRINFO* ai = res; ai != nullptr;
|
||||
ai = ::BIO_ADDRINFO_next(ai)) {
|
||||
sock = BIO_socket(BIO_ADDRINFO_family(ai), SOCK_DGRAM, 0, 0);
|
||||
if (sock == -1) continue;
|
||||
}
|
||||
::close(sock);
|
||||
|
||||
addr_in.sin_family = type;
|
||||
::inet_pton(type, presentationAddr, &addr_in.sin_addr);
|
||||
addr_in.sin_port = htons(port);
|
||||
}
|
||||
|
||||
void Address::set(int type, in_addr_t addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
setType(type);
|
||||
|
||||
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);
|
||||
}
|
||||
addr_in.sin_family = type;
|
||||
addr_in.sin_addr.s_addr = htonl(addr);
|
||||
addr_in.sin_port = htons(port);
|
||||
}
|
||||
|
||||
void Address::set(int type, in_addr addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
setType(type);
|
||||
|
||||
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);
|
||||
}
|
||||
addr_in.sin_family = type;
|
||||
addr_in.sin_addr = addr;
|
||||
addr_in.sin_port = htons(port);
|
||||
}
|
||||
|
||||
void Address::set(int type, in6_addr addr, std::uint16_t port) {
|
||||
zeroFill();
|
||||
setType(type);
|
||||
|
||||
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);
|
||||
}
|
||||
addr_in6.sin6_family = type;
|
||||
addr_in6.sin6_addr = addr;
|
||||
addr_in6.sin6_port = htons(port);
|
||||
}
|
||||
|
||||
void Address::setType(int type) {
|
||||
zeroFill();
|
||||
family = type;
|
||||
|
||||
if (type == AF_INET)
|
||||
length = sizeof(sockaddr_in);
|
||||
@@ -74,32 +77,23 @@ void Address::setType(int type) {
|
||||
}
|
||||
|
||||
Address::operator std::string() {
|
||||
std::optional<std::uint16_t> port = getPort();
|
||||
char addrStr[INET6_ADDRSTRLEN];
|
||||
|
||||
if (!port) return std::string();
|
||||
if (family != AF_INET && family != AF_INET6 || !getPort())
|
||||
return std::string();
|
||||
|
||||
if (length == sizeof(addr_in)) {
|
||||
char addrStr[INET_ADDRSTRLEN];
|
||||
::inet_ntop(AF_INET, &addr_in.sin_addr, addrStr, sizeof(addrStr));
|
||||
::inet_ntop(family, &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();
|
||||
return std::format("{}:{}", addrStr, getPort());
|
||||
}
|
||||
|
||||
std::uint16_t Address::getPort() {
|
||||
if (length == sizeof(addr_in))
|
||||
std::uint16_t Address::getPort() const {
|
||||
if (family == AF_INET)
|
||||
return ntohs(addr_in.sin_port);
|
||||
else if (length == sizeof(addr_in6))
|
||||
else if (family == AF_INET6)
|
||||
return ntohs(addr_in6.sin6_port);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace Chattr
|
||||
} // namespace Network
|
||||
|
||||
Reference in New Issue
Block a user