windows 살짝 분리!
This commit is contained in:
@@ -59,7 +59,7 @@ int main() {
|
|||||||
bool enable = true;
|
bool enable = true;
|
||||||
clientSock.setsockopt(SOL_SOCKET, SO_KEEPALIVE, (const char *)&enable, sizeof(enable));
|
clientSock.setsockopt(SOL_SOCKET, SO_KEEPALIVE, (const char *)&enable, sizeof(enable));
|
||||||
Chattr::IOCPPASSINDATA* ptr = new Chattr::IOCPPASSINDATA;
|
Chattr::IOCPPASSINDATA* ptr = new Chattr::IOCPPASSINDATA;
|
||||||
ZeroMemory(&ptr->overlapped, sizeof(OVERLAPPED));
|
::memset(&ptr->overlapped, 0, sizeof(OVERLAPPED));
|
||||||
ptr->socket = std::move(clientSock);
|
ptr->socket = std::move(clientSock);
|
||||||
ptr->recvbytes = ptr->sendbytes = 0;
|
ptr->recvbytes = ptr->sendbytes = 0;
|
||||||
ptr->wsabuf.buf = ptr->buf;
|
ptr->wsabuf.buf = ptr->buf;
|
||||||
@@ -67,7 +67,7 @@ int main() {
|
|||||||
|
|
||||||
iocp.registerSocket(ptr->socket.sock);
|
iocp.registerSocket(ptr->socket.sock);
|
||||||
|
|
||||||
int returnData = WSARecv(ptr->socket.sock, &ptr->wsabuf, 1, &recvbytes, &flags, &ptr->overlapped, NULL);
|
int returnData = iocp.recv(ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,14 @@ void IOCP::registerSocket(SOCKET sock) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int IOCP::recv(void* __restrict __buf, size_t __n, int __flags) {
|
int IOCP::recv(Chattr::IOCPPASSINDATA* data) {
|
||||||
|
DWORD recvbytes = 0, flags = 0;
|
||||||
|
return ::WSARecv(data->socket.sock, &data->wsabuf, 1, &recvbytes, &flags, &data->overlapped, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int IOCP::send(const void* __buf, size_t __n, int __flags) {
|
int IOCP::send(Chattr::IOCPPASSINDATA* data, int __flags) {
|
||||||
|
DWORD sendbytes = 0;
|
||||||
|
return ::WSASend(data->socket.sock, &data->wsabuf, 1, &sendbytes, __flags, &data->overlapped, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -7,6 +7,12 @@
|
|||||||
|
|
||||||
namespace Chattr {
|
namespace Chattr {
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
typedef struct _OVERLAPPED {
|
||||||
|
char dummy;
|
||||||
|
} OVERLAPPED;
|
||||||
|
#endif
|
||||||
|
|
||||||
struct IOCPPASSINDATA {
|
struct IOCPPASSINDATA {
|
||||||
OVERLAPPED overlapped;
|
OVERLAPPED overlapped;
|
||||||
TCPSocket socket;
|
TCPSocket socket;
|
||||||
@@ -18,6 +24,7 @@ struct IOCPPASSINDATA {
|
|||||||
|
|
||||||
class IOCP {
|
class IOCP {
|
||||||
public:
|
public:
|
||||||
|
#ifdef _WIN32
|
||||||
static void iocpWather(ThreadPool* threadPool, HANDLE completionPort_, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
static void iocpWather(ThreadPool* threadPool, HANDLE completionPort_, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||||
DWORD tid = GetCurrentThreadId();
|
DWORD tid = GetCurrentThreadId();
|
||||||
spdlog::debug("Waiting IO to complete on TID: {}.", tid);
|
spdlog::debug("Waiting IO to complete on TID: {}.", tid);
|
||||||
@@ -34,6 +41,24 @@ public:
|
|||||||
threadPool->enqueueJob(callback, data);
|
threadPool->enqueueJob(callback, data);
|
||||||
threadPool->enqueueJob(iocpWather, completionPort_, callback);
|
threadPool->enqueueJob(iocpWather, completionPort_, callback);
|
||||||
};
|
};
|
||||||
|
#elif __linux__
|
||||||
|
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));
|
||||||
|
delete data;
|
||||||
|
threadPool->enqueueJob(iocpWather, completionPort_, callback);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
threadPool->enqueueJob(callback, data);
|
||||||
|
threadPool->enqueueJob(iocpWather, completionPort_, callback);
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
template<typename _Callable>
|
template<typename _Callable>
|
||||||
void init(ThreadPool* __IOCPThread, _Callable&& callback) {
|
void init(ThreadPool* __IOCPThread, _Callable&& callback) {
|
||||||
@@ -42,7 +67,9 @@ public:
|
|||||||
completionPort_ = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
completionPort_ = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
|
||||||
if (completionPort_ == NULL)
|
if (completionPort_ == NULL)
|
||||||
log::critical("CreateIoCompletionPort()");
|
log::critical("CreateIoCompletionPort()");
|
||||||
|
#elif __linux__
|
||||||
|
|
||||||
|
#endif
|
||||||
auto boundFunc = [callback = std::move(callback)](ThreadPool* __IOCPThread, IOCPPASSINDATA* data) mutable {
|
auto boundFunc = [callback = std::move(callback)](ThreadPool* __IOCPThread, IOCPPASSINDATA* data) mutable {
|
||||||
callback(__IOCPThread, data);
|
callback(__IOCPThread, data);
|
||||||
};
|
};
|
||||||
@@ -58,15 +85,12 @@ public:
|
|||||||
std::function<void(ThreadPool*, IOCPPASSINDATA*)> task(boundFunc);
|
std::function<void(ThreadPool*, IOCPPASSINDATA*)> task(boundFunc);
|
||||||
__IOCPThread->enqueueJob(iocpWather, completionPort_, task);
|
__IOCPThread->enqueueJob(iocpWather, completionPort_, task);
|
||||||
}
|
}
|
||||||
#elif __linux__
|
|
||||||
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void registerSocket(SOCKET sock);
|
void registerSocket(SOCKET sock);
|
||||||
|
|
||||||
int recv(void* __restrict __buf, size_t __n, int __flags);
|
int recv(Chattr::IOCPPASSINDATA* data);
|
||||||
int send(const void* __buf, size_t __n, int __flags);
|
int send(Chattr::IOCPPASSINDATA* data, int __flags);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct Chattr::WSAManager wsaManager;
|
struct Chattr::WSAManager wsaManager;
|
||||||
|
|||||||
Reference in New Issue
Block a user