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