From 5507adbe9473f4b41e52d16498f14850773e5e45 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Tue, 27 May 2014 01:01:44 +0200 Subject: SocketHandlers own the poller and add themself into it only when the socket is created MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We want to call socket() with the parameters provided by getaddrinfo, so we can’t addd the fd into the poller immediately. We need to wait the connection attempt, and then the SocketHandler can call add_socket_handler itself, if the connection succeeds, or is in progress. --- src/network/socket_handler.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'src/network/socket_handler.cpp') diff --git a/src/network/socket_handler.cpp b/src/network/socket_handler.cpp index 2348faa..a9e0c5e 100644 --- a/src/network/socket_handler.cpp +++ b/src/network/socket_handler.cpp @@ -23,8 +23,8 @@ using namespace std::string_literals; # define UIO_FASTIOV 8 #endif -SocketHandler::SocketHandler(): - poller(nullptr), +SocketHandler::SocketHandler(std::shared_ptr poller): + poller(poller), connected(false), connecting(false) { @@ -107,6 +107,7 @@ void SocketHandler::connect(const std::string& address, const std::string& port) || errno == EISCONN) { log_info("Connection success."); + this->poller->add_socket_handler(this); this->connected = true; this->connecting = false; this->on_connected(); @@ -134,11 +135,6 @@ void SocketHandler::connect() this->connect(this->address, this->port); } -void SocketHandler::set_poller(Poller* poller) -{ - this->poller = poller; -} - void SocketHandler::on_recv() { static constexpr size_t buf_size = 4096; @@ -231,8 +227,6 @@ void SocketHandler::close() this->port.clear(); this->poller->remove_socket_handler(this->get_socket()); ::close(this->socket); - // recreate the socket for a potential future usage - this->init_socket(); } socket_t SocketHandler::get_socket() const -- cgit v1.2.3