diff --git a/main.cpp b/main.cpp index e19d939..1cd4ae2 100644 --- a/main.cpp +++ b/main.cpp @@ -100,7 +100,7 @@ int main(int argc, char **) { } else { myprintf("connection open\n"); myprintf("wait 10s\n"); - std::this_thread::sleep_for( std::chrono::seconds(2) ); + std::this_thread::sleep_for( std::chrono::seconds(10) ); //fgetc(stdin); myprintf("close connection\n"); connection.closeWait(); diff --git a/protocol.cpp b/protocol.cpp index ed20e23..f3cac23 100644 --- a/protocol.cpp +++ b/protocol.cpp @@ -17,7 +17,7 @@ #define DebugMSG HideDebugMSG #define SrvDebugMSG HideDebugMSG -#define ConnDebugMSG HideDebugMSG +#define ConnDebugMSG ShowDebugMSG @@ -139,7 +139,7 @@ void Protocol::wakeup() { void Protocol::updateEvents(Connection &connection) { - unsigned int events = EPOLLERR; + unsigned int events = EPOLLERR | EPOLLRDHUP; if (connection.readQueue.count()) events |= EPOLLIN; if (connection.writeQueue.count()) events |= EPOLLOUT; @@ -192,7 +192,7 @@ void Protocol::threadConnQueue() { struct epoll_event event = {}; if (sockId >= 0) { event.data.ptr = &connection; - event.events |= EPOLLERR; + event.events = EPOLLERR | EPOLLRDHUP; if (!connected) event.events |= EPOLLOUT; if (0 != epoll_ctl(epollFd, EPOLL_CTL_ADD, sockId, &event)) { ConnDebugMSG("epoll_ctl(%d, ADD, %d, %08x) errno: %d", epollFd, sockId, event.events, errno); @@ -340,10 +340,13 @@ void Protocol::threadConnEvents(Connection &connection, unsigned int events) { || connection.stateLocal >= Connection::STATE_CLOSING ) return; + bool closing = (bool)(events & (EPOLLHUP | EPOLLRDHUP | EPOLLERR)); + if (events & EPOLLIN) { ConnDebugMSG("input"); bool ready = true; int pops = 0; + int count = (connection.readQueue.count() + 2)*2; while(ready) { Connection::Task *task = connection.readQueue.peek(); if (!task) { @@ -375,11 +378,12 @@ void Protocol::threadConnEvents(Connection &connection, unsigned int events) { connection.readQueue.pop(); if (!connection.onReadReady(*task, false)) connection.readQueue.unpop(*task); else ++pops; + if (!closing && --count < 0) ready = false; } } } - if (events & (EPOLLHUP | EPOLLERR)) { + if (closing) { ConnDebugMSG("hup"); connection.close((bool)(events & EPOLLERR)); return;