다중 접속시에 문제가 생김
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "Packet/Packet.hpp"
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <queue>
|
||||
|
||||
#include "precomp.hpp"
|
||||
|
||||
@@ -42,6 +43,9 @@ struct IOCPPASSINDATA {
|
||||
std::uint32_t transferredbytes;
|
||||
WSABUF wsabuf;
|
||||
IOCP* IOCPInstance;
|
||||
#ifdef __linux__
|
||||
std::shared_ptr<std::queue<IOCPPASSINDATA*>> sendQueue;
|
||||
#endif
|
||||
};
|
||||
|
||||
class IOCP {
|
||||
@@ -66,100 +70,110 @@ public:
|
||||
};
|
||||
#elif __linux__
|
||||
static void socketReader(ThreadPool* threadPool, epoll_event event, int epollfd, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||
IOCPPASSINDATA* iocpData = (IOCPPASSINDATA*)event.data.ptr;
|
||||
|
||||
pthread_t tid = pthread_self();
|
||||
|
||||
if (iocpData == nullptr) {
|
||||
if (event.data.ptr == nullptr) {
|
||||
spdlog::error("invalid call on {}", tid);
|
||||
return;
|
||||
}
|
||||
|
||||
IOCPPASSINDATA* rootIocpData = (IOCPPASSINDATA*)event.data.ptr;
|
||||
|
||||
std::lock_guard<std::mutex> lock(iocpData->socket->readMutex);
|
||||
std::lock_guard<std::mutex> lock(rootIocpData->socket->readMutex);
|
||||
char peekBuffer[1];
|
||||
while (rootIocpData->socket->recv(peekBuffer, 1, MSG_PEEK)) {
|
||||
Chattr::IOCPPASSINDATA* ptr = new Chattr::IOCPPASSINDATA;
|
||||
::memcpy(ptr, rootIocpData, sizeof(IOCPPASSINDATA));
|
||||
::memset(&ptr->overlapped, 0, sizeof(OVERLAPPED));
|
||||
ptr->recvbytes = ptr->sendbytes = 0;
|
||||
ptr->wsabuf.buf = ptr->buf;
|
||||
ptr->wsabuf.len = 1500;
|
||||
|
||||
spdlog::trace("reading on tid: {} [{}]", tid, (std::string)iocpData->socket->remoteAddr);
|
||||
int redSize = 0;
|
||||
int headerSize = 8;
|
||||
int totalRedSize = 0;
|
||||
|
||||
int redSize = 0;
|
||||
int headerSize = 8;
|
||||
int totalRedSize = 0;
|
||||
|
||||
while (totalRedSize < headerSize) {
|
||||
redSize = iocpData->socket->recv(iocpData->buf + totalRedSize, headerSize - totalRedSize, 0);
|
||||
|
||||
if (redSize == SOCKET_ERROR) {
|
||||
spdlog::error("recv() [{}]", strerror(errno));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket->sock, NULL);
|
||||
delete iocpData;
|
||||
return;
|
||||
while (totalRedSize < headerSize) {
|
||||
redSize = ptr->socket->recv(ptr->buf + totalRedSize, headerSize - totalRedSize, 0);
|
||||
|
||||
if (redSize == SOCKET_ERROR) {
|
||||
spdlog::error("recv() [{}]", strerror(errno));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, ptr->socket->sock, NULL);
|
||||
delete ptr;
|
||||
return;
|
||||
}
|
||||
else if (redSize == 0) {
|
||||
spdlog::debug("Client disconnected. [{}]", (std::string)ptr->socket->remoteAddr);
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, ptr->socket->sock, NULL);
|
||||
delete ptr;
|
||||
return;
|
||||
}
|
||||
totalRedSize += redSize;
|
||||
}
|
||||
else if (redSize == 0) {
|
||||
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket->remoteAddr);
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket->sock, NULL);
|
||||
delete iocpData;
|
||||
return;
|
||||
|
||||
Packet packet;
|
||||
::memcpy(packet.serialized, ptr->buf, headerSize);
|
||||
|
||||
redSize = 0;
|
||||
int dataLength = ntohs(packet.__data.packetLength);
|
||||
|
||||
while (totalRedSize < dataLength + headerSize) {
|
||||
redSize = ptr->socket->recv(ptr->buf + totalRedSize, dataLength + headerSize - totalRedSize, 0);
|
||||
|
||||
if (redSize == SOCKET_ERROR) {
|
||||
spdlog::error("recv() [{}]", strerror(errno));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, ptr->socket->sock, NULL);
|
||||
delete ptr;
|
||||
return;
|
||||
}
|
||||
else if (redSize == 0) {
|
||||
spdlog::debug("Client disconnected. [{}]", (std::string)ptr->socket->remoteAddr);
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, ptr->socket->sock, NULL);
|
||||
delete ptr;
|
||||
return;
|
||||
}
|
||||
totalRedSize += redSize;
|
||||
}
|
||||
totalRedSize += redSize;
|
||||
ptr->transferredbytes = totalRedSize;
|
||||
threadPool->enqueueJob(callback, ptr);
|
||||
}
|
||||
|
||||
Packet packet;
|
||||
::memcpy(packet.serialized, iocpData->buf, headerSize);
|
||||
|
||||
redSize = 0;
|
||||
int dataLength = ntohs(packet.__data.packetLength);
|
||||
|
||||
while (totalRedSize < dataLength + headerSize) {
|
||||
redSize = iocpData->socket->recv(iocpData->buf + totalRedSize, dataLength + headerSize - totalRedSize, 0);
|
||||
|
||||
if (redSize == SOCKET_ERROR) {
|
||||
spdlog::error("recv() [{}]", strerror(errno));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket->sock, NULL);
|
||||
delete iocpData;
|
||||
return;
|
||||
}
|
||||
else if (redSize == 0) {
|
||||
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket->remoteAddr);
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket->sock, NULL);
|
||||
delete iocpData;
|
||||
return;
|
||||
}
|
||||
totalRedSize += redSize;
|
||||
}
|
||||
iocpData->transferredbytes = totalRedSize;
|
||||
threadPool->enqueueJob(callback, iocpData);
|
||||
};
|
||||
static void socketWriter(ThreadPool* threadPool, epoll_event event, int epollfd, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||
IOCPPASSINDATA* iocpData = (IOCPPASSINDATA*)event.data.ptr;
|
||||
|
||||
pthread_t tid = pthread_self();
|
||||
|
||||
if (iocpData == nullptr) {
|
||||
if (event.data.ptr == nullptr) {
|
||||
spdlog::error("invalid call on {}", tid);
|
||||
return;
|
||||
}
|
||||
|
||||
IOCPPASSINDATA* rootIocpData = (IOCPPASSINDATA*)event.data.ptr;
|
||||
|
||||
std::lock_guard<std::mutex> lock(iocpData->socket->writeMutex);
|
||||
std::lock_guard<std::mutex> lock(rootIocpData->socket->writeMutex);
|
||||
auto queue = rootIocpData->sendQueue;
|
||||
while (!queue->empty()) {
|
||||
IOCPPASSINDATA* data = queue->front();
|
||||
queue->pop();
|
||||
|
||||
spdlog::trace("Writing on tid: {} [{}]", tid, (std::string)iocpData->socket->remoteAddr);
|
||||
int packetSize = data->wsabuf.len;
|
||||
int totalSentSize = 0;
|
||||
int sentSize = 0;
|
||||
|
||||
int packetSize = iocpData->wsabuf.len;
|
||||
int totalSentSize = 0;
|
||||
int sentSize = 0;
|
||||
spdlog::trace("Sending to: [{}]", (std::string)data->socket->remoteAddr);
|
||||
|
||||
spdlog::trace("Sending to: [{}]", (std::string)iocpData->socket->remoteAddr);
|
||||
while (totalSentSize < packetSize) {
|
||||
sentSize = data->socket->send(data->buf + totalSentSize, packetSize - totalSentSize, 0);
|
||||
|
||||
while (totalSentSize < packetSize) {
|
||||
sentSize = iocpData->socket->send(iocpData->buf + totalSentSize, packetSize - totalSentSize, 0);
|
||||
|
||||
if (sentSize == SOCKET_ERROR) {
|
||||
spdlog::error("send() [{}]", strerror(errno));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket->sock, NULL);
|
||||
delete iocpData;
|
||||
return;
|
||||
if (sentSize == SOCKET_ERROR) {
|
||||
spdlog::error("send() [{}]", strerror(errno));
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, data->socket->sock, NULL);
|
||||
delete data;
|
||||
return;
|
||||
}
|
||||
totalSentSize += sentSize;
|
||||
}
|
||||
totalSentSize += sentSize;
|
||||
data->transferredbytes = totalSentSize;
|
||||
threadPool->enqueueJob(callback, data);
|
||||
}
|
||||
iocpData->transferredbytes = totalSentSize;
|
||||
threadPool->enqueueJob(callback, iocpData);
|
||||
};
|
||||
static void iocpWatcher(ThreadPool* threadPool, int epollfd, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||
struct epoll_event events[FD_SETSIZE];
|
||||
|
||||
Reference in New Issue
Block a user