]> git.sesse.net Git - cubemap/blob - input.h
Allow (and forward) zero-byte UDP packets.
[cubemap] / input.h
1 #ifndef _INPUT_H
2 #define _INPUT_H 1
3
4 #include <math.h>
5 #include <stddef.h>
6 #include <time.h>
7 #include <string>
8
9 #include "thread.h"
10
11 class Input;
12 class InputProto;
13
14 // Digested statistics for writing to logs etc.
15 struct InputStats {
16         std::string url;
17
18         // The number of bytes we have received so far, including any Metacube headers.
19         //
20         // Not reset across connections.
21         size_t bytes_received = 0;
22
23         // The number of data bytes we have received so far (or more precisely,
24         // number of data bytes we have sent on to the stream). This excludes Metacube
25         // headers, metadata and corrupted data we've skipped.
26         //
27         // Not reset across connections.
28         size_t data_bytes_received = 0;
29
30         // Same, except counts only Metacube metadata.
31         size_t metadata_bytes_received = 0;
32
33         // When the current connection was initiated. -1 if we are not currently connected.
34         time_t connect_time = -1;
35
36         // Last latency measurement, HUGE_VAL if no measurement yet.
37         double latency_sec = HUGE_VAL;
38
39         // TODO: Number of loss events might both be useful,
40         // similar to for clients. Also, per-connection byte counters.
41 };
42
43 class Input : public Thread {
44 public:
45         // Must be in sync with StreamConfig::Encoding.
46         enum Encoding { INPUT_ENCODING_RAW = 0, INPUT_ENCODING_METACUBE };
47
48         virtual ~Input();
49         virtual InputProto serialize() const = 0;
50         virtual std::string get_url() const = 0;
51         virtual void close_socket() = 0;
52         virtual void add_destination(int stream_index) = 0;
53
54         // Note: May be called from a different thread, so must be thread-safe.
55         virtual InputStats get_stats() const = 0;
56 };
57
58 // Extremely rudimentary URL parsing.
59 bool parse_url(const std::string &url, std::string *protocol, std::string *user, std::string *host, std::string *port, std::string *path);
60
61 // Figure out the right type of input based on the URL, and create a new Input of the right type.
62 // Will return nullptr if unknown.
63 Input *create_input(const std::string &url, Input::Encoding encoding);
64 Input *create_input(const InputProto &serialized);
65
66 #endif  // !defined(_INPUT_H)