When we get a header, retroactively send it out to all clients that have received...
[cubemap] / server.cpp
index c80f2da2e2c354729702ef04ba9f7068ed5ec14c..f47de543e2b69a1350138ab0cb3c0f979fe07484 100644 (file)
@@ -200,6 +200,18 @@ void Server::set_header(const string &stream_id, const string &header)
 {
        MutexLock lock(&mutex);
        find_stream(stream_id)->header = header;
+
+       // If there are clients we haven't sent anything to yet, we should give
+       // them the header, so push back into the SENDING_HEADER state.
+       for (map<int, Client>::iterator client_it = clients.begin();
+            client_it != clients.end();
+            ++client_it) {
+               Client *client = &client_it->second;
+               if (client->state == Client::SENDING_DATA &&
+                   client->bytes_sent == 0) {
+                       construct_header(client);
+               }
+       }
 }
        
 void Server::add_data(const string &stream_id, const char *data, size_t bytes)
@@ -273,6 +285,7 @@ void Server::process_client(Client *client)
                }
 
                parse_request(client);
+               construct_header(client);
                break;
        }
        case Client::SENDING_HEADER: {
@@ -356,9 +369,11 @@ void Server::parse_request(Client *client)
        // TODO: Actually parse the request. :-)
        client->stream_id = "stream";
        client->request.clear();
+}
 
-       // Construct the header.
-       client->header = "HTTP/1.0 200 OK\r\n  Content-type: video/x-flv\r\nCache-Control: no-cache\r\nContent-type: todo/fixme\r\n\r\n" +
+void Server::construct_header(Client *client)
+{
+       client->header = "HTTP/1.0 200 OK\r\nContent-type: video/x-flv\r\nCache-Control: no-cache\r\n\r\n" +
                find_stream(client->stream_id)->header;
 
        // Switch states.