Exit the poller when it handles no connection at all

This commit is contained in:
Florent Le Coz
2013-11-03 17:55:23 +01:00
parent f2f94618fc
commit d834d6ed06
4 changed files with 10 additions and 4 deletions
-1
View File
@@ -21,7 +21,6 @@ void IrcClient::on_connected()
void IrcClient::on_connection_close() void IrcClient::on_connection_close()
{ {
std::cout << "Connection closed by remote server." << std::endl; std::cout << "Connection closed by remote server." << std::endl;
this->close();
} }
void IrcClient::parse_in_buffer() void IrcClient::parse_in_buffer()
+4 -1
View File
@@ -89,9 +89,11 @@ void Poller::stop_watching_send_events(const SocketHandler* const socket_handler
throw std::runtime_error("Cannot watch a non-registered socket for send events"); throw std::runtime_error("Cannot watch a non-registered socket for send events");
} }
void Poller::poll() bool Poller::poll()
{ {
#if POLLER == POLL #if POLLER == POLL
if (this->nfds == 0)
return false;
int res = ::poll(this->fds, this->nfds, -1); int res = ::poll(this->fds, this->nfds, -1);
if (res < 0) if (res < 0)
{ {
@@ -119,4 +121,5 @@ void Poller::poll()
} }
} }
#endif #endif
return true;
} }
+2 -1
View File
@@ -57,8 +57,9 @@ public:
/** /**
* Wait for all watched events, and call the SocketHandlers' callbacks * Wait for all watched events, and call the SocketHandlers' callbacks
* when one is ready. * when one is ready.
* Returns false if there are 0 SocketHandler in the list.
*/ */
void poll(); bool poll();
private: private:
/** /**
+3
View File
@@ -66,7 +66,10 @@ void SocketHandler::on_recv()
ssize_t size = ::recv(this->socket, buf, 4096, 0); ssize_t size = ::recv(this->socket, buf, 4096, 0);
if (0 == size) if (0 == size)
{
this->on_connection_close(); this->on_connection_close();
this->close();
}
else if (-1 == static_cast<ssize_t>(size)) else if (-1 == static_cast<ssize_t>(size))
throw std::runtime_error("Error reading from socket"); throw std::runtime_error("Error reading from socket");
else else