diff options
author | Florent Le Coz <louiz@louiz.org> | 2013-12-27 11:53:29 +0100 |
---|---|---|
committer | Florent Le Coz <louiz@louiz.org> | 2014-01-04 01:59:35 +0100 |
commit | aa53276859a5fc80071d24111a311297e058e603 (patch) | |
tree | feac10c8765c161684a558416ef537abfe1707e4 /src/network | |
parent | 483e17020dc4f19223001ab36da0dc48a15a0d3e (diff) | |
download | biboumi-aa53276859a5fc80071d24111a311297e058e603.tar.gz biboumi-aa53276859a5fc80071d24111a311297e058e603.tar.bz2 biboumi-aa53276859a5fc80071d24111a311297e058e603.tar.xz biboumi-aa53276859a5fc80071d24111a311297e058e603.zip |
Keep a "connected" state in the SocketHandler class
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/socket_handler.cpp | 13 | ||||
-rw-r--r-- | src/network/socket_handler.hpp | 2 |
2 files changed, 14 insertions, 1 deletions
diff --git a/src/network/socket_handler.cpp b/src/network/socket_handler.cpp index c1ad8ae..484aa8f 100644 --- a/src/network/socket_handler.cpp +++ b/src/network/socket_handler.cpp @@ -14,7 +14,8 @@ #include <iostream> SocketHandler::SocketHandler(): - poller(nullptr) + poller(nullptr), + connected(false) { if ((this->socket = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) throw std::runtime_error("Could not create socket"); @@ -46,6 +47,7 @@ bool SocketHandler::connect(const std::string& address, const std::string& port) if (::connect(this->socket, rp->ai_addr, rp->ai_addrlen) == 0) { log_info("Connection success."); + this->connected = true; this->on_connected(); return true; } @@ -99,8 +101,12 @@ void SocketHandler::on_send() void SocketHandler::close() { + this->connected = false; this->poller->remove_socket_handler(this->get_socket()); ::close(this->socket); + // recreate the socket for a potential future usage + if ((this->socket = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) + throw std::runtime_error("Could not create socket"); } socket_t SocketHandler::get_socket() const @@ -116,3 +122,8 @@ void SocketHandler::send_data(std::string&& data) this->poller->watch_send_events(this); } } + +bool SocketHandler::is_connected() const +{ + return this->connected; +} diff --git a/src/network/socket_handler.hpp b/src/network/socket_handler.hpp index 675d247..ec63ea7 100644 --- a/src/network/socket_handler.hpp +++ b/src/network/socket_handler.hpp @@ -61,6 +61,7 @@ public: * should be truncated, only the unused data should be left untouched. */ virtual void parse_in_buffer() = 0; + bool is_connected() const; protected: socket_t socket; @@ -84,6 +85,7 @@ protected: * (actually it is sharing our ownership with a Bridge). */ Poller* poller; + bool connected; private: SocketHandler(const SocketHandler&) = delete; |