StringTokenizer 복구
This commit is contained in:
109
impl/Socket/Address.cpp
Normal file
109
impl/Socket/Address.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#include "Socket/Address.hpp"
|
||||
#include "precomp.hpp"
|
||||
#include <format>
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
Address::Address() {
|
||||
zeroFill();
|
||||
}
|
||||
|
||||
Address::Address(int type, gsl::czstring presentationAddr, std::uint16_t port) {
|
||||
set(type, presentationAddr, port);
|
||||
}
|
||||
|
||||
void Address::zeroFill() {
|
||||
memset(&addr_in6, 0, sizeof(addr_in6));
|
||||
}
|
||||
|
||||
void Address::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 Address::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 Address::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 Address::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);
|
||||
}
|
||||
}
|
||||
|
||||
void Address::setType(int type) {
|
||||
zeroFill();
|
||||
|
||||
if (type == AF_INET)
|
||||
length = sizeof(sockaddr_in);
|
||||
else if (type == AF_INET6)
|
||||
length = sizeof(sockaddr_in6);
|
||||
}
|
||||
|
||||
Address::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> Address::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;
|
||||
}
|
||||
|
||||
}
|
||||
21
impl/Socket/WSAManager.cpp
Normal file
21
impl/Socket/WSAManager.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include "Socket/WSAManager.hpp"
|
||||
#include "Socket/Log.hpp"
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
namespace Chattr {
|
||||
WSAManager::WSAManager() {
|
||||
#ifdef _WIN32
|
||||
WSADATA wsa;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
||||
log::critical("WSAStartup()");
|
||||
#endif
|
||||
}
|
||||
|
||||
WSAManager::~WSAManager() {
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user