This commit is contained in:
2025-05-09 21:47:00 +09:00
parent 78f2bfe2f6
commit 0aa312f67e
6 changed files with 41 additions and 59 deletions

View File

@@ -141,6 +141,10 @@ public:
redSize = ptr->socket->recv(ptr->buf + totalRedSize, dataLength + headerSize - totalRedSize, 0);
if (redSize == SOCKET_ERROR) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
spdlog::trace("No data to read on {}", tid);
return;
}
spdlog::error("recv() [{}]", strerror(errno));
::epoll_ctl(epollfd, EPOLL_CTL_DEL, ptr->socket->sock, NULL);
delete ptr;
@@ -169,10 +173,14 @@ public:
IOCPPASSINDATA* rootIocpData = (IOCPPASSINDATA*)event.data.ptr;
std::lock_guard<std::mutex> lock(rootIocpData->socket->writeMutex);
auto queue = rootIocpData->sendQueue;
while (!queue->empty()) {
IOCPPASSINDATA* data = queue->front();
queue->pop();
while (!rootIocpData->sendQueue->empty()) {
IOCPPASSINDATA* data = rootIocpData->sendQueue->front();
rootIocpData->sendQueue->pop();
if (data == nullptr) {
spdlog::error("invalid call on {}", tid);
break;
}
int packetSize = data->wsabuf.len;
int totalSentSize = 0;
@@ -184,6 +192,10 @@ public:
sentSize = data->socket->send(data->buf + totalSentSize, packetSize - totalSentSize, 0);
if (sentSize == SOCKET_ERROR) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
spdlog::warn("buffer full");
continue;
}
spdlog::error("send() [{}]", strerror(errno));
::epoll_ctl(epollfd, EPOLL_CTL_DEL, data->socket->sock, NULL);
delete data;
@@ -199,8 +211,7 @@ public:
struct epoll_event events[FD_SETSIZE];
pthread_t tid = pthread_self();
spdlog::trace("epoll waiting on {}", tid);
int nready = ::epoll_wait(epollfd, events, FD_SETSIZE, 1000);
int nready = ::epoll_wait(epollfd, events, FD_SETSIZE, -1);
for (int i=0; i<nready; i++) {
struct epoll_event current_event = events[i];
@@ -232,6 +243,8 @@ public:
log::critical("CreateIoCompletionPort()");
#elif __linux__
epollfd_ = ::epoll_create(1);
epollDetroyerFd_ = ::eventfd(0, EFD_NONBLOCK);
::epoll_ctl(epollfd_, EPOLL_CTL_ADD, epollDetroyerFd_, NULL);
#endif
auto boundFunc = [callback = std::move(callback)](ThreadPool* __IOCPThread, IOCPPASSINDATA* data) mutable {
callback(__IOCPThread, data);
@@ -270,6 +283,7 @@ private:
HANDLE completionPort_ = INVALID_HANDLE_VALUE;
#elif __linux__
int epollfd_ = -1;
int epollDetroyerFd_ = -1;
std::unordered_map<std::shared_ptr<TCPSocket>, std::mutex> writeMutex;
std::unordered_map<std::shared_ptr<TCPSocket>, std::queue<IOCPPASSINDATA*>> writeBuffer;
#endif

View File

@@ -15,6 +15,7 @@
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/eventfd.h>
#define SOCKET int
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1