]> git.tdb.fi Git - libs/net.git/commitdiff
Add a function to close all HTTP connections
authorMikko Rasa <tdb@tdb.fi>
Fri, 9 Dec 2022 18:35:24 +0000 (20:35 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 9 Dec 2022 18:35:24 +0000 (20:35 +0200)
This can be useful for graceful shutdown at program exit.

source/http/server.cpp
source/http/server.h

index 97a42b575477a6f89938d1333ac779db2b03842e..667403e86788c9228e179e1f9677feb1552fb6cc 100644 (file)
@@ -83,6 +83,29 @@ 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(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
+       {
+               i->sock->shutdown(IO::M_WRITE);
+               poller.set_object(*i->sock, IO::P_INPUT);
+       }
+
+       while(!clients.empty() && poller.poll(timeout))
+       {
+               const vector<IO::Poller::PolledObject> &result = poller.get_result();
+               for(vector<IO::Poller::PolledObject>::const_iterator i=result.begin(); i!=result.end(); ++i)
+                       for(list<Client>::iterator j=clients.begin(); j!=clients.end(); ++j)
+                               if(j->sock.get()==i->object)
+                               {
+                                       poller.set_object(*j->sock, IO::P_NONE);
+                                       clients.erase(j);
+                                       break;
+                               }
+       }
+}
+
 void Server::data_available()
 {
        Net::StreamSocket *csock = sock.accept();
index dd054201d1bf421e136a97c4c2365e8f408cac70..4835de039c87b46781499a52e54cb901782b5188 100644 (file)
@@ -4,6 +4,7 @@
 #include <msp/core/refptr.h>
 #include <msp/io/eventdispatcher.h>
 #include <msp/net/streamserversocket.h>
+#include <msp/time/timedelta.h>
 
 namespace Msp {
 namespace Http {
@@ -47,6 +48,7 @@ public:
        void delay_response(Response &);
        void submit_response(Response &);
        void cancel_keepalive(Response &);
+       void close_connections(const Time::TimeDelta &);
 private:
        void data_available();
        void client_data_available(Client &);