diff options
Diffstat (limited to 'src/Net/Listener.cpp')
-rw-r--r-- | src/Net/Listener.cpp | 66 |
1 files changed, 38 insertions, 28 deletions
diff --git a/src/Net/Listener.cpp b/src/Net/Listener.cpp index 95147fa..5bcc353 100644 --- a/src/Net/Listener.cpp +++ b/src/Net/Listener.cpp @@ -21,10 +21,14 @@ #include "FdManager.h" #include "ServerConnection.h" +#include <Common/Logger.h> + #include <cerrno> #include <cstring> #include <fcntl.h> +#include <sigc++/bind.h> + namespace Mad { namespace Net { @@ -35,12 +39,42 @@ void Listener::acceptHandler(int) { while((sd = accept(sock, (struct sockaddr*)&sa, &addrlen)) >= 0) { - connections.push_back(new ServerConnection(sd, IPAddress(sa), dh_params, x905CertFile, x905KeyFile)); + ServerConnection *con = new ServerConnection(sd, IPAddress(sa), dh_params, x905CertFile, x905KeyFile); + sigc::connection con1 = con->signalConnected().connect(sigc::bind(sigc::mem_fun(this, &Listener::connectHandler), con)); + sigc::connection con2 = con->signalDisconnected().connect(sigc::bind(sigc::mem_fun(this, &Listener::disconnectHandler), con)); + + connections.insert(std::make_pair(con, std::make_pair(con1, con2))); addrlen = sizeof(sa); } } + +void Listener::connectHandler(ServerConnection *con) { + std::map<ServerConnection*, std::pair<sigc::connection, sigc::connection> >::iterator it = connections.find(con); + + if(it == connections.end()) + return; + + // Disconnect signal handlers + it->second.first.disconnect(); + it->second.second.disconnect(); + connections.erase(it); + + signal(con); +} + +void Listener::disconnectHandler(ServerConnection *con) { + std::map<ServerConnection*, std::pair<sigc::connection, sigc::connection> >::iterator it = connections.find(con); + + if(it == connections.end()) + return; + + delete it->first; + connections.erase(it); +} + + Listener::Listener(const std::string &x905CertFile0, const std::string &x905KeyFile0, const IPAddress &address0) throw(Exception) : x905CertFile(x905CertFile0), x905KeyFile(x905KeyFile0), address(address0) { gnutls_dh_params_init(&dh_params); @@ -82,9 +116,9 @@ Listener::Listener(const std::string &x905CertFile0, const std::string &x905KeyF } Listener::~Listener() { - for(std::list<ServerConnection*>::iterator con = connections.begin(); con != connections.end(); ++con) { - (*con)->disconnect(); - delete *con; + for(std::map<ServerConnection*,std::pair<sigc::connection, sigc::connection> >::iterator con = connections.begin(); con != connections.end(); ++con) { + con->first->disconnect(); + delete con->first; } shutdown(sock, SHUT_RDWR); @@ -93,29 +127,5 @@ Listener::~Listener() { gnutls_dh_params_deinit(dh_params); } -ServerConnection* Listener::getConnection() { - // TODO: Logging - - for(std::list<ServerConnection*>::iterator con = connections.begin(); con != connections.end();) { - if(!(*con)->isConnected()) { - delete *con; - connections.erase(con++); // Erase unincremented iterator - - continue; - } - - if(!(*con)->isConnecting()) { - ServerConnection *connection = *con; - connections.erase(con); - - return connection; - } - - ++con; - } - - return 0; -} - } } |