Do some cleanup around EPOLL_CTL_MOD operations.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 4 Apr 2018 22:43:55 +0000 (00:43 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 4 Apr 2018 22:44:41 +0000 (00:44 +0200)
server.cpp
server.h

index 7f07483..6bac2c6 100644 (file)
@@ -112,7 +112,7 @@ void Server::do_work()
 
                // Process each client where we have socket activity.
                for (int i = 0; i < nfds; ++i) {
-                       Client *client = reinterpret_cast<Client *>(events[i].data.u64);
+                       Client *client = reinterpret_cast<Client *>(events[i].data.ptr);
 
                        if (events[i].events & (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) {
                                close_client(client);
@@ -243,7 +243,7 @@ void Server::add_client(int sock, Acceptor *acceptor)
                // EPOLLOUT will be added once we go out of READING_REQUEST.
                ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
        }
-       ev.data.u64 = reinterpret_cast<uint64_t>(client_ptr);
+       ev.data.ptr = client_ptr;
        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
                log_perror("epoll_ctl(EPOLL_CTL_ADD)");
                exit(1);
@@ -298,7 +298,7 @@ void Server::add_client_from_serialized(const ClientProto &client)
                // the sleeping array again soon.
                ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
        }
-       ev.data.u64 = reinterpret_cast<uint64_t>(client_ptr);
+       ev.data.ptr = client_ptr;
        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client.sock(), &ev) == -1) {
                log_perror("epoll_ctl(EPOLL_CTL_ADD)");
                exit(1);
@@ -903,15 +903,7 @@ void Server::construct_header(Client *client)
 
        // Switch states.
        client->state = Client::SENDING_HEADER;
-
-       epoll_event ev;
-       ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
-       ev.data.u64 = reinterpret_cast<uint64_t>(client);
-
-       if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
-               log_perror("epoll_ctl(EPOLL_CTL_MOD)");
-               exit(1);
-       }
+       change_epoll_events(client, EPOLLOUT | EPOLLET | EPOLLRDHUP);
 }
        
 void Server::construct_error(Client *client, int error_code)
@@ -923,15 +915,7 @@ void Server::construct_error(Client *client, int error_code)
 
        // Switch states.
        client->state = Client::SENDING_SHORT_RESPONSE;
-
-       epoll_event ev;
-       ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
-       ev.data.u64 = reinterpret_cast<uint64_t>(client);
-
-       if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
-               log_perror("epoll_ctl(EPOLL_CTL_MOD)");
-               exit(1);
-       }
+       change_epoll_events(client, EPOLLOUT | EPOLLET | EPOLLRDHUP);
 }
 
 void Server::construct_204(Client *client)
@@ -955,15 +939,7 @@ void Server::construct_204(Client *client)
 
        // Switch states.
        client->state = Client::SENDING_SHORT_RESPONSE;
-
-       epoll_event ev;
-       ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
-       ev.data.u64 = reinterpret_cast<uint64_t>(client);
-
-       if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
-               log_perror("epoll_ctl(EPOLL_CTL_MOD)");
-               exit(1);
-       }
+       change_epoll_events(client, EPOLLOUT | EPOLLET | EPOLLRDHUP);
 }
 
 template<class T>
@@ -998,7 +974,19 @@ void Server::close_client(Client *client)
 
        clients.erase(client->sock);
 }
-       
+
+void Server::change_epoll_events(Client *client, uint32_t events)
+{
+       epoll_event ev;
+       ev.events = events;
+       ev.data.ptr = client;
+
+       if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
+               log_perror("epoll_ctl(EPOLL_CTL_MOD)");
+               exit(1);
+       }
+}
+
 void Server::process_queued_data()
 {
        {
index cb726c6..1e6f44a 100644 (file)
--- a/server.h
+++ b/server.h
@@ -158,6 +158,9 @@ private:
        // Close a given client socket, and clean up after it.
        void close_client(Client *client);
 
+       // Listen for a different set of epoll events.
+       void change_epoll_events(Client *client, uint32_t events);
+
        // Parse the HTTP request. Returns a HTTP status code (200/204/400/404).
        int parse_request(Client *client);