summaryrefslogtreecommitdiff
path: root/src/network/socket_handler.cpp
diff options
context:
space:
mode:
authorFlorent Le Coz <louiz@louiz.org>2014-05-27 01:01:44 +0200
committerFlorent Le Coz <louiz@louiz.org>2014-05-27 01:01:44 +0200
commit5507adbe9473f4b41e52d16498f14850773e5e45 (patch)
tree34f2960edf6b73828537460cc50e6cdb9252a5e3 /src/network/socket_handler.cpp
parent6b0ffb5fc2eca537e2cfaf24acb8a4d2ca9b99f1 (diff)
downloadbiboumi-5507adbe9473f4b41e52d16498f14850773e5e45.tar.gz
biboumi-5507adbe9473f4b41e52d16498f14850773e5e45.tar.bz2
biboumi-5507adbe9473f4b41e52d16498f14850773e5e45.tar.xz
biboumi-5507adbe9473f4b41e52d16498f14850773e5e45.zip
SocketHandlers own the poller and add themself into it only when the socket is created
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.
Diffstat (limited to 'src/network/socket_handler.cpp')
-rw-r--r--src/network/socket_handler.cpp12
1 files changed, 3 insertions, 9 deletions
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(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