]> git.sesse.net Git - cubemap/blob - client.h
f1d14e65cf2cbc321770d069aeed8f93fc087f94
[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         // Whether we should close the connection after sending the response.
62         // Not relevant for READING_REQUEST. Must be true if http_11 is false.
63         bool close_after_response;
64
65         // Whether the request came in over HTTP/1.1 or higher.
66         bool http_11;
67
68         // The header we want to send, or the response with headers if we know
69         // it in its entirety after reading the request (typically an error).
70         // This is nominally a copy of Stream::header, but since that might
71         // change on reconnects etc., we keep a local copy here. Only relevant
72         // for SENDING_HEADER or SENDING_SHORT_RESPONSE; blank otherwise.
73         //
74         // Must start with the string "HTTP/1.0 ", which will be changed to 1.1
75         // if relevant.
76         std::string header_or_short_response;
77
78         // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER
79         // or SENDING_SHORT_RESPONSE.
80         size_t header_or_short_response_bytes_sent = 0;
81
82         // Number of bytes we are into the stream (ie., the end of last send).
83         // -1 means we want to send from the end of the backlog (the normal case),
84         // although only at a keyframe.
85         // -2 means we want to send from the _beginning_ of the backlog.
86         // Once we go into WAITING_FOR_KEYFRAME, PREBUFFERING or SENDING_DATA,
87         // these negative values will be translated to real numbers.
88         size_t stream_pos = 0;
89
90         // Position at which to end the stream (one-past-the-end, used for fragments).
91         // -1 means never to end; this is the common case.
92         size_t stream_pos_end = 0;
93
94         // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
95         size_t bytes_sent = 0;
96
97         // Number of times we've skipped forward due to the backlog being too big,
98         // and how many bytes we've skipped over in all. Only relevant for SENDING_DATA.
99         size_t bytes_lost = 0, num_loss_events = 0;
100
101         TLSContext *tls_context = nullptr;
102         const unsigned char *tls_data_to_send = nullptr;
103         unsigned tls_data_left_to_send = 0;
104         bool in_ktls_mode = false;
105 };
106
107 #endif  // !defined(_CLIENT_H)