]> git.tdb.fi Git - libs/net.git/commitdiff
Store pending responses in a map
authorMikko Rasa <tdb@tdb.fi>
Wed, 10 Aug 2011 18:42:48 +0000 (21:42 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 10 Aug 2011 18:42:48 +0000 (21:42 +0300)
source/http/server.cpp
source/http/server.h

index 9205cc1096383caa797f1af237ec9e02e1b07724..47de7dd381b6c84cd995e4dd56239ab64d91b2e2 100644 (file)
@@ -1,4 +1,5 @@
 #include <exception>
+#include <msp/core/maputils.h>
 #include <msp/core/refptr.h>
 #include <msp/net/inet.h>
 #include <msp/net/resolve.h>
@@ -124,11 +125,13 @@ void Server::client_data_available(Client &cl)
                try
                {
                        cl.response = response.get();
+                       responses[cl.response] = &cl;
                        signal_request.emit(*cl.request, *response);
                        if(cl.async)
                                response.release();
                        else
                        {
+                               responses.erase(cl.response);
                                cl.response = 0;
                                if(response->get_status()==NONE)
                                {
@@ -139,6 +142,7 @@ void Server::client_data_available(Client &cl)
                }
                catch(const exception &e)
                {
+                       responses.erase(cl.response);
                        cl.response = 0;
                        response = new Response(INTERNAL_ERROR);
                        response->add_content(e.what());
@@ -159,12 +163,7 @@ void Server::client_end_of_file(Client &cl)
 
 Server::Client &Server::get_client_by_response(Response &resp)
 {
-       for(list<Client>::iterator i=clients.begin(); i!=clients.end(); ++i)
-               if(i->response==&resp)
-                       return *i;
-
-       // XXX Do this differently
-       throw invalid_argument("Response does not belong to any client");
+       return *get_item(responses, &resp);
 }
 
 
index 41894db99d6141dc89c901c354d06b4f25ca25b3..e07ee55a2251a860652d98679ac0755ae25921d8 100644 (file)
@@ -32,6 +32,7 @@ private:
 
        Net::StreamServerSocket sock;
        std::list<Client> clients;
+       std::map<Response *, Client *> responses;
        IO::EventDispatcher *event_disp;
 
 public: