7 #include <sys/socket.h>
10 #include <sys/ioctl.h>
11 #include <sys/epoll.h>
18 #include "mutexlock.h"
24 extern Server *servers;
26 Input::Input(const string &stream_id, const string &url)
27 : stream_id(stream_id),
29 has_metacube_header(false)
37 // Joinable is already the default, but it's good to be certain.
39 pthread_attr_init(&attr);
40 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
41 pthread_create(&worker_thread, &attr, Input::do_work_thunk, this);
48 if (pthread_join(worker_thread, NULL) == -1) {
49 perror("pthread_join");
54 void *Input::do_work_thunk(void *arg)
56 Input *input = static_cast<Input *>(arg);
63 CURL *curl = curl_easy_init();
65 while (!should_stop) {
66 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
67 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Input::curl_callback_thunk);
68 curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
69 curl_easy_perform(curl);
71 printf("Transfer ended, waiting 0.2 seconds and restarting...\n");
77 size_t Input::curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata)
79 Input *input = static_cast<Input *>(userdata);
80 if (input->should_stop) {
84 size_t bytes = size * nmemb;
85 input->curl_callback(ptr, bytes);
89 void Input::curl_callback(char *ptr, size_t bytes)
91 pending_data.insert(pending_data.end(), ptr, ptr + bytes);
94 // If we don't have enough data (yet) for even the Metacube header, just return.
95 if (pending_data.size() < sizeof(metacube_block_header)) {
99 // Make sure we have the Metacube sync header at the start.
100 // We may need to skip over junk data (it _should_ not happen, though).
101 if (!has_metacube_header) {
102 char *ptr = static_cast<char *>(
103 memmem(pending_data.data(), pending_data.size(),
104 METACUBE_SYNC, strlen(METACUBE_SYNC)));
106 // OK, so we didn't find the sync marker. We know then that
107 // we do not have the _full_ marker in the buffer, but we
108 // could have N-1 bytes. Drop everything before that,
110 drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
113 // Yay, we found the header. Drop everything (if anything) before it.
114 drop_pending_data(ptr - pending_data.data());
115 has_metacube_header = true;
117 // Re-check that we have the entire header; we could have dropped data.
118 if (pending_data.size() < sizeof(metacube_block_header)) {
124 // Now it's safe to read the header.
125 metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());
126 assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
127 uint32_t size = ntohl(hdr->size);
128 uint32_t flags = ntohl(hdr->flags);
130 // See if we have the entire block. If not, wait for more data.
131 if (pending_data.size() < sizeof(metacube_block_header) + size) {
135 process_block(pending_data.data() + sizeof(metacube_block_header), size, flags);
137 // Consume this block. This isn't the most efficient way of dealing with things
138 // should we have many blocks, but these routines don't need to be too efficient
140 pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
141 has_metacube_header = false;
145 void Input::process_block(const char *data, uint32_t size, uint32_t flags)
147 if (flags & METACUBE_FLAGS_HEADER) {
148 string header(data, data + size);
149 for (int i = 0; i < NUM_SERVERS; ++i) {
150 servers[i].set_header(stream_id, header);
153 for (int i = 0; i < NUM_SERVERS; ++i) {
154 servers[i].add_data(stream_id, data, size);
159 void Input::drop_pending_data(size_t num_bytes)
161 if (num_bytes == 0) {
164 fprintf(stderr, "Warning: Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?\n",
165 (long long)num_bytes);
166 pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);