]> git.sesse.net Git - cubemap/blob - input.h
8a26ce6d1baad5887fe2b7831618c0fb0304ef9c
[cubemap] / input.h
1 #ifndef _INPUT_H
2 #define _INPUT_H 1
3
4 #include <vector>
5 #include <string>
6
7 class Input {
8 public:
9         Input(const std::string &stream_id, const std::string &url);
10
11         // Connect to the given URL and start streaming.
12         void run();
13
14         // Stops the streaming, but lets the file descriptor stay open.
15         void stop();
16
17 private:
18         // Recovers the this pointer and calls do_work().
19         static void *do_work_thunk(void *arg);
20
21         // Actually does the download.
22         void do_work();
23         
24         // Open a socket that connects to the given host and port. Does DNS resolving.
25         int lookup_and_connect(const std::string &host, const std::string &port);
26
27         // Stores the given data, looks for Metacube blocks (skipping data if needed),
28         // and calls process_block() for each one.
29         void process_data(char *ptr, size_t bytes);
30
31         // Drops <num_bytes> bytes from the head of <pending_data>,
32         // and outputs a warning.
33         void drop_pending_data(size_t num_bytes);
34
35         enum State {
36                 NOT_CONNECTED,
37                 SENDING_REQUEST,
38                 RECEIVING_HEADER,
39                 RECEIVING_DATA,
40                 CLOSING_SOCKET,  // Due to error.
41         };
42         State state;
43
44         std::string stream_id;
45
46         // The URL and its parsed components.
47         std::string url;
48         std::string host, port, path;
49
50         // The HTTP request, with headers and all.
51         // Only relevant for SENDING_REQUEST.
52         std::string request;
53
54         // How many bytes we've sent of the request so far.
55         // Only relevant for SENDING_REQUEST.
56         size_t request_bytes_sent;
57
58         // The HTTP response we've received so far. Only relevant for RECEIVING_HEADER.
59         std::string response;
60
61         // Data we have received but not fully processed yet.
62         std::vector<char> pending_data;
63
64         // If <pending_data> starts with a Metacube header,
65         // this is true.
66         bool has_metacube_header;
67
68         // The socket we are downloading on (or -1).
69         int sock;       
70
71         // Handle to the thread that actually does the download.
72         pthread_t worker_thread;
73
74         // Whether we should stop or not.
75         volatile bool should_stop;
76 };
77
78 #endif  // !defined(_INPUT_H)