]> git.sesse.net Git - cubemap/commitdiff
Use in-class initialization for making it harder to forget to set a default.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 5 Apr 2018 15:48:39 +0000 (17:48 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 5 Apr 2018 15:48:39 +0000 (17:48 +0200)
client.cpp
client.h
httpinput.cpp
httpinput.h
input.h
serverpool.cpp
serverpool.h
stream.cpp
stream.h
udpinput.cpp

index 3fdd817db7afb47c2cec6a1cd2cfa8051c3e8562..4b7bcb7e0bdb7604551e4d146f3bb1a4689e358d 100644 (file)
 using namespace std;
 
 Client::Client(int sock)
 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);
 
 {
        request.reserve(1024);
 
index 666e3b417c1e6db142bc2c34d759be902dc27d56..834ddad9cd2227e7b302674d33e67d49783ca1f1 100644 (file)
--- 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 };
        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.
 
        // 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 <request>.
        // Not relevant for READING_REQUEST.
        std::string url;
        // 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).
 
        // 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.
 
        // 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),
 
        // 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.
        // -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.
 
        // 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.
 
        // 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)
 };
 
 #endif  // !defined(_CLIENT_H)
index 5b74319d71f77645c99b45452b0a677298316901..155e0b3988e484dfe966f7b7e92565909ca2b342 100644 (file)
@@ -49,11 +49,7 @@ extern ServerPool *servers;
 HTTPInput::HTTPInput(const string &url, Input::Encoding encoding)
        : state(NOT_CONNECTED),
          url(url),
 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;
 {
        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;
        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)
 }
 
 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()),
          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());
 {
        pending_data.resize(serialized.pending_data().size());
        memcpy(&pending_data[0], serialized.pending_data().data(), serialized.pending_data().size());
index 0e29453fb4bdab7aabcc6f7f092940bcf7d8b353..ac52bfc34b019e91fa58dc688306d872695ca7f8 100644 (file)
@@ -87,10 +87,10 @@ private:
 
        // If <pending_data> starts with a Metacube header,
        // this is true.
 
        // 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).
 
        // The socket we are downloading on (or -1).
-       int sock;
+       int sock = -1;
 
        // Mutex protecting <stats>.
        mutable pthread_mutex_t stats_mutex;
 
        // Mutex protecting <stats>.
        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.
        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.)
 
        // 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.)
 
        // 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)
 };
 
 #endif  // !defined(_HTTPINPUT_H)
diff --git a/input.h b/input.h
index 7e67392da0319fc50ac2dbe254495c0a919614d2..624ae33c6a3b10fac20577480df62f657dd9240d 100644 (file)
--- a/input.h
+++ b/input.h
@@ -1,6 +1,7 @@
 #ifndef _INPUT_H
 #define _INPUT_H 1
 
 #ifndef _INPUT_H
 #define _INPUT_H 1
 
+#include <math.h>
 #include <stddef.h>
 #include <time.h>
 #include <string>
 #include <stddef.h>
 #include <time.h>
 #include <string>
@@ -17,23 +18,23 @@ struct InputStats {
        // The number of bytes we have received so far, including any Metacube headers.
        //
        // Not reset across connections.
        // 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.
 
        // 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.
 
        // 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.
 
        // 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.
 
        // 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.
 
        // TODO: Number of loss events might both be useful,
        // similar to for clients. Also, per-connection byte counters.
index 232ccea72a1acdc01fef8104c8f7048e7cb28a8c..a5c8a72ffc0ae7726a1413d3f071cb06666bf6d3 100644 (file)
@@ -16,9 +16,7 @@ using namespace std;
 
 ServerPool::ServerPool(int size)
        : servers(new Server[size]),
 
 ServerPool::ServerPool(int size)
        : servers(new Server[size]),
-         num_servers(size),
-         clients_added(0),
-         num_http_streams(0)
+         num_servers(size)
 {
 }
 
 {
 }
 
index 87f3f8e4d1837054a036dc0b96e66767f8014312..5f661df2dc175046f6197fdf89177341f704dd5b 100644 (file)
@@ -76,13 +76,13 @@ public:
 
 private:
        std::unique_ptr<Server[]> servers;
 
 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>.
 
        // 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 &);
        std::vector<std::unique_ptr<UDPStream>> udp_streams;
 
        ServerPool(const ServerPool &);
index f5abbfd0f3e07795a0502c3d562ec56a01d3a5f4..d7a78a602098ab5d77dda08873af8c3d7b29e11b 100644 (file)
@@ -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),
          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);
 {
        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()),
          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);
 {
        if (data_fd == -1) {
                exit(1);
index 9995765b2db95042fbe1c6369e9d3c2114896400..6c757a19ff29224f1049c2fc8554d4c24f36af0a 100644 (file)
--- 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 <backlog_size>, since the buffer wraps.
 
        // 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.
 
        // 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<Client *> to_process;
 
        // Maximum pacing rate for the stream.
        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.
 
        // Queued data, if any. Protected by <queued_data_mutex>.
        // The data pointers in the iovec are owned by us.
index 445d4f551fff145b3ceac1e417b574d4292a1ade..93eaaa76801c0dbab7383e2e7b8902b5fd408ea0 100644 (file)
@@ -115,11 +115,7 @@ UDPInput::UDPInput(const string &url)
 
        pthread_mutex_init(&stats_mutex, nullptr);
        stats.url = 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.connect_time = time(nullptr);
-       stats.latency_sec = HUGE_VAL;
 }
 
 UDPInput::UDPInput(const InputProto &serialized)
 }
 
 UDPInput::UDPInput(const InputProto &serialized)