summaryrefslogtreecommitdiff
path: root/src/network
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
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')
-rw-r--r--src/network/poller.cpp9
-rw-r--r--src/network/poller.hpp4
-rw-r--r--src/network/socket_handler.cpp12
-rw-r--r--src/network/socket_handler.hpp9
4 files changed, 12 insertions, 22 deletions
diff --git a/src/network/poller.cpp b/src/network/poller.cpp
index 708fe23..d89e50f 100644
--- a/src/network/poller.cpp
+++ b/src/network/poller.cpp
@@ -27,15 +27,14 @@ Poller::~Poller()
{
}
-void Poller::add_socket_handler(std::shared_ptr<SocketHandler> socket_handler)
+void Poller::add_socket_handler(SocketHandler* socket_handler)
{
- // Raise an error if that socket is already in the list
+ // Don't do anything if the socket is already managed
const auto it = this->socket_handlers.find(socket_handler->get_socket());
if (it != this->socket_handlers.end())
- throw std::runtime_error("Trying to insert SocketHandler already managed");
+ return ;
this->socket_handlers.emplace(socket_handler->get_socket(), socket_handler);
- socket_handler->set_poller(this);
// We always watch all sockets for receive events
#if POLLER == POLL
@@ -44,7 +43,7 @@ void Poller::add_socket_handler(std::shared_ptr<SocketHandler> socket_handler)
this->nfds++;
#endif
#if POLLER == EPOLL
- struct epoll_event event = {EPOLLIN, {socket_handler.get()}};
+ struct epoll_event event = {EPOLLIN, {socket_handler}};
const int res = ::epoll_ctl(this->epfd, EPOLL_CTL_ADD, socket_handler->get_socket(), &event);
if (res == -1)
{
diff --git a/src/network/poller.hpp b/src/network/poller.hpp
index dc087a2..c3edcd7 100644
--- a/src/network/poller.hpp
+++ b/src/network/poller.hpp
@@ -42,7 +42,7 @@ public:
* Add a SocketHandler to be monitored by this Poller. All receive events
* are always automatically watched.
*/
- void add_socket_handler(std::shared_ptr<SocketHandler> socket_handler);
+ void add_socket_handler(SocketHandler* socket_handler);
/**
* Remove (and stop managing) a SocketHandler, designed by the given socket_t.
*/
@@ -77,7 +77,7 @@ private:
* because that's what is returned by select/poll/etc when an event
* occures.
*/
- std::unordered_map<socket_t, std::shared_ptr<SocketHandler>> socket_handlers;
+ std::unordered_map<socket_t, SocketHandler*> socket_handlers;
#if POLLER == POLL
struct pollfd fds[MAX_POLL_FD_NUMBER];
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
diff --git a/src/network/socket_handler.hpp b/src/network/socket_handler.hpp
index f554350..1e31dcd 100644
--- a/src/network/socket_handler.hpp
+++ b/src/network/socket_handler.hpp
@@ -6,6 +6,7 @@
#include <netdb.h>
#include <utility>
+#include <memory>
#include <string>
#include <list>
@@ -22,7 +23,7 @@ class Poller;
class SocketHandler
{
public:
- explicit SocketHandler();
+ explicit SocketHandler(std::shared_ptr<Poller> poller);
virtual ~SocketHandler() {}
/**
* (re-)Initialize the socket
@@ -34,10 +35,6 @@ public:
void connect(const std::string& address, const std::string& port);
void connect();
/**
- * Set the pointer to the given Poller, to communicate with it.
- */
- void set_poller(Poller* poller);
- /**
* Reads data in our in_buf and the call parse_in_buf, for the implementor
* to handle the data received so far.
*/
@@ -119,7 +116,7 @@ protected:
* And a raw pointer because we are not owning it, it is owning us
* (actually it is sharing our ownership with a Bridge).
*/
- Poller* poller;
+ std::shared_ptr<Poller> poller;
/**
* Hostname we are connected/connecting to
*/