]> git.sesse.net Git - cubemap/commitdiff
Split Input into its own file.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 14:10:21 +0000 (16:10 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 6 Apr 2013 14:48:53 +0000 (16:48 +0200)
cubemap.cpp
input.cpp [new file with mode: 0644]
input.h [new file with mode: 0644]

index 49889f2e28bec81da0e90a51ab43ad8978f3e0c7..98db848152a380b9a279f4ac29e57ceedcf0a8d0 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "metacube.h"
 #include "server.h"
+#include "input.h"
 
 #define NUM_SERVERS 4
 #define STREAM_ID "stream"
@@ -26,139 +27,6 @@ using namespace std;
 
 Server *servers = NULL;
 
-class Input {
-public:
-       Input(const string &stream_id);
-
-       // Connect to the given URL and start streaming.
-       // WARNING: Currently this blocks; it does not run in a separate thread!
-       void run(const string &url);
-
-private:
-       // Recovers the this pointer and calls curl_callback().
-       static size_t curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata);
-
-       // Stores the given data, looks for Metacube blocks (skipping data if needed),
-       // and calls process_block() for each one.
-       void curl_callback(char *ptr, size_t bytes);
-       void process_block(const char *data, uint32_t size, uint32_t flags);
-
-       // Drops <num_bytes> bytes from the head of <pending_data>,
-       // and outputs a warning.
-       void drop_pending_data(size_t num_bytes);
-
-       string stream_id;
-
-       // Data we have received but not fully processed yet.
-       vector<char> pending_data;
-
-       // If <pending_data> starts with a Metacube header,
-       // this is true.
-       bool has_metacube_header;
-};
-
-Input::Input(const string &stream_id)
-       : stream_id(stream_id),
-         has_metacube_header(false)
-{
-}
-
-void Input::run(const string &url)
-{
-       CURL *curl = curl_easy_init();
-       curl_easy_setopt(curl, CURLOPT_URL, STREAM_URL);
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Input::curl_callback_thunk);
-       curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
-       curl_easy_perform(curl);
-}
-
-size_t Input::curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata)
-{
-       Input *input = static_cast<Input *>(userdata);
-       size_t bytes = size * nmemb;
-       input->curl_callback(ptr, bytes);       
-       return bytes;
-}
-       
-void Input::curl_callback(char *ptr, size_t bytes)
-{
-       pending_data.insert(pending_data.end(), ptr, ptr + bytes);
-
-       for ( ;; ) {
-               // If we don't have enough data (yet) for even the Metacube header, just return.
-               if (pending_data.size() < sizeof(metacube_block_header)) {
-                       return;
-               }
-
-               // Make sure we have the Metacube sync header at the start.
-               // We may need to skip over junk data (it _should_ not happen, though).
-               if (!has_metacube_header) {
-                       char *ptr = static_cast<char *>(
-                               memmem(pending_data.data(), pending_data.size(),
-                                      METACUBE_SYNC, strlen(METACUBE_SYNC)));
-                       if (ptr == NULL) {
-                               // OK, so we didn't find the sync marker. We know then that
-                               // we do not have the _full_ marker in the buffer, but we
-                               // could have N-1 bytes. Drop everything before that,
-                               // and then give up.
-                               drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
-                               return;
-                       } else {
-                               // Yay, we found the header. Drop everything (if anything) before it.
-                               drop_pending_data(ptr - pending_data.data());
-                               has_metacube_header = true;
-
-                               // Re-check that we have the entire header; we could have dropped data.
-                               if (pending_data.size() < sizeof(metacube_block_header)) {
-                                       return;
-                               }
-                       }
-               }
-
-               // Now it's safe to read the header.
-               metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());    
-               assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
-               uint32_t size = ntohl(hdr->size);
-               uint32_t flags = ntohl(hdr->flags);
-
-               // See if we have the entire block. If not, wait for more data.
-               if (pending_data.size() < sizeof(metacube_block_header) + size) {
-                       return;
-               }
-
-               process_block(pending_data.data(), size, flags);
-
-               // Consume this block. This isn't the most efficient way of dealing with things
-               // should we have many blocks, but these routines don't need to be too efficient
-               // anyway.
-               pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
-       }
-}
-               
-void Input::process_block(const char *data, uint32_t size, uint32_t flags)
-{      
-       if (flags & METACUBE_FLAGS_HEADER) {
-               string header(data, data + size);
-               for (int i = 0; i < NUM_SERVERS; ++i) {
-                       servers[i].set_header(stream_id, header);
-               }
-       } else { 
-               for (int i = 0; i < NUM_SERVERS; ++i) {
-                       servers[i].add_data(stream_id, data, size);
-               }
-       }
-}
-
-void Input::drop_pending_data(size_t num_bytes)
-{
-       if (num_bytes == 0) {
-               return;
-       }
-       fprintf(stderr, "Warning: Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?\n",
-               (long long)num_bytes);
-       pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
-}
-
 int create_server_socket(int port)
 {
        int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
diff --git a/input.cpp b/input.cpp
new file mode 100644 (file)
index 0000000..29e4ed3
--- /dev/null
+++ b/input.cpp
@@ -0,0 +1,126 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <assert.h>
+#include <arpa/inet.h>
+#include <curl/curl.h>
+#include <sys/socket.h>
+#include <pthread.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/epoll.h>
+#include <errno.h>
+#include <vector>
+#include <string>
+#include <map>
+
+#include "metacube.h"
+#include "input.h"
+#include "server.h"
+
+using namespace std;
+
+extern Server *servers;
+
+Input::Input(const string &stream_id)
+       : stream_id(stream_id),
+         has_metacube_header(false)
+{
+}
+
+void Input::run(const string &url)
+{
+       CURL *curl = curl_easy_init();
+       curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Input::curl_callback_thunk);
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, this);
+       curl_easy_perform(curl);
+}
+
+size_t Input::curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata)
+{
+       Input *input = static_cast<Input *>(userdata);
+       size_t bytes = size * nmemb;
+       input->curl_callback(ptr, bytes);       
+       return bytes;
+}
+       
+void Input::curl_callback(char *ptr, size_t bytes)
+{
+       pending_data.insert(pending_data.end(), ptr, ptr + bytes);
+
+       for ( ;; ) {
+               // If we don't have enough data (yet) for even the Metacube header, just return.
+               if (pending_data.size() < sizeof(metacube_block_header)) {
+                       return;
+               }
+
+               // Make sure we have the Metacube sync header at the start.
+               // We may need to skip over junk data (it _should_ not happen, though).
+               if (!has_metacube_header) {
+                       char *ptr = static_cast<char *>(
+                               memmem(pending_data.data(), pending_data.size(),
+                                      METACUBE_SYNC, strlen(METACUBE_SYNC)));
+                       if (ptr == NULL) {
+                               // OK, so we didn't find the sync marker. We know then that
+                               // we do not have the _full_ marker in the buffer, but we
+                               // could have N-1 bytes. Drop everything before that,
+                               // and then give up.
+                               drop_pending_data(pending_data.size() - (strlen(METACUBE_SYNC) - 1));
+                               return;
+                       } else {
+                               // Yay, we found the header. Drop everything (if anything) before it.
+                               drop_pending_data(ptr - pending_data.data());
+                               has_metacube_header = true;
+
+                               // Re-check that we have the entire header; we could have dropped data.
+                               if (pending_data.size() < sizeof(metacube_block_header)) {
+                                       return;
+                               }
+                       }
+               }
+
+               // Now it's safe to read the header.
+               metacube_block_header *hdr = reinterpret_cast<metacube_block_header *>(pending_data.data());    
+               assert(memcmp(hdr->sync, METACUBE_SYNC, sizeof(hdr->sync)) == 0);
+               uint32_t size = ntohl(hdr->size);
+               uint32_t flags = ntohl(hdr->flags);
+
+               // See if we have the entire block. If not, wait for more data.
+               if (pending_data.size() < sizeof(metacube_block_header) + size) {
+                       return;
+               }
+
+               process_block(pending_data.data(), size, flags);
+
+               // Consume this block. This isn't the most efficient way of dealing with things
+               // should we have many blocks, but these routines don't need to be too efficient
+               // anyway.
+               pending_data.erase(pending_data.begin(), pending_data.begin() + sizeof(metacube_block_header) + size);
+       }
+}
+               
+void Input::process_block(const char *data, uint32_t size, uint32_t flags)
+{      
+       if (flags & METACUBE_FLAGS_HEADER) {
+               string header(data, data + size);
+               for (int i = 0; i < NUM_SERVERS; ++i) {
+                       servers[i].set_header(stream_id, header);
+               }
+       } else { 
+               for (int i = 0; i < NUM_SERVERS; ++i) {
+                       servers[i].add_data(stream_id, data, size);
+               }
+       }
+}
+
+void Input::drop_pending_data(size_t num_bytes)
+{
+       if (num_bytes == 0) {
+               return;
+       }
+       fprintf(stderr, "Warning: Dropping %lld junk bytes from stream, maybe it is not a Metacube stream?\n",
+               (long long)num_bytes);
+       pending_data.erase(pending_data.begin(), pending_data.begin() + num_bytes);
+}
+
diff --git a/input.h b/input.h
new file mode 100644 (file)
index 0000000..c293071
--- /dev/null
+++ b/input.h
@@ -0,0 +1,38 @@
+#ifndef _INPUT_H
+#define _INPUT_H 1
+
+#include <vector>
+#include <string>
+
+class Input {
+public:
+       Input(const std::string &stream_id);
+
+       // Connect to the given URL and start streaming.
+       // WARNING: Currently this blocks; it does not run in a separate thread!
+       void run(const std::string &url);
+
+private:
+       // Recovers the this pointer and calls curl_callback().
+       static size_t curl_callback_thunk(char *ptr, size_t size, size_t nmemb, void *userdata);
+
+       // Stores the given data, looks for Metacube blocks (skipping data if needed),
+       // and calls process_block() for each one.
+       void curl_callback(char *ptr, size_t bytes);
+       void process_block(const char *data, uint32_t size, uint32_t flags);
+
+       // Drops <num_bytes> bytes from the head of <pending_data>,
+       // and outputs a warning.
+       void drop_pending_data(size_t num_bytes);
+
+       std::string stream_id;
+
+       // Data we have received but not fully processed yet.
+       std::vector<char> pending_data;
+
+       // If <pending_data> starts with a Metacube header,
+       // this is true.
+       bool has_metacube_header;
+};
+
+#endif  // !defined(_INPUT_H)