#include <assert.h>
#include <arpa/inet.h>
#include <curl/curl.h>
+#include <sys/socket.h>
+#include <pthread.h>
+#include <sys/types.h>
+#include <errno.h>
#include <vector>
+#include <string>
+#include <map>
#include "metacube.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 <data> 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_socket(int server_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<string, Stream> streams;
+
+ // Recover the this pointer, and call do_work().
+ static void *do_work_thunk(void *arg);
+
+ // The actual worker thread.
+ void do_work();
+};
+
+Server::Server()
+{
+ pthread_mutex_init(&mutex, NULL);
+}
+
+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<Server *>(arg);
+ server->do_work();
+ return NULL;
+}
+
+void Server::do_work()
+{
+ for ( ;; ) {
+ printf("server thread running\n");
+ sleep(1);
+ }
+}
+
class Input {
public:
Input();
// TODO: treat it right here
printf("Block: %d bytes, flags=0x%x\n", size, flags);
}
-
+
void Input::drop_pending_data(size_t num_bytes)
{
if (num_bytes == 0) {
return bytes;
}
+int create_server_socket(int port)
+{
+ int server_sock = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
+ if (server_sock == -1) {
+ perror("socket");
+ 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) {
+ perror("setsockopt(IPV6_V6ONLY)");
+ exit(1);
+ }
+
+ sockaddr_in6 addr;
+ memset(&addr, 0, sizeof(addr));
+ addr.sin6_family = AF_INET6;
+ addr.sin6_port = htons(port);
+
+ if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
+ perror("bind");
+ exit(1);
+ }
+
+ if (listen(server_sock, 128) == -1) {
+ perror("listen");
+ exit(1);
+ }
+
+ return server_sock;
+}
+
+void *acceptor_thread_run(void *arg)
+{
+ int server_sock = int(intptr_t(arg));
+ for ( ;; ) {
+ sockaddr_in6 addr;
+ socklen_t addrlen = sizeof(addr);
+
+ int sock = accept(server_sock, reinterpret_cast<sockaddr *>(&addr), &addrlen);
+ if (sock == -1 && errno == EINTR) {
+ continue;
+ }
+ if (sock == -1) {
+ perror("accept");
+ exit(1);
+ }
+
+ printf("got a socket yaaaay\n");
+ }
+}
+
+Server *servers = NULL;
+
int main(int argc, char **argv)
{
+ servers = new Server[NUM_SERVERS];
+ for (int i = 0; i < NUM_SERVERS; ++i) {
+ servers[i].run();
+ }
+
+ int server_sock = create_server_socket(PORT);
+
+ pthread_t acceptor_thread;
+ pthread_create(&acceptor_thread, NULL, acceptor_thread_run, reinterpret_cast<void *>(server_sock));
+
Input input;
CURL *curl = curl_easy_init();
- curl_easy_setopt(curl, CURLOPT_URL, "http://gruessi.zrh.sesse.net:4013/");
- // curl_easy_setopt(curl, CURLOPT_URL, "http://www.google.com/");
+ curl_easy_setopt(curl, CURLOPT_URL, STREAM_URL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &input);
curl_easy_perform(curl);