return stats;
}
-Stream::Stream(const string &stream_id)
+Stream::Stream(const string &stream_id, size_t backlog_size)
: stream_id(stream_id),
data_fd(make_tempfile("")),
- data_size(0),
+ backlog_size(backlog_size),
+ bytes_received(0),
mark_pool(NULL)
{
if (data_fd == -1) {
: stream_id(serialized.stream_id()),
header(serialized.header()),
data_fd(make_tempfile(serialized.data())),
- data_size(serialized.data_size()),
+ backlog_size(serialized.backlog_size()),
+ bytes_received(serialized.bytes_received()),
mark_pool(NULL)
{
if (data_fd == -1) {
if (!read_tempfile(data_fd, serialized.mutable_data())) { // Closes data_fd.
exit(1);
}
- serialized.set_data_size(data_size);
+ serialized.set_backlog_size(backlog_size);
+ serialized.set_bytes_received(bytes_received);
serialized.set_stream_id(stream_id);
data_fd = -1;
return serialized;
}
if (client_ptr->state == Client::SENDING_DATA &&
- client_ptr->bytes_sent == client_ptr->stream->data_size) {
+ client_ptr->bytes_sent == client_ptr->stream->bytes_received) {
client_ptr->stream->put_client_to_sleep(client_ptr);
} else {
process_client(client_ptr);
}
}
-void Server::add_stream(const string &stream_id)
+void Server::add_stream(const string &stream_id, size_t backlog_size)
{
MutexLock lock(&mutex);
- streams.insert(make_pair(stream_id, new Stream(stream_id)));
+ streams.insert(make_pair(stream_id, new Stream(stream_id, backlog_size)));
}
void Server::add_stream_from_serialized(const StreamProto &stream)
void Server::add_data(const string &stream_id, const char *data, ssize_t bytes)
{
Stream *stream = find_stream(stream_id);
- size_t pos = stream->data_size % BACKLOG_SIZE;
- stream->data_size += bytes;
+ size_t pos = stream->bytes_received % stream->backlog_size;
+ stream->bytes_received += bytes;
- if (pos + bytes > BACKLOG_SIZE) {
- ssize_t to_copy = BACKLOG_SIZE - pos;
+ 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) {
// but we'll start sending immediately as we get data.
// This is postcondition #3.
client->state = Client::SENDING_DATA;
- client->bytes_sent = client->stream->data_size;
+ client->bytes_sent = client->stream->bytes_received;
client->stream->put_client_to_sleep(client);
return;
}
// See if there's some data we've lost. Ideally, we should drop to a block boundary,
// but resync will be the mux's problem.
Stream *stream = client->stream;
- size_t bytes_to_send = stream->data_size - client->bytes_sent;
+ size_t bytes_to_send = stream->bytes_received - client->bytes_sent;
if (bytes_to_send == 0) {
return;
}
- if (bytes_to_send > BACKLOG_SIZE) {
+ if (bytes_to_send > stream->backlog_size) {
fprintf(stderr, "WARNING: fd %d lost %lld bytes, maybe too slow connection\n",
client->sock,
- (long long int)(bytes_to_send - BACKLOG_SIZE));
- client->bytes_sent = stream->data_size - BACKLOG_SIZE;
- bytes_to_send = BACKLOG_SIZE;
+ (long long int)(bytes_to_send - stream->backlog_size));
+ client->bytes_sent = stream->bytes_received - stream->backlog_size;
+ bytes_to_send = stream->backlog_size;
}
// See if we need to split across the circular buffer.
bool more_data = false;
- if ((client->bytes_sent % BACKLOG_SIZE) + bytes_to_send > BACKLOG_SIZE) {
- bytes_to_send = BACKLOG_SIZE - (client->bytes_sent % BACKLOG_SIZE);
+ if ((client->bytes_sent % stream->backlog_size) + bytes_to_send > stream->backlog_size) {
+ bytes_to_send = stream->backlog_size - (client->bytes_sent % stream->backlog_size);
more_data = true;
}
ssize_t ret;
do {
- loff_t offset = client->bytes_sent % BACKLOG_SIZE;
+ loff_t offset = client->bytes_sent % stream->backlog_size;
ret = sendfile(client->sock, stream->data_fd, &offset, bytes_to_send);
} while (ret == -1 && errno == EINTR);
}
client->bytes_sent += ret;
- if (client->bytes_sent == stream->data_size) {
+ if (client->bytes_sent == stream->bytes_received) {
// We don't have any more data for this client, so put it to sleep.
// This is postcondition #3.
stream->put_client_to_sleep(client);
#include "thread.h"
-#define BACKLOG_SIZE 1048576
#define EPOLL_MAX_EVENTS 8192
#define EPOLL_TIMEOUT_MS 20
#define MAX_CLIENT_REQUEST 16384
};
struct Stream {
- Stream(const std::string &stream_id);
+ Stream(const std::string &stream_id, size_t backlog_size);
~Stream();
// Serialization/deserialization.
// The HTTP response header, plus the video stream header (if any).
std::string header;
- // The stream data itself, stored in a circular buffer.q
+ // The stream data itself, stored in a circular buffer.
//
// We store our data in a file, so that we can send the data to the
// kernel only once (with write()). We then use sendfile() for each
// the same data from userspace many times.
int data_fd;
- // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
- // since the buffer wraps.
- size_t data_size;
+ // How many bytes <data_fd> can hold (the buffer size).
+ size_t backlog_size;
+
+ // How many bytes this stream have received. Can very well be larger
+ // than <backlog_size>, since the buffer wraps.
+ size_t bytes_received;
// Clients that are in SENDING_DATA, but that we don't listen on,
// because we currently don't have any data for them.
// at the same time).
CubemapStateProto serialize();
void add_client_from_serialized(const ClientProto &client);
- void add_stream(const std::string &stream_id);
+ void add_stream(const std::string &stream_id, size_t bytes_received);
void add_stream_from_serialized(const StreamProto &stream);
private: