.
This commit is contained in:
10
.vscode/settings.json
vendored
10
.vscode/settings.json
vendored
@@ -71,6 +71,14 @@
|
||||
"queue": "cpp",
|
||||
"stack": "cpp",
|
||||
"fstream": "cpp",
|
||||
"regex": "cpp"
|
||||
"regex": "cpp",
|
||||
"assert": "cpp",
|
||||
"pointers": "cpp",
|
||||
"future": "cpp",
|
||||
"zstring": "cpp",
|
||||
"set": "cpp",
|
||||
"util": "cpp",
|
||||
"expected": "cpp",
|
||||
"complex": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,9 @@ int main() {
|
||||
Chattr::TCPSocket sock;
|
||||
sock.init(AF_INET6);
|
||||
|
||||
Chattr::Address serveraddr;
|
||||
serveraddr.addr_in6.sin6_family = AF_INET6;
|
||||
inet_pton(AF_INET6, "::1", &serveraddr.addr_in6.sin6_addr);
|
||||
serveraddr.addr_in6.sin6_port = htons(9010);
|
||||
serveraddr.length = sizeof(sockaddr_in6);
|
||||
Chattr::Address serveraddr(AF_INET6, "::1", 9010);
|
||||
|
||||
sock.connect(serveraddr);
|
||||
|
||||
spdlog::info("Connection established from {}, {}", sock, (std::string)serveraddr);
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
[2025-04-24 21:34:04.752] [Chattering Logger] [info] thread?
|
||||
[2025-04-24 21:34:04.752] [Chattering Logger] [info] Waiting for connection...
|
||||
[2025-04-24 21:34:24.926] [Chattering Logger] [info] thread?
|
||||
[2025-04-24 21:34:24.926] [Chattering Logger] [info] Waiting for connection...
|
||||
[2025-04-24 21:34:39.658] [Chattering Logger] [info] thread?
|
||||
[2025-04-24 21:34:39.658] [Chattering Logger] [info] Waiting for connection...
|
||||
[2025-04-24 21:34:48.422] [Chattering Logger] [info] Waiting for connection...
|
||||
[2025-04-24 21:34:48.422] [Chattering Logger] [info] thread?
|
||||
[2025-04-24 21:34:49.422] [Chattering Logger] [info] thread?
|
||||
[2025-04-24 21:35:32.526] [Chattering Logger] [info] thread?
|
||||
[2025-04-24 21:35:33.526] [Chattering Logger] [info] thread?
|
||||
[2025-04-24 21:35:33.526] [Chattering Logger] [info] Waiting for connection...
|
||||
@@ -1,8 +0,0 @@
|
||||
{
|
||||
"IP Version" : 6,
|
||||
"Listen Port" : 9010,
|
||||
"LogLevel" : 1,
|
||||
"LogfileCount" : 5,
|
||||
"LogfileName" : "Chattering.log",
|
||||
"LogfileSize" : 4294967295
|
||||
}
|
||||
@@ -2,34 +2,27 @@
|
||||
#include "Socket/Log.hpp"
|
||||
#include "Utils/ConfigManager.hpp"
|
||||
#include "Utils/Thread.hpp"
|
||||
#include "Utils/StringTokenizer.hpp"
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
void _TCPClient(Chattr::TCPSocket sock, Chattr::Address addr);
|
||||
|
||||
int main() {
|
||||
auto config = Chattr::ConfigManager::load();
|
||||
Chattr::log::setDefaultLogger(config.logLevel, config.logFileName, config.logfileSize, config.logfileCount);
|
||||
|
||||
struct Chattr::TCPSocket sock;
|
||||
struct Chattr::Address serveraddr;
|
||||
if (config.ipVersion == 4) {
|
||||
sock.init(AF_INET);
|
||||
|
||||
struct Chattr::Address serveraddr;
|
||||
serveraddr.addr_in.sin_family = AF_INET;
|
||||
serveraddr.addr_in.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
serveraddr.addr_in.sin_port = htons(config.listenPort);
|
||||
|
||||
sock.bind(&serveraddr.addr_in);
|
||||
serveraddr.set(AF_INET, INADDR_ANY, config.listenPort);
|
||||
}
|
||||
else {
|
||||
else if (config.ipVersion == 6) {
|
||||
sock.init(AF_INET6);
|
||||
|
||||
struct Chattr::Address serveraddr;
|
||||
serveraddr.addr_in6.sin6_family = AF_INET6;
|
||||
serveraddr.addr_in6.sin6_addr = in6addr_any;
|
||||
serveraddr.addr_in6.sin6_port = htons(config.listenPort);
|
||||
|
||||
sock.bind(&serveraddr.addr_in6);
|
||||
serveraddr.set(AF_INET6, in6addr_any, config.listenPort);
|
||||
}
|
||||
sock.bind(&serveraddr.addr, serveraddr.length);
|
||||
sock.listen(SOMAXCONN);
|
||||
struct Chattr::TCPSocket clientSock;
|
||||
struct Chattr::Address clientAddr;
|
||||
@@ -39,12 +32,15 @@ int main() {
|
||||
else
|
||||
clientAddr.length = sizeof(sockaddr_in6);
|
||||
|
||||
Chattr::Thread t1([]() {
|
||||
spdlog::info("thread?");
|
||||
sleep(1);
|
||||
spdlog::info("thread?");
|
||||
});
|
||||
while (true) {
|
||||
spdlog::info("Waiting for connection...");
|
||||
sock.accept(clientSock, clientAddr);
|
||||
|
||||
Chattr::Thread thread_(_TCPClient, std::move(clientSock), clientAddr);
|
||||
}
|
||||
}
|
||||
|
||||
void _TCPClient(Chattr::TCPSocket sock, Chattr::Address addr) {
|
||||
spdlog::info("Connection accepted! from: {}, {}", sock, (std::string)addr);
|
||||
|
||||
spdlog::info("Waiting for connection...");
|
||||
// sock.accept(clientSock, clientAddr);
|
||||
}
|
||||
@@ -22,14 +22,7 @@ Socket::Socket(int domain, int type, int protocol) {
|
||||
}
|
||||
|
||||
Socket::~Socket() {
|
||||
if (!valid_)
|
||||
return;
|
||||
#ifdef _WIN32
|
||||
closesocket(sock_);
|
||||
WSACleanup();
|
||||
#elif __linux__
|
||||
::close(sock_);
|
||||
#endif
|
||||
destruct();
|
||||
}
|
||||
|
||||
void Socket::init(int domain, int type, int protocol) {
|
||||
@@ -45,6 +38,17 @@ void Socket::init(int domain, int type, int protocol) {
|
||||
valid_ = true;
|
||||
}
|
||||
|
||||
void Socket::destruct() {
|
||||
if (!valid_)
|
||||
return;
|
||||
#ifdef _WIN32
|
||||
closesocket(sock_);
|
||||
WSACleanup();
|
||||
#elif __linux__
|
||||
::close(sock_);
|
||||
#endif
|
||||
}
|
||||
|
||||
Socket::operator SOCKET() const {
|
||||
if (valid_)
|
||||
return sock_;
|
||||
@@ -52,30 +56,25 @@ Socket::operator SOCKET() const {
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
void Socket::move(const SOCKET __sock) {
|
||||
if (__sock == INVALID_SOCKET)
|
||||
void Socket::move(const SOCKET __sock)
|
||||
{
|
||||
if (__sock == INVALID_SOCKET)
|
||||
log::critical("socket()");
|
||||
|
||||
if (valid_) {
|
||||
#ifdef _WIN32
|
||||
closesocket(sock_);
|
||||
WSACleanup();
|
||||
#elif __linux__
|
||||
::close(sock_);
|
||||
#endif
|
||||
}
|
||||
else {
|
||||
#ifdef _WIN32
|
||||
WSADATA wsa;
|
||||
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
|
||||
log::critical("WSAStartup()");
|
||||
#endif
|
||||
}
|
||||
destruct();
|
||||
|
||||
sock_ = __sock;
|
||||
valid_ = true;
|
||||
};
|
||||
|
||||
Socket& Socket::move() {
|
||||
std::shared_ptr<Socket> socket = std::make_shared<Socket>();
|
||||
valid_ = false;
|
||||
socket->move(sock_);
|
||||
|
||||
return *socket;
|
||||
};
|
||||
|
||||
void Socket::bind(sockaddr *__addr) {
|
||||
bind((struct sockaddr *)__addr, sizeof(sockaddr));
|
||||
}
|
||||
@@ -104,4 +103,20 @@ void Socket::bind(sockaddr_in6 *__addr, socklen_t __len) {
|
||||
bind((struct sockaddr *)__addr, __len);
|
||||
}
|
||||
|
||||
}
|
||||
Socket::Socket(Socket&& other_) {
|
||||
other_.valid_ = false;
|
||||
sock_ = other_.sock_;
|
||||
valid_ = true;
|
||||
}
|
||||
|
||||
Socket& Socket::operator=(Socket && other_) {
|
||||
other_.valid_ = false;
|
||||
sock_ = other_.sock_;
|
||||
valid_ = true;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,8 +21,8 @@ void TCPSocket::accept(TCPSocket& newSock, Address& __addr) {
|
||||
log::critical("accept()");
|
||||
}
|
||||
|
||||
void TCPSocket::connect(Chattr::Address serveraddr) {
|
||||
int retVal = ::connect(sock_, (struct sockaddr *)&serveraddr, serveraddr.length);
|
||||
void TCPSocket::connect(Chattr::Address& serveraddr) {
|
||||
int retVal = ::connect(sock_, (struct sockaddr *)&serveraddr.addr, serveraddr.length);
|
||||
if (retVal == INVALID_SOCKET)
|
||||
log::critical("connect()");
|
||||
}
|
||||
|
||||
@@ -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