일단 거의 모든 버그 다 고칢..

This commit is contained in:
2025-05-02 11:16:25 +09:00
parent 6622cc8939
commit 14c325c428
10 changed files with 212 additions and 99 deletions

View File

@@ -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
};
}