일단 거의 모든 버그 다 고칢..
This commit is contained in:
@@ -1,24 +1,81 @@
|
||||
#pragma once
|
||||
#include "Utils/ThreadPool.hpp"
|
||||
#include "Socket/WSAManager.hpp"
|
||||
#include "Socket/TCPSocket.hpp"
|
||||
#include "Socket/Log.hpp"
|
||||
#include <functional>
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
struct IOCPPASSINDATA {
|
||||
OVERLAPPED overlapped;
|
||||
TCPSocket socket;
|
||||
char buf[1501];
|
||||
int recvbytes;
|
||||
int sendbytes;
|
||||
WSABUF wsabuf;
|
||||
};
|
||||
|
||||
class IOCP {
|
||||
public:
|
||||
IOCP(std::shared_ptr<ThreadPool> __IOCPThread);
|
||||
~IOCP();
|
||||
static void iocpWather(ThreadPool* threadPool, HANDLE completionPort_, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||
DWORD tid = GetCurrentThreadId();
|
||||
spdlog::debug("Waiting IO to complete on TID: {}.", tid);
|
||||
IOCPPASSINDATA* data;
|
||||
SOCKET sock;
|
||||
DWORD cbTransfrred;
|
||||
int retVal = GetQueuedCompletionStatus(completionPort_, &cbTransfrred, (PULONG_PTR)&sock, (LPOVERLAPPED*)&data, INFINITE);
|
||||
if (retVal == 0 || cbTransfrred == 0) {
|
||||
spdlog::info("Client disconnected. [{}]", (std::string)(data->socket.remoteAddr));
|
||||
threadPool->enqueueJob(iocpWather, completionPort_, callback);
|
||||
return;
|
||||
}
|
||||
threadPool->enqueueJob(callback, data);
|
||||
threadPool->enqueueJob(iocpWather, completionPort_, callback);
|
||||
};
|
||||
|
||||
void init(std::shared_ptr<ThreadPool> __IOCPThread);
|
||||
template<typename _Callable>
|
||||
void init(ThreadPool* __IOCPThread, _Callable&& callback) {
|
||||
IOCPThread_ = __IOCPThread;
|
||||
#ifdef _WIN32
|
||||
completionPort_ = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
||||
if (completionPort_ == NULL)
|
||||
log::critical("CreateIoCompletionPort()");
|
||||
|
||||
auto boundFunc = [callback = std::move(callback)](ThreadPool* __IOCPThread, IOCPPASSINDATA* data) mutable {
|
||||
callback(__IOCPThread, data);
|
||||
};
|
||||
|
||||
int tCount = __IOCPThread->threadCount;
|
||||
|
||||
spdlog::info("Resizing threadpool size to: {}", tCount * 2);
|
||||
|
||||
__IOCPThread->respawnWorker(tCount * 2);
|
||||
|
||||
spdlog::info("Set IOCP Worker count to: {}", tCount);
|
||||
for (int i = 0; i < tCount; i++) {
|
||||
std::function<void(ThreadPool*, IOCPPASSINDATA*)> task(boundFunc);
|
||||
__IOCPThread->enqueueJob(iocpWather, completionPort_, task);
|
||||
}
|
||||
#elif __linux__
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void registerSocket(SOCKET sock);
|
||||
|
||||
int recv(void* __restrict __buf, size_t __n, int __flags);
|
||||
int send(const void* __buf, size_t __n, int __flags);
|
||||
|
||||
private:
|
||||
std::shared_ptr<ThreadPool> IOCPThread_;
|
||||
struct Chattr::WSAManager wsaManager;
|
||||
ThreadPool* IOCPThread_;
|
||||
|
||||
#ifdef _WIN32
|
||||
HANDLE completinPort_;
|
||||
HANDLE completionPort_ = INVALID_HANDLE_VALUE;
|
||||
#elif __linux__
|
||||
|
||||
#endif
|
||||
};
|
||||
|
||||
}
|
||||
@@ -23,17 +23,17 @@ public:
|
||||
int sendto(const void *__buf, size_t __n, int __flags, struct Address __addr);
|
||||
|
||||
Socket(const Socket&) = delete;
|
||||
Socket(Socket&&);
|
||||
Socket(Socket&&) noexcept;
|
||||
Socket& operator=(const Socket&) = delete;
|
||||
Socket& operator=(Socket&&);
|
||||
Socket& operator=(Socket&&) noexcept;
|
||||
|
||||
struct Address bindAddr = {};
|
||||
struct Address remoteAddr = {};
|
||||
|
||||
int domain = 0;
|
||||
SOCKET sock = INVALID_SOCKET;
|
||||
protected:
|
||||
bool valid_ = false;
|
||||
SOCKET sock_ = INVALID_SOCKET;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -39,7 +39,7 @@ public:
|
||||
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>) &&
|
||||
(!std::is_void_v<std::invoke_result_t<_Callable, _Args...>>)
|
||||
Thread(_Callable&& __f, _Args&&... __args) {
|
||||
auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable {
|
||||
auto boundFunc = [this, __f, ... __args = std::move(__args)]() mutable {
|
||||
returnValuePtr = new std::invoke_result_t<_Callable, _Args...>(__f(std::move(__args)...));
|
||||
};
|
||||
std::packaged_task<void()>* funcPtr = new std::packaged_task<void()>(std::move(boundFunc));
|
||||
@@ -53,7 +53,7 @@ public:
|
||||
requires (!std::is_same_v<std::decay_t<_Callable>, Thread>) &&
|
||||
std::is_void_v<std::invoke_result_t<_Callable, _Args...>>
|
||||
Thread(_Callable&& __f, _Args&&... __args) {
|
||||
auto boundFunc = [this, __f = std::move(__f), ... __args = std::move(__args)]() mutable {
|
||||
auto boundFunc = [this, __f, ... __args = std::move(__args)]() mutable {
|
||||
__f(std::move(__args)...);
|
||||
};
|
||||
std::packaged_task<void()>* funcPtr = new std::packaged_task<void()>(std::move(boundFunc));
|
||||
|
||||
@@ -16,6 +16,9 @@ public:
|
||||
~ThreadPool();
|
||||
|
||||
void init(std::uint32_t numThreads);
|
||||
void terminate();
|
||||
|
||||
void respawnWorker(std::uint32_t numThreads);
|
||||
|
||||
template<typename _Callable, typename... _Args>
|
||||
requires (!std::is_void_v<std::invoke_result_t<_Callable, ThreadPool*, _Args...>>)
|
||||
@@ -26,7 +29,7 @@ public:
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
||||
auto boundFunc = [this, &retVal, __job = std::move(__job), ... __args = std::move(__args)]() mutable {
|
||||
auto boundFunc = [this, &retVal, __job, ... __args = std::move(__args)]() mutable {
|
||||
retVal = __job(this, std::move(__args)...);
|
||||
};
|
||||
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
||||
@@ -44,7 +47,7 @@ public:
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(jobQueueMutex);
|
||||
auto boundFunc = [this, __job = std::move(__job), ... __args = std::move(__args)]() mutable {
|
||||
auto boundFunc = [this, __job, ... __args = std::move(__args)]() mutable {
|
||||
__job(this, std::move(__args)...);
|
||||
};
|
||||
auto task = std::packaged_task<void()>(std::move(boundFunc));
|
||||
@@ -54,6 +57,7 @@ public:
|
||||
return 0;
|
||||
}
|
||||
|
||||
int threadCount = 0;
|
||||
private:
|
||||
void* Worker();
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <ws2bth.h>
|
||||
#include <ws2def.h>
|
||||
#include <windows.h>
|
||||
#define in_addr_t ULONG
|
||||
#elif __linux__
|
||||
|
||||
Reference in New Issue
Block a user