자잘한 수정
This commit is contained in:
@@ -51,8 +51,6 @@ int main() {
|
||||
#endif
|
||||
spdlog::debug("PID : {}", pid);
|
||||
|
||||
DWORD recvbytes = 0, flags = 0;
|
||||
|
||||
while (true) {
|
||||
spdlog::info("Waiting for connection...");
|
||||
sock.accept(clientSock, clientAddr);
|
||||
@@ -64,6 +62,7 @@ int main() {
|
||||
ptr->recvbytes = ptr->sendbytes = 0;
|
||||
ptr->wsabuf.buf = ptr->buf;
|
||||
ptr->wsabuf.len = 1500;
|
||||
ptr->IOCPInstance = &iocp;
|
||||
|
||||
iocp.registerSocket(ptr->socket.sock);
|
||||
|
||||
@@ -166,9 +165,8 @@ void _IOCPClient(Chattr::ThreadPool* thread, Chattr::IOCPPASSINDATA* data) {
|
||||
packetError = true;
|
||||
break;
|
||||
}
|
||||
DWORD recvbytes = 0, flags = 0;
|
||||
|
||||
int returnData = WSARecv(data->socket.sock, &data->wsabuf, 1, &recvbytes, &flags, &data->overlapped, NULL);
|
||||
int returnData = data->IOCPInstance->recv(data);
|
||||
}
|
||||
|
||||
void _TCPSendClient(Chattr::ThreadPool* thread, Chattr::TCPSocket sock, std::queue<Chattr::ResponsePacket> packets) {
|
||||
|
||||
@@ -16,13 +16,21 @@ void IOCP::registerSocket(SOCKET sock) {
|
||||
}
|
||||
|
||||
int IOCP::recv(Chattr::IOCPPASSINDATA* data) {
|
||||
#ifdef _WIN32
|
||||
DWORD recvbytes = 0, flags = 0;
|
||||
return ::WSARecv(data->socket.sock, &data->wsabuf, 1, &recvbytes, &flags, &data->overlapped, NULL);
|
||||
#elif __linux__
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int IOCP::send(Chattr::IOCPPASSINDATA* data, int __flags) {
|
||||
#ifdef _WIN32
|
||||
DWORD sendbytes = 0;
|
||||
return ::WSASend(data->socket.sock, &data->wsabuf, 1, &sendbytes, __flags, &data->overlapped, NULL);
|
||||
#elif __linux__
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
@@ -54,7 +54,7 @@ void Socket::set(const SOCKET __sock, int __domain) {
|
||||
valid_ = true;
|
||||
};
|
||||
|
||||
int Chattr::Socket::setsockopt(int level, int optname, const char FAR* optval, int optlen) {
|
||||
int Chattr::Socket::setsockopt(int level, int optname, const char* optval, int optlen) {
|
||||
return ::setsockopt(sock, level, optname, optval, optlen);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,23 @@
|
||||
#include "Socket/Log.hpp"
|
||||
#include <functional>
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
typedef struct _OVERLAPPED {
|
||||
char dummy;
|
||||
} OVERLAPPED;
|
||||
|
||||
typedef struct __WSABUF {
|
||||
std::uint32_t len;
|
||||
char *buf;
|
||||
} WSABUF;
|
||||
|
||||
#endif
|
||||
|
||||
namespace Chattr {
|
||||
|
||||
class IOCP;
|
||||
|
||||
struct IOCPPASSINDATA {
|
||||
OVERLAPPED overlapped;
|
||||
TCPSocket socket;
|
||||
@@ -20,6 +29,7 @@ struct IOCPPASSINDATA {
|
||||
int recvbytes;
|
||||
int sendbytes;
|
||||
WSABUF wsabuf;
|
||||
IOCP* IOCPInstance;
|
||||
};
|
||||
|
||||
class IOCP {
|
||||
@@ -42,21 +52,21 @@ public:
|
||||
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);
|
||||
static void iocpWather(ThreadPool* threadPool, 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
|
||||
|
||||
@@ -83,7 +93,7 @@ public:
|
||||
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);
|
||||
__IOCPThread->enqueueJob(iocpWather, task);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,7 +109,7 @@ private:
|
||||
#ifdef _WIN32
|
||||
HANDLE completionPort_ = INVALID_HANDLE_VALUE;
|
||||
#elif __linux__
|
||||
|
||||
int epollfd = -1;
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ public:
|
||||
|
||||
operator SOCKET();
|
||||
void set(const SOCKET __sock, int __domain);
|
||||
int setsockopt(int level, int optname, const char FAR* optval, int optlen);
|
||||
int setsockopt(int level, int optname, const char* optval, int optlen);
|
||||
|
||||
int bind(Address __addr);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user