-void Server::add_data(const string &stream_id, const char *data, ssize_t bytes)
-{
- Stream *stream = find_stream(stream_id);
- size_t pos = stream->bytes_received % stream->backlog_size;
- stream->bytes_received += bytes;
-
- if (pos + bytes > stream->backlog_size) {
- ssize_t to_copy = stream->backlog_size - pos;
- while (to_copy > 0) {
- int ret = pwrite(stream->data_fd, data, to_copy, pos);
- if (ret == -1 && errno == EINTR) {
- continue;
- }
- if (ret == -1) {
- perror("pwrite");
- // Dazed and confused, but trying to continue...
- break;
- }
- pos += ret;
- data += ret;
- to_copy -= ret;
- bytes -= ret;
- }
- pos = 0;
- }
-
- while (bytes > 0) {
- int ret = pwrite(stream->data_fd, data, bytes, pos);
- if (ret == -1 && errno == EINTR) {
- continue;
- }
- if (ret == -1) {
- perror("pwrite");
- // Dazed and confused, but trying to continue...
- break;
- }
- pos += ret;
- data += ret;
- bytes -= ret;
- }
-
- stream->wake_up_all_clients();
-}
-