.
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);
|
||||
};
|
||||
|
||||
}
|
||||
19
include/Utils/StringTokenizer.hpp
Normal file
19
include/Utils/StringTokenizer.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include "precomp.hpp"
|
||||
#include <queue>
|
||||
|
||||
namespace Chattr {
|
||||
class StringTokenizer {
|
||||
public:
|
||||
StringTokenizer() {}
|
||||
StringTokenizer(gsl::czstring string, std::uint32_t size);
|
||||
|
||||
void set(gsl::czstring string, std::uint32_t size);
|
||||
std::vector<std::string> get();
|
||||
|
||||
operator std::vector<std::string>();
|
||||
private:
|
||||
std::queue<gsl::czstring> tokens;
|
||||
std::queue<std::uint32_t> tokens_length;
|
||||
};
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
#error "이 플랫폼은 지원되지 않습니다."
|
||||
#endif
|
||||
#include <functional>
|
||||
#include <future>
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
@@ -15,23 +16,26 @@ class Thread {
|
||||
public:
|
||||
#ifdef _WIN32
|
||||
static unsigned __stdcall thread_func(LPVOID param) {
|
||||
std::unique_ptr<std::function<void()>> func(static_cast<std::function<void()>*>(param));
|
||||
(*func)();
|
||||
auto task(static_cast<std::packaged_task<void()>*>(param));
|
||||
(*task)();
|
||||
delete task;
|
||||
return 0;
|
||||
}
|
||||
#elif __linux__
|
||||
static void* thread_func(void *param) {
|
||||
std::unique_ptr<std::function<void()>> func(static_cast<std::function<void()>*>(param));
|
||||
(*func)();
|
||||
auto task(static_cast<std::packaged_task<void()>*>(param));
|
||||
(*task)();
|
||||
delete task;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
template<typename Callable, typename... Args>
|
||||
Thread(Callable&& f, Args&&... args) {
|
||||
auto boundFunc = std::bind(std::forward<Callable>(f), std::forward<Args>(args)...);
|
||||
|
||||
auto funcPtr = new std::function<void()>(boundFunc);
|
||||
|
||||
template<typename _Callable, typename... _Args>
|
||||
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>) //복사 생성하면 안 되므로
|
||||
Thread(_Callable&& __f, _Args&&... __args) {
|
||||
auto boundFunc = [__f = std::move(__f), ... __args = std::move(__args)]() mutable {
|
||||
__f(std::move(__args)...);
|
||||
};
|
||||
auto funcPtr = new std::packaged_task<void()>(std::move(boundFunc));
|
||||
#ifdef _WIN32
|
||||
handle_ = (HANDLE)_beginthreadex(nullptr, 0, thread_func, funcPtr, 0, nullptr);
|
||||
#elif __linux__
|
||||
|
||||
Reference in New Issue
Block a user