둘 다 동시 디버깅을 위해 push
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include "Socket/WSAManager.hpp"
|
||||
#include "Socket/TCPSocket.hpp"
|
||||
#include "Socket/Log.hpp"
|
||||
#include "Packet/Packet.hpp"
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
@@ -31,7 +32,7 @@ struct IOCPPASSINDATA {
|
||||
char buf[1501];
|
||||
std::uint32_t recvbytes;
|
||||
std::uint32_t sendbytes;
|
||||
std::uint32_t transfrredbytes;
|
||||
std::uint32_t transferredbytes;
|
||||
WSABUF wsabuf;
|
||||
IOCP* IOCPInstance;
|
||||
};
|
||||
@@ -67,49 +68,95 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
spdlog::trace("reading on tid: {} [{}]", tid, (std::string)iocpData->socket.remoteAddr);
|
||||
std::lock_guard<std::mutex> lock(iocpData->socket->readMutex);
|
||||
|
||||
spdlog::trace("reading on tid: {} [{}]", tid, (std::string)iocpData->socket->remoteAddr);
|
||||
|
||||
int redSize = 0;
|
||||
int packetSize = iocpData->wsabuf.len;
|
||||
int headerSize = 8;
|
||||
int totalRedSize = 0;
|
||||
|
||||
while (totalRedSize < packetSize) {
|
||||
redSize = iocpData->socket.recv(iocpData->buf, packetSize - totalRedSize, 0);
|
||||
while (totalRedSize < headerSize) {
|
||||
redSize = iocpData->socket->recv(iocpData->buf, headerSize - totalRedSize, 0);
|
||||
|
||||
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;
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
spdlog::trace("{}", strerror(errno));
|
||||
break;
|
||||
} else {
|
||||
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->sendbytes = packetSize - totalRedSize;
|
||||
iocpData->recvbytes = totalRedSize;
|
||||
|
||||
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 <= 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
spdlog::trace("{}", strerror(errno));
|
||||
break;
|
||||
} else {
|
||||
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();
|
||||
|
||||
int packetSize = iocpData->wsabuf.len;
|
||||
if (iocpData == nullptr) {
|
||||
spdlog::error("invalid call on {}", tid);
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(iocpData->socket->writeMutex);
|
||||
|
||||
spdlog::trace("Writing on tid: {} [{}]", tid, (std::string)iocpData->socket->remoteAddr);
|
||||
|
||||
int packetSize = iocpData->transferredbytes;
|
||||
int totalSentSize = 0;
|
||||
int sentSize = 0;
|
||||
|
||||
spdlog::trace("Sending to: [{}]", (std::string)iocpData->socket.remoteAddr);
|
||||
spdlog::trace("Sending to: [{}]", (std::string)iocpData->socket->remoteAddr);
|
||||
|
||||
while (totalSentSize < packetSize) {
|
||||
sentSize = iocpData->socket.send(iocpData->buf, packetSize - totalSentSize, 0);
|
||||
sentSize = iocpData->socket->send(iocpData->buf, packetSize - totalSentSize, 0);
|
||||
|
||||
if (sentSize <= 0) {
|
||||
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket.remoteAddr);
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket.sock, NULL);
|
||||
delete iocpData;
|
||||
return;
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
spdlog::trace("{}", strerror(errno));
|
||||
break;
|
||||
} else {
|
||||
spdlog::debug("Client disconnected. [{}]", (std::string)iocpData->socket->remoteAddr);
|
||||
::epoll_ctl(epollfd, EPOLL_CTL_DEL, iocpData->socket->sock, NULL);
|
||||
delete iocpData;
|
||||
return;
|
||||
}
|
||||
}
|
||||
totalSentSize += sentSize;
|
||||
}
|
||||
iocpData->recvbytes = packetSize - totalSentSize;
|
||||
iocpData->sendbytes = totalSentSize;
|
||||
iocpData->transferredbytes = totalSentSize;
|
||||
threadPool->enqueueJob(callback, iocpData);
|
||||
};
|
||||
static void iocpWatcher(ThreadPool* threadPool, int epollfd, std::function<void(ThreadPool*, IOCPPASSINDATA*)> callback) {
|
||||
|
||||
Reference in New Issue
Block a user