]> git.sesse.net Git - cubemap/blob - server.h
Move Client and Stream into their own files.
[cubemap] / server.h
1 #ifndef _SERVER_H
2 #define _SERVER_H 1
3
4 #include <stdint.h>
5 #include <pthread.h>
6 #include <sys/epoll.h>
7 #include <time.h>
8 #include <string>
9 #include <map>
10 #include <vector>
11
12 #include "client.h"
13 #include "thread.h"
14
15 #define EPOLL_MAX_EVENTS 8192
16 #define EPOLL_TIMEOUT_MS 20
17 #define MAX_CLIENT_REQUEST 16384
18
19 class CubemapStateProto;
20 class MarkPool;
21 class StreamProto;
22
23 class Server : public Thread {
24 public:
25         Server();
26         ~Server();
27
28         // Get the list of all currently connected clients.     
29         std::vector<ClientStats> get_client_stats() const;
30
31         // Set header (both HTTP header and any stream headers) for the given stream.
32         void set_header(const std::string &stream_id, const std::string &header);
33
34         // Set that the given stream should use the given mark pool from now on.
35         // NOTE: This should be set before any clients are connected!
36         void set_mark_pool(const std::string &stream_id, MarkPool *mark_pool);
37
38         // These will be deferred until the next time an iteration in do_work() happens,
39         // and the order between them are undefined.
40         // XXX: header should ideally be ordered with respect to data.
41         void add_client_deferred(int sock);
42         void add_data_deferred(const std::string &stream_id, const char *data, size_t bytes);
43
44         // These should not be called while running, since that would violate
45         // threading assumptions (ie., that epoll is only called from one thread
46         // at the same time).
47         CubemapStateProto serialize();
48         void add_client_from_serialized(const ClientProto &client);
49         void add_stream(const std::string &stream_id, size_t bytes_received);
50         void add_stream_from_serialized(const StreamProto &stream);
51
52 private:
53         // Mutex protecting queued_data only. Note that if you want to hold both this
54         // and <mutex> below, you will need to take <mutex> before this one.
55         mutable pthread_mutex_t queued_data_mutex;
56
57         // Deferred commands that should be run from the do_work() thread as soon as possible.
58         // We defer these for two reasons:
59         //
60         //  - We only want to fiddle with epoll from one thread at any given time,
61         //    and doing add_client() from the acceptor thread would violate that.
62         //  - We don't want the input thread(s) hanging on <mutex> when doing
63         //    add_data(), since they want to do add_data() rather often, and <mutex>
64         //    can be taken a lot of the time.
65         //      
66         // Protected by <queued_data_mutex>.
67         std::vector<int> queued_add_clients;
68         std::map<std::string, std::string> queued_data;
69
70         // All variables below this line are protected by the mutex.
71         mutable pthread_mutex_t mutex;
72
73         // Map from stream ID to stream.
74         std::map<std::string, Stream *> streams;
75
76         // Map from file descriptor to client.
77         std::map<int, Client> clients;
78
79         // Used for epoll implementation (obviously).
80         int epoll_fd;
81         epoll_event events[EPOLL_MAX_EVENTS];
82
83         // The actual worker thread.
84         virtual void do_work();
85
86         // Process a client; read and write data as far as we can.
87         // After this call, one of these four is true:
88         //
89         //  1. The socket is closed, and the client deleted.
90         //  2. We are still waiting for more data from the client.
91         //  3. We've sent all the data we have to the client,
92         //     and put it in <sleeping_clients>.
93         //  4. The socket buffer is full (which means we still have
94         //     data outstanding).
95         //
96         // For #2, we listen for EPOLLIN events. For #3 and #4, we listen
97         // for EPOLLOUT in edge-triggered mode; it will never fire for #3,
98         // but it's cheaper than taking it in and out all the time.
99         void process_client(Client *client);
100
101         // Close a given client socket, and clean up after it.
102         void close_client(Client *client);
103
104         // Parse the HTTP request. Returns a HTTP status code (200/400/404).
105         int parse_request(Client *client);
106
107         // Construct the HTTP header, and set the client into
108         // the SENDING_HEADER state.
109         void construct_header(Client *client);
110
111         // Construct a generic error with the given line, and set the client into
112         // the SENDING_ERROR state.
113         void construct_error(Client *client, int error_code);
114
115         // TODO: This function should probably die.
116         Stream *find_stream(const std::string &stream_id);
117
118         void process_queued_data();
119
120         void add_client(int sock);
121         void add_data(const std::string &stream_id, const char *data, ssize_t bytes);
122 };
123
124 #endif  // !defined(_SERVER_H)