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