X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=cubemap.cpp;h=692d48baf1d9e507d36926aa10a7f35f7d8847b1;hp=643f1a8b0b6c7a0e1bc274a47ae65ebf7e18df76;hb=f3e2d52ccf79454ee157917f9017263a8f4c50c3;hpb=2bd0c02ac79dc8bc0da62b27d6b1e9f3cea525c6 diff --git a/cubemap.cpp b/cubemap.cpp index 643f1a8..692d48b 100644 --- a/cubemap.cpp +++ b/cubemap.cpp @@ -13,303 +13,20 @@ #include #include #include + #include "metacube.h" +#include "server.h" +#include "input.h" #define NUM_SERVERS 4 #define STREAM_ID "stream" #define STREAM_URL "http://gruessi.zrh.sesse.net:4013/" -#define BACKLOG_SIZE 1048576 #define PORT 9094 using namespace std; -// Locks a pthread mutex, RAII-style. -class MutexLock { -public: - MutexLock(pthread_mutex_t *mutex); - ~MutexLock(); - -private: - pthread_mutex_t *mutex; -}; - -MutexLock::MutexLock(pthread_mutex_t *mutex) - : mutex(mutex) -{ - pthread_mutex_lock(mutex); -} - -MutexLock::~MutexLock() -{ - pthread_mutex_unlock(mutex); -} - -struct Client { - enum State { READING_REQUEST, SENDING_HEADER, SENDING_DATA }; - State state; - - // The HTTP request, as sent by the client. If we are in READING_REQUEST, - // this might not be finished. - string client_request; - -#if 0 - // What stream we're connecting to; parsed from client_request. - // Not relevant for READING_REQUEST. - string stream_id; -#endif - - // Number of bytes we've sent of the header. Only relevant for SENDING_HEADER. - size_t header_bytes_sent; - - // Number of bytes we've sent of data. Only relevant for SENDING_DATA. - size_t bytes_sent; -}; - -struct Stream { - // The HTTP response header, plus the video stream header (if any). - string header; - - // The stream data itself, stored in a circular buffer. - char data[BACKLOG_SIZE]; - - // How many bytes contains. Can very well be larger than BACKLOG_SIZE, - // since the buffer wraps. - size_t data_size; -}; - -class Server { -public: - Server(); - - // Start a new thread that handles clients. - void run(); - void add_client(int sock); - void add_stream(const string &stream_id); - void set_header(const string &stream_id, const string &header); - void add_data(const string &stream_id, const char *data, size_t bytes); - -private: - pthread_mutex_t mutex; - - // Map from stream ID to stream. - map streams; - - // Map from file descriptor to client. - map clients; - - int epoll_fd; - - // Recover the this pointer, and call do_work(). - static void *do_work_thunk(void *arg); - - // The actual worker thread. - void do_work(); -}; - Server *servers = NULL; -Server::Server() -{ - pthread_mutex_init(&mutex, NULL); - - epoll_fd = epoll_create(1024); // Size argument is ignored. - if (epoll_fd == -1) { - perror("epoll_fd"); - exit(1); - } -} - -void Server::run() -{ - pthread_t thread; - pthread_create(&thread, NULL, Server::do_work_thunk, this); -} - -void *Server::do_work_thunk(void *arg) -{ - Server *server = static_cast(arg); - server->do_work(); - return NULL; -} - -void Server::do_work() -{ - for ( ;; ) { - MutexLock lock(&mutex); - printf("server thread running\n"); - sleep(1); - } -} - -void Server::add_client(int sock) -{ - MutexLock lock(&mutex); - Client new_client; - new_client.state = Client::READING_REQUEST; - new_client.header_bytes_sent = 0; - new_client.bytes_sent = 0; - - clients.insert(make_pair(sock, new_client)); - - // Start listening on data from this socket. - epoll_event ev; - ev.events = EPOLLIN; - ev.data.fd = sock; - if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) { - perror("epoll_ctl(EPOLL_CTL_ADD)"); - exit(1); - } -} - -void Server::add_stream(const string &stream_id) -{ - // TODO -} - -void Server::set_header(const string &stream_id, const string &header) -{ - // TODO - printf("got header! %lu bytes\n", header.size()); -} - -void Server::add_data(const string &stream_id, const char *data, size_t bytes) -{ - // TODO -} - -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 bytes from the head of , - // 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 pending_data; - - // If 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(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( - 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(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); @@ -318,6 +35,12 @@ int create_server_socket(int port) exit(1); } + int one = 1; + if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) { + perror("setsockopt(SO_REUSEADDR)"); + exit(1); + } + // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...) int zero = 0; if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) { @@ -389,4 +112,9 @@ int main(int argc, char **argv) Input input(STREAM_ID); input.run(STREAM_URL); + + for (int i = 0; i < NUM_SERVERS; ++i) { + servers[i].stop(); + } + delete[] servers; }