]> git.sesse.net Git - cubemap/blob - cubemap.cpp
Start adding scaffolds for most of the basic classes.
[cubemap] / cubemap.cpp
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdint.h>
4 #include <assert.h>
5 #include <arpa/inet.h>
6 #include <curl/curl.h>
7 #include <sys/socket.h>
8 #include <vector>
9 #include <string>
10 #include <map>
11 #include "metacube.h"
12
13 #define NUM_SERVERS 4
14 #define STREAM_ID "stream"
15 #define STREAM_URL "http://gruessi.zrh.sesse.net:4013/"
16 #define BACKLOG_SIZE 1048576
17
18 using namespace std;
19
20 struct Client {
21         enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA };
22         State state;
23
24         // The HTTP request, as sent by the client. If we are in READING_REQUEST,
25         // this might not be finished.
26         string client_request;
27
28 #if 0
29         // What stream we're connecting to; parsed from client_request.
30         // Not relevant for READING_REQUEST.
31         string stream_id;
32 #endif
33
34         // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER.
35         size_t header_bytes_sent;
36
37         // Number of bytes we've sent of data. Only relevant for SENDING_DATA.
38         size_t bytes_sent;
39 };
40
41 struct Stream {
42         // The HTTP response header, plus the video stream header (if any).
43         string header;
44
45         // The stream data itself, stored in a circular buffer.
46         char data[BACKLOG_SIZE];
47
48         // How many bytes <data> contains. Can very well be larger than BACKLOG_SIZE,
49         // since the buffer wraps.
50         size_t data_size;
51 };
52
53 class Server {
54 public:
55         void add_socket(int server_sock);
56         void add_stream(const string &stream_id);
57         void set_header(const string &stream_id, const string &header);
58         void add_data(const string &stream_id, const char *data, size_t bytes);
59
60 private:
61         map<string, Stream> streams;
62 };
63
64 class Input {
65 public:
66         Input();
67         void curl_callback(char *ptr, size_t bytes);
68
69 private:
70         void process_block(const char *data, uint32_t size, uint32_t flags);
71         void drop_pending_data(size_t num_bytes);
72
73         // Data we have received but not fully processed yet.
74         vector<char> pending_data;
75
76         // If <pending_data> starts with a Metacube header,
77         // this is true.
78         bool has_metacube_header;
79 };
80
81 Input::Input()
82         : has_metacube_header(false)
83 {
84 }
85         
86 void Input::curl_callback(char *ptr, size_t bytes)
87 {
88         pending_data.insert(pending_data.end(), ptr, ptr + bytes);
89
90         for ( ;; ) {
91                 // If we don't have enough data (yet) for even the Metacube header, just return.
92                 if (pending_data.size() < sizeof(metacube_block_header)) {
93                         return;
94                 }
95
96                 // Make sure we have the Metacube sync header at the start.
97                 // We may need to skip over junk data (it _should_ not happen, though).
98                 if (!has_metacube_header) {
99                         char *ptr = static_cast<char *>(
100                                 memmem(pending_data.data(), pending_data.size(),
101                                        METACUBE_SYNC, strlen(METACUBE_SYNC)));
102                         if (ptr == NULL) {
103                                 // OK, so we didn't find the sync marker. We know then that
104                                 // we do not have the _full_ marker in the buffer, but we
105                                 // could have N-1 bytes. Drop everything before that,
106                                 // and then give up.
107                                 drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
108                                 return;
109                         } else {
110                                 // Yay, we found the header. Drop everything (if anything) before it.
111                                 drop_pending_data(ptr - pending_data.data());
112                                 has_metacube_header = true;
113
114                                 // Re-check that we have the entire header; we could have dropped data.
115                                 if (pending_data.size() < sizeof(metacube_block_header)) {
116                                         return;
117                                 }
118                         }
119                 }
120
121                 // Now it's safe to read the header.
122                 metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());    
123                 assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
124                 uint32_t size = ntohl(hdr->size);
125                 uint32_t flags = ntohl(hdr->flags);
126
127                 // See if we have the entire block. If not, wait for more data.
128                 if (pending_data.size() < sizeof(metacube_block_header) + size) {
129                         return;
130                 }
131
132                 process_block(pending_data.data(), size, flags);
133
134                 // Consume this block. This isn't the most efficient way of dealing with things
135                 // should we have many blocks, but these routines don't need to be too efficient
136                 // anyway.
137                 pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
138         }
139 }
140                 
141 void Input::process_block(const char *data, uint32_t size, uint32_t flags)
142 {
143         // TODO: treat it right here
144         printf("Block: %d bytes, flags=0x%x\n", size, flags);
145 }
146                         
147 void Input::drop_pending_data(size_t num_bytes)
148 {
149         if (num_bytes == 0) {
150                 return;
151         }
152         fprintf(stderr, "Warning: Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?\n",
153                 (long long)num_bytes);
154         pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
155 }
156
157 size_t curl_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
158 {
159         Input *input = static_cast<Input *>(userdata);
160         size_t bytes = size * nmemb;
161         input->curl_callback(ptr, bytes);       
162         return bytes;
163 }
164
165 int main(int argc, char **argv)
166 {
167         Input input;
168         CURL *curl = curl_easy_init();
169         curl_easy_setopt(curl, CURLOPT_URL, "http://gruessi.zrh.sesse.net:4013/");
170         // curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
171         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
172         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &input);
173         curl_easy_perform(curl);
174 }