using namespace std;
Client::Client(int sock)
- : sock(sock),
- state(Client::READING_REQUEST),
- stream(nullptr),
- header_or_short_response_bytes_sent(0),
- stream_pos(0),
- bytes_sent(0),
- bytes_lost(0),
- num_loss_events(0),
- tls_context(nullptr),
- tls_data_to_send(nullptr),
- tls_data_left_to_send(0),
- in_ktls_mode(false)
+ : sock(sock)
{
request.reserve(1024);
std::string user_agent;
enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_SHORT_RESPONSE, WAITING_FOR_KEYFRAME, PREBUFFERING };
- State state;
+ State state = READING_REQUEST;
// The HTTP request, as sent by the client. If we are in READING_REQUEST,
// this might not be finished.
// What stream we're connecting to; parsed from <request>.
// Not relevant for READING_REQUEST.
std::string url;
- Stream *stream;
+ Stream *stream = nullptr;
// The header we want to send, or the response with headers if we know
// it in its entirety after reading the request (typically an error).
// Number of bytes we've sent of the header. Only relevant for SENDING_HEADER
// or SENDING_SHORT_RESPONSE.
- size_t header_or_short_response_bytes_sent;
+ size_t header_or_short_response_bytes_sent = 0;
// Number of bytes we are into the stream (ie., the end of last send).
// -1 means we want to send from the end of the backlog (the normal case),
// -2 means we want to send from the _beginning_ of the backlog.
// Once we go into WAITING_FOR_KEYFRAME, PREBUFFERING or SENDING_DATA,
// these negative values will be translated to real numbers.
- size_t stream_pos;
+ size_t stream_pos = 0;
// Number of bytes we've sent of data. Only relevant for SENDING_DATA.
- size_t bytes_sent;
+ size_t bytes_sent = 0;
// Number of times we've skipped forward due to the backlog being too big,
// and how many bytes we've skipped over in all. Only relevant for SENDING_DATA.
- size_t bytes_lost, num_loss_events;
+ size_t bytes_lost = 0, num_loss_events = 0;
- TLSContext *tls_context;
- const unsigned char *tls_data_to_send;
- unsigned tls_data_left_to_send;
- bool in_ktls_mode;
+ TLSContext *tls_context = nullptr;
+ const unsigned char *tls_data_to_send = nullptr;
+ unsigned tls_data_left_to_send = 0;
+ bool in_ktls_mode = false;
};
#endif // !defined(_CLIENT_H)
HTTPInput::HTTPInput(const string &url, Input::Encoding encoding)
: state(NOT_CONNECTED),
url(url),
- encoding(encoding),
- has_metacube_header(false),
- sock(-1),
- num_connection_attempts(0),
- suppress_logging(false)
+ encoding(encoding)
{
pthread_mutex_init(&stats_mutex, nullptr);
stats.url = url;
stats.metadata_bytes_received = 0;
stats.connect_time = -1;
stats.latency_sec = HUGE_VAL;
-
- last_verbose_connection.tv_sec = -3600;
- last_verbose_connection.tv_nsec = 0;
}
HTTPInput::HTTPInput(const InputProto &serialized)
http_header(serialized.http_header()),
stream_header(serialized.stream_header()),
has_metacube_header(serialized.has_metacube_header()),
- sock(serialized.sock()),
- num_connection_attempts(0),
- suppress_logging(false)
+ sock(serialized.sock())
{
pending_data.resize(serialized.pending_data().size());
memcpy(&pending_data[0], serialized.pending_data().data(), serialized.pending_data().size());
// If <pending_data> starts with a Metacube header,
// this is true.
- bool has_metacube_header;
+ bool has_metacube_header = false;
// The socket we are downloading on (or -1).
- int sock;
+ int sock = -1;
// Mutex protecting <stats>.
mutable pthread_mutex_t stats_mutex;
InputStats stats;
// Number of (started) connection attempts since last data byte was successfully read.
- unsigned num_connection_attempts;
+ unsigned num_connection_attempts = 0;
// If set, don't log anything related to connections.
// (Only set if we've had enough unsuccessful connection attempts.)
- bool suppress_logging;
+ bool suppress_logging = false;
// Last time we made a connection with logging enabled.
// (Initially at some point before the epoch.)
- timespec last_verbose_connection;
+ timespec last_verbose_connection { -3600, 0 };
};
#endif // !defined(_HTTPINPUT_H)
#ifndef _INPUT_H
#define _INPUT_H 1
+#include <math.h>
#include <stddef.h>
#include <time.h>
#include <string>
// The number of bytes we have received so far, including any Metacube headers.
//
// Not reset across connections.
- size_t bytes_received;
+ size_t bytes_received = 0;
// The number of data bytes we have received so far (or more precisely,
// number of data bytes we have sent on to the stream). This excludes Metacube
// headers, metadata and corrupted data we've skipped.
//
// Not reset across connections.
- size_t data_bytes_received;
+ size_t data_bytes_received = 0;
// Same, except counts only Metacube metadata.
- size_t metadata_bytes_received;
+ size_t metadata_bytes_received = 0;
// When the current connection was initiated. -1 if we are not currently connected.
- time_t connect_time;
+ time_t connect_time = -1;
// Last latency measurement, HUGE_VAL if no measurement yet.
- double latency_sec;
+ double latency_sec = HUGE_VAL;
// TODO: Number of loss events might both be useful,
// similar to for clients. Also, per-connection byte counters.
ServerPool::ServerPool(int size)
: servers(new Server[size]),
- num_servers(size),
- clients_added(0),
- num_http_streams(0)
+ num_servers(size)
{
}
private:
std::unique_ptr<Server[]> servers;
- int num_servers, clients_added;
+ int num_servers, clients_added = 0;
// Our indexing is currently rather primitive; every stream_index in
// [0, num_http_streams) maps to a HTTP stream (of which every Server
// has exactly one copy), and after that, it's mapping directly into
// <udp_streams>.
- int num_http_streams;
+ int num_http_streams = 0;
std::vector<std::unique_ptr<UDPStream>> udp_streams;
ServerPool(const ServerPool &);
src_encoding(src_encoding),
data_fd(make_tempfile("")),
backlog_size(backlog_size),
- prebuffering_bytes(prebuffering_bytes),
- bytes_received(0),
- pacing_rate(~0U)
+ prebuffering_bytes(prebuffering_bytes)
{
if (data_fd == -1) {
exit(1);
data_fd(data_fd),
backlog_size(serialized.backlog_size()),
prebuffering_bytes(serialized.prebuffering_bytes()),
- bytes_received(serialized.bytes_received()),
- pacing_rate(~0U)
+ bytes_received(serialized.bytes_received())
{
if (data_fd == -1) {
exit(1);
// How many bytes this stream have received. Can very well be larger
// than <backlog_size>, since the buffer wraps.
- size_t bytes_received;
+ size_t bytes_received = 0;
// A list of points in the stream that is suitable to start new clients at
// (after having sent the header). Empty if no such point exists yet.
std::vector<Client *> to_process;
// Maximum pacing rate for the stream.
- uint32_t pacing_rate;
+ uint32_t pacing_rate = ~0U;
// Queued data, if any. Protected by <queued_data_mutex>.
// The data pointers in the iovec are owned by us.
pthread_mutex_init(&stats_mutex, nullptr);
stats.url = url;
- stats.bytes_received = 0;
- stats.data_bytes_received = 0;
- stats.metadata_bytes_received = 0;
stats.connect_time = time(nullptr);
- stats.latency_sec = HUGE_VAL;
}
UDPInput::UDPInput(const InputProto &serialized)