X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=stream.cpp;h=2b9a69593a478605800fd21c9585063eebec8d0c;hp=d57157822aa93939c9250940bec1cf4132f212cb;hb=e666e2735bf831084d744027b844de9f4d3fe34d;hpb=ae994771c0747d43bd1ed422224f4caacb95ca9f diff --git a/stream.cpp b/stream.cpp index d571578..2b9a695 100644 --- a/stream.cpp +++ b/stream.cpp @@ -6,6 +6,7 @@ #include #include "state.pb.h" +#include "log.h" #include "stream.h" #include "util.h" @@ -31,7 +32,7 @@ Stream::~Stream() ret = close(data_fd); } while (ret == -1 && errno == EINTR); if (ret == -1) { - perror("close"); + log_perror("close"); } } } @@ -62,6 +63,41 @@ StreamProto Stream::serialize() data_fd = -1; return serialized; } + +void Stream::set_backlog_size(size_t new_size) +{ + if (backlog_size == new_size) { + return; + } + + string existing_data; + if (!read_tempfile(data_fd, &existing_data)) { // Closes data_fd. + exit(1); + } + + // Unwrap the data so it's no longer circular. + if (bytes_received <= backlog_size) { + existing_data.resize(bytes_received); + } else { + size_t pos = bytes_received % backlog_size; + existing_data = existing_data.substr(pos, string::npos) + + existing_data.substr(0, pos); + } + + // See if we need to discard data. + if (new_size < existing_data.size()) { + size_t to_discard = existing_data.size() - new_size; + existing_data = existing_data.substr(to_discard, string::npos); + } + + // Create a new, empty data file. + data_fd = make_tempfile(""); + backlog_size = new_size; + + // Now cheat a bit by rewinding, and adding all the old data back. + bytes_received -= existing_data.size(); + add_data(existing_data.data(), existing_data.size()); +} void Stream::put_client_to_sleep(Client *client) { @@ -81,7 +117,7 @@ void Stream::add_data(const char *data, ssize_t bytes) continue; } if (ret == -1) { - perror("pwrite"); + log_perror("pwrite"); // Dazed and confused, but trying to continue... break; } @@ -99,7 +135,7 @@ void Stream::add_data(const char *data, ssize_t bytes) continue; } if (ret == -1) { - perror("pwrite"); + log_perror("pwrite"); // Dazed and confused, but trying to continue... break; } @@ -107,8 +143,6 @@ void Stream::add_data(const char *data, ssize_t bytes) data += ret; bytes -= ret; } - - wake_up_all_clients(); } void Stream::wake_up_all_clients()