X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=cubemap.cpp;h=9f5e42e6a23b0f5cff0c37b3fecaded17b13b522;hp=b02121d78b8b6013fbafd2942cadee6f35192fa2;hb=418ca367be670db5377ca99462906eb9c73d296b;hpb=e40eedd621354cb0f634f50a559b7bca1fc11123 diff --git a/cubemap.cpp b/cubemap.cpp index b02121d..9f5e42e 100644 --- a/cubemap.cpp +++ b/cubemap.cpp @@ -17,9 +17,31 @@ #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; @@ -55,15 +77,52 @@ struct Stream { 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 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(arg); + server->do_work(); + return NULL; +} + +void Server::do_work() +{ + for ( ;; ) { + printf("server thread running\n"); + sleep(1); + } +} + class Input { public: Input(); @@ -146,7 +205,7 @@ void Input::process_block(const char *data, uint32_t size, uint32_t flags) // 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) { @@ -218,11 +277,13 @@ void *acceptor_thread_run(void *arg) } } +Server *servers = NULL; + int main(int argc, char **argv) { + servers = new Server[NUM_SERVERS]; for (int i = 0; i < NUM_SERVERS; ++i) { - Server *s = new Server; - //s->run(); + servers[i].run(); } int server_sock = create_server_socket(PORT);