From: Mikko Rasa Date: Fri, 9 Dec 2022 18:35:24 +0000 (+0200) Subject: Add a function to close all HTTP connections X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=bc3d93e11d501f12e25ad05d5eb2dea77f0fa0ec;p=libs%2Fnet.git Add a function to close all HTTP connections This can be useful for graceful shutdown at program exit. --- diff --git a/source/http/server.cpp b/source/http/server.cpp index 97a42b5..667403e 100644 --- a/source/http/server.cpp +++ b/source/http/server.cpp @@ -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::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 &result = poller.get_result(); + for(vector::const_iterator i=result.begin(); i!=result.end(); ++i) + for(list::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(); diff --git a/source/http/server.h b/source/http/server.h index dd05420..4835de0 100644 --- a/source/http/server.h +++ b/source/http/server.h @@ -4,6 +4,7 @@ #include #include #include +#include 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 &);