close socket as fast as possible if it was closed by the client

This commit is contained in:
john30
2015-01-25 18:50:12 +01:00
parent 0095c5d44d
commit 16c52e9a39
2 changed files with 25 additions and 19 deletions
+24 -18
View File
@@ -44,6 +44,8 @@ void Connection::run()
// set timeout // set timeout
tdiff.tv_sec = 2; tdiff.tv_sec = 2;
tdiff.tv_nsec = 0; tdiff.tv_nsec = 0;
int notifyFD = m_notify.notifyFD();
int sockFD = m_socket->getFD();
#ifdef HAVE_PPOLL #ifdef HAVE_PPOLL
int nfds = 2; int nfds = 2;
@@ -51,29 +53,30 @@ void Connection::run()
memset(fds, 0, sizeof(fds)); memset(fds, 0, sizeof(fds));
fds[0].fd = m_notify.notifyFD(); fds[0].fd = notifyFD;
fds[0].events = POLLIN; fds[0].events = POLLIN | POLLERR | POLLHUP | POLLRDHUP;
fds[1].fd = m_socket->getFD(); fds[1].fd = sockFD;
fds[1].events = POLLIN; fds[1].events = POLLIN | POLLERR | POLLHUP | POLLRDHUP;
#else #else
#ifdef HAVE_PSELECT #ifdef HAVE_PSELECT
int maxfd; int maxfd = (notifyFD > sockFD) ? notifyFD : sockFD;
fd_set checkfds; fd_set checkfds, exceptfds;
FD_ZERO(&checkfds); FD_ZERO(&checkfds);
FD_SET(m_notify.notifyFD(), &checkfds); FD_SET(notifyFD, &checkfds);
FD_SET(m_socket->getFD(), &checkfds); FD_SET(sockFD, &checkfds);
(m_notify.notifyFD() > m_socket->getFD()) ? FD_ZERO(&exceptfds);
(maxfd = m_notify.notifyFD()) : (maxfd = m_socket->getFD()); FD_SET(notifyFD, &exceptfds);
FD_SET(sockFD, &exceptfds);
#endif #endif
#endif #endif
time_t listenSince = 0; time_t listenSince = 0;
bool closed = false;
for (;;) { while (closed == false) {
#ifdef HAVE_PPOLL #ifdef HAVE_PPOLL
// wait for new fd event // wait for new fd event
ret = ppoll(fds, nfds, &tdiff, NULL); ret = ppoll(fds, nfds, &tdiff, NULL);
@@ -82,27 +85,28 @@ void Connection::run()
// set readfds to inital checkfds // set readfds to inital checkfds
fd_set readfds = checkfds; fd_set readfds = checkfds;
// wait for new fd event // wait for new fd event
ret = pselect(maxfd + 1, &readfds, NULL, NULL, &tdiff, NULL); ret = pselect(maxfd + 1, &readfds, NULL, &exceptfds, &tdiff, NULL);
#endif #endif
#endif #endif
bool newData = false; bool newData = false;
if (ret != 0) { if (ret != 0) {
#ifdef HAVE_PPOLL #ifdef HAVE_PPOLL
// new data from notify // new data from notify
if (fds[0].revents & POLLIN) if (ret < 0 || (fds[0].revents & (POLLIN | POLLERR | POLLHUP | POLLRDHUP)) || (fds[1].revents & (POLLERR | POLLHUP))) {
break; break;
}
// new data from socket // new data from socket
newData = fds[1].revents & POLLIN; newData = fds[1].revents & POLLIN;
closed = fds[1].revents & POLLRDHUP;
#else #else
#ifdef HAVE_PSELECT #ifdef HAVE_PSELECT
// new data from notify // new data from notify
if (FD_ISSET(m_notify.notifyFD(), &readfds)) if (ret < 0 || FD_ISSET(notifyFD, &readfds) || FD_ISSET(notifyFD, &exceptfds))
break; break;
// new data from socket // new data from socket
newData = FD_ISSET(m_socket->getFD(), &readfds); newData = FD_ISSET(sockFD, &readfds);
closed = FD_ISSET(sockFD, &exceptfds);
#endif #endif
#endif #endif
} }
@@ -140,6 +144,8 @@ void Connection::run()
} }
delete m_socket;
m_socket = NULL;
L.log(net, trace, "[%05d] connection closed", getID()); L.log(net, trace, "[%05d] connection closed", getID());
} }
+1 -1
View File
@@ -156,7 +156,7 @@ public:
: m_socket(socket), m_netQueue(netQueue), m_listening(false) : m_socket(socket), m_netQueue(netQueue), m_listening(false)
{ m_id = ++m_ids; } { m_id = ++m_ids; }
virtual ~Connection() { delete m_socket; } virtual ~Connection() { if (m_socket) delete m_socket; }
/** /**
* endless loop for connection instance. * endless loop for connection instance.
*/ */