]> git.sesse.net Git - cubemap/blob - cubemap.cpp
cbdab184dc7f5e3be2c098b65884ab4a1083c4b1
[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 <vector>
8 #include "metacube.h"
9
10 using namespace std;
11
12 class Input {
13 public:
14         Input();
15         void curl_callback(char *ptr, size_t bytes);
16
17 private:
18         void process_block(const char *data, uint32_t size, uint32_t flags);
19         void drop_pending_data(size_t num_bytes);
20
21         // Data we have received but not fully processed yet.
22         vector<char> pending_data;
23
24         // If <pending_data> starts with a Metacube header,
25         // this is true.
26         bool has_metacube_header;
27 };
28
29 Input::Input()
30         : has_metacube_header(false)
31 {
32 }
33         
34 void Input::curl_callback(char *ptr, size_t bytes)
35 {
36         pending_data.insert(pending_data.end(), ptr, ptr + bytes);
37
38         for ( ;; ) {
39                 // If we don't have enough data (yet) for even the Metacube header, just return.
40                 if (pending_data.size() < sizeof(metacube_block_header)) {
41                         return;
42                 }
43
44                 // Make sure we have the Metacube sync header at the start.
45                 // We may need to skip over junk data (it _should_ not happen, though).
46                 if (!has_metacube_header) {
47                         char *ptr = static_cast<char *>(
48                                 memmem(pending_data.data(), pending_data.size(),
49                                        METACUBE_SYNC, strlen(METACUBE_SYNC)));
50                         if (ptr == NULL) {
51                                 // OK, so we didn't find the sync marker. We know then that
52                                 // we do not have the _full_ marker in the buffer, but we
53                                 // could have N-1 bytes. Drop everything before that,
54                                 // and then give up.
55                                 drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
56                                 return;
57                         } else {
58                                 // Yay, we found the header. Drop everything (if anything) before it.
59                                 drop_pending_data(ptr - pending_data.data());
60                                 has_metacube_header = true;
61
62                                 // Re-check that we have the entire header; we could have dropped data.
63                                 if (pending_data.size() < sizeof(metacube_block_header)) {
64                                         return;
65                                 }
66                         }
67                 }
68
69                 // Now it's safe to read the header.
70                 metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());    
71                 assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
72                 uint32_t size = ntohl(hdr->size);
73                 uint32_t flags = ntohl(hdr->flags);
74
75                 // See if we have the entire block. If not, wait for more data.
76                 if (pending_data.size() < sizeof(metacube_block_header) + size) {
77                         return;
78                 }
79
80                 process_block(pending_data.data(), size, flags);
81
82                 // Consume this block. This isn't the most efficient way of dealing with things
83                 // should we have many blocks, but these routines don't need to be too efficient
84                 // anyway.
85                 pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
86         }
87 }
88                 
89 void Input::process_block(const char *data, uint32_t size, uint32_t flags)
90 {
91         // TODO: treat it right here
92         printf("Block: %d bytes, flags=0x%x\n", size, flags);
93 }
94                         
95 void Input::drop_pending_data(size_t num_bytes)
96 {
97         if (num_bytes == 0) {
98                 return;
99         }
100         fprintf(stderr, "Warning: Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?\n",
101                 (long long)num_bytes);
102         pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
103 }
104
105 size_t curl_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
106 {
107         Input *input = static_cast<Input *>(userdata);
108         size_t bytes = size * nmemb;
109         input->curl_callback(ptr, bytes);       
110         return bytes;
111 }
112
113 int main(int argc, char **argv)
114 {
115         Input input;
116         CURL *curl = curl_easy_init();
117         curl_easy_setopt(curl, CURLOPT_URL, "http://gruessi.zrh.sesse.net:4013/");
118         // curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
119         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
120         curl_easy_setopt(curl, CURLOPT_WRITEDATA, &input);
121         curl_easy_perform(curl);
122 }