#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;
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) {
}
}
+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);