]> git.sesse.net Git - cubemap/blob - client.h
Do not serialize prebuffering_bytes in StreamProto.
[cubemap] / client.h
1 #ifndef _CLIENT_H
2 #define _CLIENT_H 1
3
4 // A Client represents a single connection from a client (watching a single stream).
5
6 #include <stddef.h>
7 #include <time.h>
8 #include <string>
9
10 #include "tlse.h"
11
12 class ClientProto;
13 struct Stream;
14
15 // Digested statistics for writing to logs etc.
16 // Note that "referer" and "user_agent" here are already escaped for scary characters.
17 struct ClientStats {
18         std::string url;
19         int sock;
20         std::string remote_addr;
21         std::string referer;
22         std::string user_agent;
23         timespec connect_time;
24         size_t bytes_sent;
25         size_t bytes_lost;
26         size_t num_loss_events;
27 };
28
29 struct Client {
30         Client(int sock);
31
32         // Serialization/deserialization.
33         Client(const ClientProto &serialized, Stream *stream);
34         ClientProto serialize() const;
35
36         ClientStats get_stats() const;
37
38         // The file descriptor associated with this socket.
39         int sock;
40
41         // When the client connected, in terms of CLOCK_MONOTONIC_COARSE.
42         timespec connect_time;
43
44         // Some information only used for logging.
45         std::string remote_addr;
46         std::string referer;
47         std::string user_agent;
48
49         enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_SHORT_RESPONSE, WAITING_FOR_KEYFRAME, PREBUFFERING };
50         State state = READING_REQUEST;
51
52         // The HTTP request, as sent by the client. If we are in READING_REQUEST,
53         // this might not be finished.
54         std::string request;
55
56         // What stream we're connecting to; parsed from <request>.
57         // Not relevant for READING_REQUEST.
58         std::string url;
59         Stream *stream = nullptr;
60
61         // The header we want to send, or the response with headers if we know
62         // it in its entirety after reading the request (typically an error).
63         // This is nominally a copy of Stream::header, but since that might
64         // change on reconnects etc., we keep a local copy here. Only relevant
65         // for SENDING_HEADER or SENDING_SHORT_RESPONSE; blank otherwise.
66         std::string header_or_short_response;
67
68         // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER
69         // or SENDING_SHORT_RESPONSE.
70         size_t header_or_short_response_bytes_sent = 0;
71
72         // Number of bytes we are into the stream (ie., the end of last send).
73         // -1 means we want to send from the end of the backlog (the normal case),
74         // although only at a keyframe.
75         // -2 means we want to send from the _beginning_ of the backlog.
76         // Once we go into WAITING_FOR_KEYFRAME, PREBUFFERING or SENDING_DATA,
77         // these negative values will be translated to real numbers.
78         size_t stream_pos = 0;
79
80         // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
81         size_t bytes_sent = 0;
82
83         // Number of times we've skipped forward due to the backlog being too big,
84         // and how many bytes we've skipped over in all. Only relevant for SENDING_DATA.
85         size_t bytes_lost = 0, num_loss_events = 0;
86
87         TLSContext *tls_context = nullptr;
88         const unsigned char *tls_data_to_send = nullptr;
89         unsigned tls_data_left_to_send = 0;
90         bool in_ktls_mode = false;
91 };
92
93 #endif  // !defined(_CLIENT_H)