X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=server.cpp;h=69615e705aba182b9fbbf168c56ea5c78931695c;hp=08f2afdfc907c38d8858de1715b46ce0ac2c179a;hb=1d995a9b2f2d56fb66192b8404c9f5591af9ff75;hpb=f52f37b4792a117821b2aab6a4213e30996a28b3 diff --git a/server.cpp b/server.cpp index 08f2afd..69615e7 100644 --- a/server.cpp +++ b/server.cpp @@ -18,9 +18,43 @@ #include "metacube.h" #include "server.h" #include "mutexlock.h" +#include "state.pb.h" using namespace std; +Client::Client(int sock) + : sock(sock), + state(Client::READING_REQUEST), + header_bytes_sent(0), + bytes_sent(0) +{ + request.reserve(1024); +} + +Client::Client(const ClientProto &serialized) + : sock(serialized.sock()), + state(State(serialized.state())), + request(serialized.request()), + stream_id(serialized.stream_id()), + header(serialized.header()), + header_bytes_sent(serialized.header_bytes_sent()), + bytes_sent(serialized.bytes_sent()) +{ +} + +ClientProto Client::serialize() const +{ + ClientProto serialized; + serialized.set_sock(sock); + serialized.set_state(state); + serialized.set_request(request); + serialized.set_stream_id(stream_id); + serialized.set_header(header); + serialized.set_header_bytes_sent(serialized.header_bytes_sent()); + serialized.set_bytes_sent(bytes_sent); + return serialized; +} + Server::Server() { pthread_mutex_init(&mutex, NULL); @@ -34,8 +68,26 @@ Server::Server() void Server::run() { - pthread_t thread; - pthread_create(&thread, NULL, Server::do_work_thunk, this); + 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; + } + + if (pthread_join(worker_thread, NULL) == -1) { + perror("pthread_join"); + exit(1); + } } void *Server::do_work_thunk(void *arg) @@ -49,13 +101,17 @@ void Server::do_work() { for ( ;; ) { int nfds = epoll_wait(epoll_fd, events, EPOLL_MAX_EVENTS, EPOLL_TIMEOUT_MS); - - MutexLock lock(&mutex); // We release the mutex between iterations. if (nfds == -1) { perror("epoll_wait"); exit(1); } - + + MutexLock lock(&mutex); // We release the mutex between iterations. + + if (should_stop) { + return; + } + for (int i = 0; i < nfds; ++i) { int fd = events[i].data.fd; assert(clients.count(fd) != 0); @@ -74,14 +130,7 @@ void Server::do_work() void Server::add_client(int sock) { MutexLock lock(&mutex); - Client new_client; - new_client.sock = sock; - new_client.request.reserve(1024); - new_client.state = Client::READING_REQUEST; - new_client.header_bytes_sent = 0; - new_client.bytes_sent = 0; - - clients.insert(make_pair(sock, new_client)); + clients.insert(make_pair(sock, Client(sock))); // Start listening on data from this socket. epoll_event ev;