15 void curl_callback(char *ptr, size_t bytes);
18 void process_block(const char *data, uint32_t size, uint32_t flags);
19 void drop_pending_data(size_t num_bytes);
21 // Data we have received but not fully processed yet.
22 vector<char> pending_data;
24 // If <pending_data> starts with a Metacube header,
26 bool has_metacube_header;
30 : has_metacube_header(false)
34 void Input::curl_callback(char *ptr, size_t bytes)
36 pending_data.insert(pending_data.end(), ptr, ptr + bytes);
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)) {
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)));
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,
55 drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
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;
62 // Re-check that we have the entire header; we could have dropped data.
63 if (pending_data.size() < sizeof(metacube_block_header)) {
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);
75 // See if we have the entire block. If not, wait for more data.
76 if (pending_data.size() < sizeof(metacube_block_header) + size) {
80 process_block(pending_data.data(), size, flags);
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
85 pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
89 void Input::process_block(const char *data, uint32_t size, uint32_t flags)
91 // TODO: treat it right here
92 printf("Block: %d bytes, flags=0x%x\n", size, flags);
95 void Input::drop_pending_data(size_t num_bytes)
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);
105 size_t curl_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
107 Input *input = static_cast<Input *>(userdata);
108 size_t bytes = size * nmemb;
109 input->curl_callback(ptr, bytes);
113 int main(int argc, char **argv)
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);