]> git.tdb.fi Git - libs/net.git/blobdiff - source/http/server.cpp
Use nullptr instead of 0 for pointers
[libs/net.git] / source / http / server.cpp
index 97a42b575477a6f89938d1333ac779db2b03842e..a7435d330350b41e60416dbc3eb632d840b742ab 100644 (file)
@@ -18,13 +18,11 @@ namespace Msp {
 namespace Http {
 
 Server::Server():
-       sock(Net::INET6),
-       event_disp(0)
+       sock(Net::INET6)
 { }
 
 Server::Server(unsigned port):
-       sock(Net::INET6),
-       event_disp(0)
+       sock(Net::INET6)
 {
        listen(port);
 }
@@ -54,15 +52,15 @@ void Server::use_event_dispatcher(IO::EventDispatcher *ed)
        if(event_disp)
        {
                event_disp->remove(sock);
-               for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
-                       event_disp->remove(*i->sock);
+               for(Client &c: clients)
+                       event_disp->remove(*c.sock);
        }
        event_disp = ed;
        if(event_disp)
        {
                event_disp->add(sock);
-               for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
-                       event_disp->add(*i->sock);
+               for(Client &c: clients)
+                       event_disp->add(*c.sock);
        }
 }
 
@@ -83,6 +81,28 @@ void Server::cancel_keepalive(Response &resp)
        get_client_by_response(resp).keepalive = false;
 }
 
+void Server::close_connections(const Time::TimeDelta &timeout)
+{
+       IO::Poller poller;
+       for(Client &c: clients)
+       {
+               c.sock->shutdown(IO::M_WRITE);
+               poller.set_object(*c.sock, IO::P_INPUT);
+       }
+
+       while(!clients.empty() && poller.poll(timeout))
+       {
+               for(const IO::Poller::PolledObject &p: poller.get_result())
+                       for(auto j=clients.begin(); j!=clients.end(); ++j)
+                               if(j->sock.get()==p.object)
+                               {
+                                       poller.set_object(*j->sock, IO::P_NONE);
+                                       clients.erase(j);
+                                       break;
+                               }
+       }
+}
+
 void Server::data_available()
 {
        Net::StreamSocket *csock = sock.accept();
@@ -95,7 +115,7 @@ void Server::data_available()
 
 void Server::client_data_available(Client &cl)
 {
-       for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
+       for(auto i=clients.begin(); i!=clients.end(); ++i)
                if(i->stale && &*i!=&cl)
                {
                        clients.erase(i);
@@ -172,7 +192,7 @@ void Server::client_data_available(Client &cl)
                        else
                        {
                                responses.erase(cl.response);
-                               cl.response = 0;
+                               cl.response = nullptr;
                                if(response->get_status()==NONE)
                                {
                                        response = new Response(NOT_FOUND);
@@ -183,7 +203,7 @@ void Server::client_data_available(Client &cl)
                catch(const exception &e)
                {
                        responses.erase(cl.response);
-                       cl.response = 0;
+                       cl.response = nullptr;
                        response = new Response(INTERNAL_ERROR);
                        response->add_content(format("An error occurred while processing the request:\ntype: %s\nwhat: %s",
                                Debug::demangle(typeid(e).name()), e.what()));
@@ -213,9 +233,9 @@ void Server::send_response(Client &cl, Response &resp)
        if(cl.keepalive)
        {
                delete cl.request;
-               cl.request = 0;
+               cl.request = nullptr;
                delete cl.response;
-               cl.response = 0;
+               cl.response = nullptr;
        }
        else
        {
@@ -236,12 +256,7 @@ Server::Client &Server::get_client_by_response(Response &resp)
 
 
 Server::Client::Client(RefPtr<Net::StreamSocket> s):
-       sock(s),
-       request(0),
-       response(0),
-       keepalive(false),
-       async(false),
-       stale(false)
+       sock(s)
 { }
 
 Server::Client::~Client()