sleeping_clients.push_back(client);
}
-void Stream::add_data(const char *data, ssize_t bytes)
-{
- if (encoding == Stream::STREAM_ENCODING_RAW) {
- add_data_raw(data, bytes);
- } else if (encoding == STREAM_ENCODING_METACUBE) {
- metacube_block_header hdr;
- memcpy(hdr.sync, METACUBE_SYNC, sizeof(hdr.sync));
- hdr.size = htonl(bytes);
- hdr.flags = htonl(0);
-
- char *block = new char[bytes + sizeof(hdr)];
- memcpy(block, &hdr, sizeof(hdr));
- memcpy(block + sizeof(hdr), data, bytes);
- add_data_raw(block, bytes + sizeof(hdr));
- delete[] block;
- } else {
- assert(false);
- }
-}
-
void Stream::add_data_raw(const char *data, ssize_t bytes)
{
size_t pos = bytes_received % backlog_size;
}
}
-void Stream::wake_up_all_clients()
+void Stream::add_data_deferred(const char *data, size_t bytes)
+{
+ if (encoding == Stream::STREAM_ENCODING_RAW) {
+ queued_data.append(string(data, data + bytes));
+ } else if (encoding == STREAM_ENCODING_METACUBE) {
+ metacube_block_header hdr;
+ memcpy(hdr.sync, METACUBE_SYNC, sizeof(hdr.sync));
+ hdr.size = htonl(bytes);
+ hdr.flags = htonl(0);
+
+ char *block = new char[bytes + sizeof(hdr)];
+ memcpy(block, &hdr, sizeof(hdr));
+ memcpy(block + sizeof(hdr), data, bytes);
+ queued_data.append(string(block, block + bytes + sizeof(hdr)));
+ delete[] block;
+ } else {
+ assert(false);
+ }
+}
+
+void Stream::process_queued_data()
{
+ if (queued_data.empty()) {
+ return;
+ }
+
+ add_data_raw(queued_data.data(), queued_data.size());
+ queued_data.clear();
+
+ // We have more data, so wake up all clients.
if (to_process.empty()) {
swap(sleeping_clients, to_process);
} else {