From: Steinar H. Gunderson Date: Thu, 5 Apr 2018 15:48:39 +0000 (+0200) Subject: Use in-class initialization for making it harder to forget to set a default. X-Git-Tag: 1.4.0~32 X-Git-Url: https://git.sesse.net/?p=cubemap;a=commitdiff_plain;h=f0621e41fdb96ce1bd58e7561e0aa76345072ba3;ds=sidebyside Use in-class initialization for making it harder to forget to set a default. --- diff --git a/client.cpp b/client.cpp index 3fdd817..4b7bcb7 100644 --- a/client.cpp +++ b/client.cpp @@ -16,18 +16,7 @@ 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); diff --git a/client.h b/client.h index 666e3b4..834ddad 100644 --- a/client.h +++ b/client.h @@ -47,7 +47,7 @@ struct Client { 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. @@ -56,7 +56,7 @@ struct Client { // What stream we're connecting to; parsed from . // 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). @@ -67,7 +67,7 @@ struct Client { // 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), @@ -75,19 +75,19 @@ struct Client { // -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) diff --git a/httpinput.cpp b/httpinput.cpp index 5b74319..155e0b3 100644 --- a/httpinput.cpp +++ b/httpinput.cpp @@ -49,11 +49,7 @@ extern ServerPool *servers; 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; @@ -62,9 +58,6 @@ HTTPInput::HTTPInput(const string &url, Input::Encoding encoding) 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) @@ -79,9 +72,7 @@ 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()); diff --git a/httpinput.h b/httpinput.h index 0e29453..ac52bfc 100644 --- a/httpinput.h +++ b/httpinput.h @@ -87,10 +87,10 @@ private: // If 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 . mutable pthread_mutex_t stats_mutex; @@ -99,15 +99,15 @@ private: 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) diff --git a/input.h b/input.h index 7e67392..624ae33 100644 --- a/input.h +++ b/input.h @@ -1,6 +1,7 @@ #ifndef _INPUT_H #define _INPUT_H 1 +#include #include #include #include @@ -17,23 +18,23 @@ struct InputStats { // 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. diff --git a/serverpool.cpp b/serverpool.cpp index 232ccea..a5c8a72 100644 --- a/serverpool.cpp +++ b/serverpool.cpp @@ -16,9 +16,7 @@ using namespace std; ServerPool::ServerPool(int size) : servers(new Server[size]), - num_servers(size), - clients_added(0), - num_http_streams(0) + num_servers(size) { } diff --git a/serverpool.h b/serverpool.h index 87f3f8e..5f661df 100644 --- a/serverpool.h +++ b/serverpool.h @@ -76,13 +76,13 @@ public: private: std::unique_ptr 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 // . - int num_http_streams; + int num_http_streams = 0; std::vector> udp_streams; ServerPool(const ServerPool &); diff --git a/stream.cpp b/stream.cpp index f5abbfd..d7a78a6 100644 --- a/stream.cpp +++ b/stream.cpp @@ -26,9 +26,7 @@ Stream::Stream(const string &url, size_t backlog_size, size_t prebuffering_bytes 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); @@ -52,8 +50,7 @@ Stream::Stream(const StreamProto &serialized, int data_fd) 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); diff --git a/stream.h b/stream.h index 9995765..6c757a1 100644 --- a/stream.h +++ b/stream.h @@ -72,7 +72,7 @@ struct Stream { // How many bytes this stream have received. Can very well be larger // than , 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. @@ -88,7 +88,7 @@ struct Stream { std::vector to_process; // Maximum pacing rate for the stream. - uint32_t pacing_rate; + uint32_t pacing_rate = ~0U; // Queued data, if any. Protected by . // The data pointers in the iovec are owned by us. diff --git a/udpinput.cpp b/udpinput.cpp index 445d4f5..93eaaa7 100644 --- a/udpinput.cpp +++ b/udpinput.cpp @@ -115,11 +115,7 @@ UDPInput::UDPInput(const string &url) 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)