Fix a few issues in the to_process() handling.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 8 Apr 2013 22:48:57 +0000 (00:48 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 8 Apr 2013 22:48:57 +0000 (00:48 +0200)
server.cpp

index f06685ab02224ea654b85d82f09d35a5dc456a5c..6b92fc6af5eb6eec6fa9983a587681dfc2e14623 100644 (file)
@@ -196,11 +196,11 @@ void Server::do_work()
                for (map<string, Stream *>::iterator stream_it = streams.begin();
                     stream_it != streams.end();
                     ++stream_it) {
-                       Stream *stream = stream_it->second;
-                       for (size_t i = 0; i < stream->to_process.size(); ++i) {
-                               process_client(stream->to_process[i]);
+                       vector<Client *> to_process;
+                       swap(stream_it->second->to_process, to_process);
+                       for (size_t i = 0; i < to_process.size(); ++i) {
+                               process_client(to_process[i]);
                        }
-                       stream->to_process.clear();
                }
        }
 }
@@ -575,6 +575,13 @@ void Server::construct_error(Client *client, int error_code)
                exit(1);
        }
 }
+
+template<class T>
+void delete_from(vector<T> *v, T elem)
+{
+       typename vector<T>::iterator new_end = remove(v->begin(), v->end(), elem);
+       v->erase(new_end, v->end());
+}
        
 void Server::close_client(Client *client)
 {
@@ -585,12 +592,8 @@ void Server::close_client(Client *client)
 
        // This client could be sleeping, so we'll need to fix that. (Argh, O(n).)
        if (client->stream != NULL) {
-               vector<Client *>::iterator new_end =
-                       remove(client->stream->sleeping_clients.begin(),
-                              client->stream->sleeping_clients.end(),
-                              client);
-               client->stream->sleeping_clients.erase(
-                       new_end, client->stream->sleeping_clients.end());
+               delete_from(&client->stream->sleeping_clients, client);
+               delete_from(&client->stream->to_process, client);
        }
        
        // Bye-bye!