From 7850079d5f7a6700f96569fdbf2908a9b34a74ac Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 7 Apr 2013 00:51:12 +0200 Subject: [PATCH 1/1] When we get a header, retroactively send it out to all clients that have received no data yet. Useful if the clients connect before the encoder does. --- server.cpp | 19 +++++++++++++++++-- server.h | 7 +++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/server.cpp b/server.cpp index c80f2da..f47de54 100644 --- a/server.cpp +++ b/server.cpp @@ -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::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. diff --git a/server.h b/server.h index 9900960..65ac6b8 100644 --- a/server.h +++ b/server.h @@ -126,10 +126,13 @@ private: // Close a given client socket, and clean up after it. void close_client(Client *client); - // Parse the HTTP request, construct the header, and set the client into - // the SENDING_HEADER state. + // Parse the HTTP request. void parse_request(Client *client); + // Construct the HTTP header, and set the client into + // the SENDING_HEADER state. + void construct_header(Client *client); + // Put client to sleep, since there is no more data for it; we will on // longer listen on POLLOUT until we get more data. Also, it will be put // in the list of clients to wake up when we do. -- 2.39.2