]> git.sesse.net Git - cubemap/blob - client.h
Fix a memory leak in Server (streams were leaked).
[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 struct ClientStats {
15         std::string stream_id;
16         int sock;
17         int fwmark;
18         std::string remote_addr;
19         time_t connect_time;
20         size_t bytes_sent;
21         size_t bytes_lost;
22         size_t num_loss_events;
23 };
24
25 struct Client {
26         Client() {}
27         Client(int sock);
28
29         // Serialization/deserialization.
30         Client(const ClientProto &serialized, Stream *stream);
31         ClientProto serialize() const;
32
33         ClientStats get_stats() const;
34
35         // The file descriptor associated with this socket.
36         int sock;
37
38         // The fwmark associated with this socket (or 0).
39         int fwmark;
40
41         // Some information only used for logging.
42         std::string remote_addr;
43         time_t connect_time;
44
45         enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA, SENDING_ERROR };
46         State state;
47
48         // The HTTP request, as sent by the client. If we are in READING_REQUEST,
49         // this might not be finished.
50         std::string request;
51
52         // What stream we're connecting to; parsed from <request>.
53         // Not relevant for READING_REQUEST.
54         std::string stream_id;
55         Stream *stream;
56
57         // The header we want to send. This is nominally a copy of Stream::header,
58         // but since that might change on reconnects etc., we keep a local copy here.
59         // Only relevant for SENDING_HEADER or SENDING_ERROR; blank otherwise.
60         std::string header_or_error;
61
62         // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER
63         // or SENDING_ERROR.
64         size_t header_or_error_bytes_sent;
65
66         // Number of bytes we are into the stream (ie., the end of last send).
67         // Only relevant for SENDING_DATA.
68         size_t stream_pos;
69
70         // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
71         size_t bytes_sent;
72
73         // Number of times we've skipped forward due to the backlog being too big,
74         // and how many bytes we've skipped over in all. Only relevant for SENDING_DATA.
75         size_t bytes_lost, num_loss_events;
76 };
77
78 #endif  // !defined(_CLIENT_H)