diff options
Diffstat (limited to 'src/Net/Listener.cpp')
-rw-r--r-- | src/Net/Listener.cpp | 53 |
1 files changed, 18 insertions, 35 deletions
diff --git a/src/Net/Listener.cpp b/src/Net/Listener.cpp index 9233a79..ed820d0 100644 --- a/src/Net/Listener.cpp +++ b/src/Net/Listener.cpp @@ -18,7 +18,9 @@ */ #include "Listener.h" +#include "FdManager.h" #include "ServerConnection.h" + #include <cerrno> #include <cstring> #include <fcntl.h> @@ -26,6 +28,19 @@ namespace Mad { namespace Net { +void Listener::acceptHandler(int) { + int sd; + struct sockaddr_in sa; + socklen_t addrlen = sizeof(sa); + + + while((sd = accept(sock, (struct sockaddr*)&sa, &addrlen)) >= 0) { + connections.push_back(new ServerConnection(sd, IPAddress(sa), dh_params, x905CertFile, x905KeyFile)); + + addrlen = sizeof(sa); + } +} + Listener::Listener(const std::string &x905CertFile0, const std::string &x905KeyFile0, const IPAddress &address0) throw(Common::Exception) : x905CertFile(x905CertFile0), x905KeyFile(x905KeyFile0), address(address0) { gnutls_dh_params_init(&dh_params); @@ -62,6 +77,8 @@ Listener::Listener(const std::string &x905CertFile0, const std::string &x905KeyF throw Common::Exception("listen()", Common::Exception::INTERNAL_ERRNO, errno); } + + FdManager::getFdManager()->registerFd(sock, sigc::mem_fun(this, &Listener::acceptHandler), POLLIN); } Listener::~Listener() { @@ -76,43 +93,9 @@ Listener::~Listener() { gnutls_dh_params_deinit(dh_params); } -std::vector<struct pollfd> Listener::getPollfds() const { - std::vector<struct pollfd> pollfds; - - struct pollfd fd = {sock, POLLIN, 0}; - pollfds.push_back(fd); - - for(std::list<ServerConnection*>::const_iterator con = connections.begin(); con != connections.end(); ++con) - pollfds.push_back((*con)->getPollfd()); - - return pollfds; -} - -ServerConnection* Listener::getConnection(const std::map<int,const short*> &pollfdMap) { +ServerConnection* Listener::getConnection() { // TODO: Logging - int sd; - struct sockaddr_in sa; - socklen_t addrlen = sizeof(sa); - - - while((sd = accept(sock, (struct sockaddr*)&sa, &addrlen)) >= 0) { - connections.push_back(new ServerConnection(sd, IPAddress(sa), dh_params, x905CertFile, x905KeyFile)); - - addrlen = sizeof(sa); - } - - for(std::list<ServerConnection*>::iterator con = connections.begin(); con != connections.end(); ++con) { - std::map<int,const short*>::const_iterator events = pollfdMap.find((*con)->getSocket()); - - if(events != pollfdMap.end()) { - if(*events->second) - (*con)->sendReceive(*events->second); - } - else - (*con)->sendReceive(); - } - for(std::list<ServerConnection*>::iterator con = connections.begin(); con != connections.end();) { if(!(*con)->isConnected()) { delete *con; |