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