]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Even more missing unistd.h includes.
[cubemap] / server.cpp
index 6e1005e1090ccd851c5690f3f6f4174c6c00f8ab..88819129d45a4d1a85fdd86f156bc4617b61f716 100644 (file)
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
+#include <unistd.h>
 #include <assert.h>
 #include <arpa/inet.h>
 #include <sys/socket.h>
@@ -16,6 +17,7 @@
 #include <map>
 #include <algorithm>
 
+#include "markpool.h"
 #include "metacube.h"
 #include "server.h"
 #include "mutexlock.h"
@@ -26,6 +28,7 @@ using namespace std;
 
 Client::Client(int sock)
        : sock(sock),
+         fwmark(0),
          connect_time(time(NULL)),
          state(Client::READING_REQUEST),
          stream(NULL),
@@ -64,6 +67,16 @@ Client::Client(const ClientProto &serialized, Stream *stream)
          header_or_error_bytes_sent(serialized.header_or_error_bytes_sent()),
          bytes_sent(serialized.bytes_sent())
 {
+       if (stream->mark_pool != NULL) {
+               fwmark = stream->mark_pool->get_mark();
+       } else {
+               fwmark = 0;  // No mark.
+       }
+       if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {
+               if (fwmark != 0) {
+                       perror("setsockopt(SO_MARK)");
+               }
+       }
 }
 
 ClientProto Client::serialize() const
@@ -94,7 +107,8 @@ ClientStats Client::get_stats() const
 Stream::Stream(const string &stream_id)
        : stream_id(stream_id),
          data(new char[BACKLOG_SIZE]),
-         data_size(0)
+         data_size(0),
+         mark_pool(NULL)
 {
        memset(data, 0, BACKLOG_SIZE);
 }
@@ -108,7 +122,8 @@ Stream::Stream(const StreamProto &serialized)
        : stream_id(serialized.stream_id()),
          header(serialized.header()),
          data(new char[BACKLOG_SIZE]),
-         data_size(serialized.data_size())
+         data_size(serialized.data_size()),
+         mark_pool(NULL)
 {
        assert(serialized.data().size() == BACKLOG_SIZE);
        memcpy(data, serialized.data().data(), BACKLOG_SIZE);
@@ -163,31 +178,6 @@ Server::~Server()
        }
 }
 
-void Server::run()
-{
-       should_stop = false;
-       
-       // Joinable is already the default, but it's good to be certain.
-       pthread_attr_t attr;
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
-       pthread_create(&worker_thread, &attr, Server::do_work_thunk, this);
-}
-       
-void Server::stop()
-{
-       {
-               MutexLock lock(&mutex);
-               should_stop = true;
-       }
-
-       pthread_kill(worker_thread, SIGHUP);
-       if (pthread_join(worker_thread, NULL) == -1) {
-               perror("pthread_join");
-               exit(1);
-       }
-}
-       
 vector<ClientStats> Server::get_client_stats() const
 {
        vector<ClientStats> ret;
@@ -201,13 +191,6 @@ vector<ClientStats> Server::get_client_stats() const
        return ret;
 }
 
-void *Server::do_work_thunk(void *arg)
-{
-       Server *server = static_cast<Server *>(arg);
-       server->do_work();
-       return NULL;
-}
-
 void Server::do_work()
 {
        for ( ;; ) {
@@ -358,6 +341,13 @@ void Server::set_header(const string &stream_id, const string &header)
                }
        }
 }
+       
+void Server::set_mark_pool(const std::string &stream_id, MarkPool *mark_pool)
+{
+       MutexLock lock(&mutex);
+       assert(clients.empty());
+       find_stream(stream_id)->mark_pool = mark_pool;
+}
 
 void Server::add_data_deferred(const string &stream_id, const char *data, size_t bytes)
 {
@@ -582,6 +572,16 @@ int Server::parse_request(Client *client)
 
        client->stream_id = request_tokens[1];
        client->stream = find_stream(client->stream_id);
+       if (client->stream->mark_pool != NULL) {
+               client->fwmark = client->stream->mark_pool->get_mark();
+       } else {
+               client->fwmark = 0;  // No mark.
+       }
+       if (setsockopt(client->sock, SOL_SOCKET, SO_MARK, &client->fwmark, sizeof(client->fwmark)) == -1) {                          
+               if (client->fwmark != 0) {
+                       perror("setsockopt(SO_MARK)");
+               }
+       }
        client->request.clear();
 
        return 200;  // OK!
@@ -644,8 +644,12 @@ void Server::close_client(Client *client)
        if (client->stream != NULL) {
                delete_from(&client->stream->sleeping_clients, client);
                delete_from(&client->stream->to_process, client);
+               if (client->stream->mark_pool != NULL) {
+                       int fwmark = client->fwmark;
+                       client->stream->mark_pool->release_mark(fwmark);
+               }
        }
-       
+
        // Bye-bye!
        int ret;
        do {