Implement add_data().
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 16:00:54 +0000 (18:00 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 16:00:54 +0000 (18:00 +0200)
server.cpp

index d6b5b5b8b783446ce08e87972b34430b9bf4e547..9b91e0371254e67044a8138424334a21aea4b88c 100644 (file)
@@ -107,7 +107,23 @@ void Server::set_header(const string &stream_id, const string &header)
        
 void Server::add_data(const string &stream_id, const char *data, size_t bytes)
 {
-       // TODO
+       MutexLock lock(&mutex);
+       assert(streams.count(stream_id) != 0);
+       Stream *stream = &streams[stream_id];
+       size_t pos = stream->data_size % BACKLOG_SIZE;
+       stream->data_size += bytes;
+
+       if (pos + bytes > BACKLOG_SIZE) {
+               size_t to_copy = BACKLOG_SIZE - pos;
+               memcpy(stream->data + pos, data, to_copy);
+               data += to_copy;
+               bytes -= to_copy;
+               pos = 0;
+       }
+
+       memcpy(stream->data + pos, data, bytes);
+
+       // TODO: wake up clients
 }
        
 void Server::process_client(Client *client)
@@ -216,6 +232,9 @@ void Server::process_client(Client *client)
                        return;
                }
                client->bytes_sent += ret;      
+
+               // TODO: put clients to sleep
+
                break;
        }
        default: