]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Implement much better logging, with support for both files and syslog.
[cubemap] / server.cpp
index 730bb4463c7433c7734a49d19a9e5ea9d08331fd..02efdbbdb6c165612f2ac45219072bbfbd414982 100644 (file)
@@ -14,6 +14,7 @@
 #include <utility>
 #include <vector>
 
+#include "log.h"
 #include "markpool.h"
 #include "mutexlock.h"
 #include "parse.h"
@@ -80,9 +81,7 @@ void Server::do_work()
                process_queued_data();
 
                for (int i = 0; i < nfds; ++i) {
-                       int fd = events[i].data.fd;
-                       assert(clients.count(fd) != 0);
-                       Client *client = &clients[fd];
+                       Client *client = reinterpret_cast<Client *>(events[i].data.u64);
 
                        if (events[i].events & (EPOLLERR | EPOLLRDHUP | EPOLLHUP)) {
                                close_client(client);
@@ -140,8 +139,7 @@ void Server::add_client(int sock)
        // Start listening on data from this socket.
        epoll_event ev;
        ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
-       ev.data.u64 = 0;  // Keep Valgrind happy.
-       ev.data.fd = sock;
+       ev.data.u64 = reinterpret_cast<uint64_t>(&clients[sock]);
        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
                perror("epoll_ctl(EPOLL_CTL_ADD)");
                exit(1);
@@ -173,7 +171,7 @@ void Server::add_client_from_serialized(const ClientProto &client)
                ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
        }
        ev.data.u64 = 0;  // Keep Valgrind happy.
-       ev.data.fd = client.sock();
+       ev.data.u64 = reinterpret_cast<uint64_t>(client_ptr);
        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client.sock(), &ev) == -1) {
                perror("epoll_ctl(EPOLL_CTL_ADD)");
                exit(1);
@@ -270,7 +268,7 @@ read_request_again:
        
                switch (status) {
                case RP_OUT_OF_SPACE:
-                       fprintf(stderr, "WARNING: fd %d sent overlong request!\n", client->sock);
+                       log(WARNING, "fd %d sent overlong request!", client->sock);
                        close_client(client);
                        return;
                case RP_NOT_FINISHED_YET:
@@ -278,7 +276,7 @@ read_request_again:
                        // See if there's more data for us.
                        goto read_request_again;
                case RP_EXTRA_DATA:
-                       fprintf(stderr, "WARNING: fd %d had junk data after request!\n", client->sock);
+                       log(WARNING, "fd %d had junk data after request!", client->sock);
                        close_client(client);
                        return;
                case RP_FINISHED:
@@ -359,7 +357,7 @@ sending_data_again:
                        return;
                }
                if (bytes_to_send > stream->backlog_size) {
-                       fprintf(stderr, "WARNING: fd %d lost %lld bytes, maybe too slow connection\n",
+                       log(WARNING, "fd %d lost %lld bytes, maybe too slow connection",
                                client->sock,
                                (long long int)(bytes_to_send - stream->backlog_size));
                        client->stream_pos = stream->bytes_received - stream->backlog_size;
@@ -400,7 +398,7 @@ sending_data_again:
                        // We don't have any more data for this client, so put it to sleep.
                        // This is postcondition #3.
                        stream->put_client_to_sleep(client);
-               } else if (more_data) {
+               } else if (more_data && size_t(ret) == bytes_to_send) {
                        goto sending_data_again;
                }
                break;
@@ -454,8 +452,7 @@ void Server::construct_header(Client *client)
 
        epoll_event ev;
        ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
-       ev.data.u64 = 0;  // Keep Valgrind happy.
-       ev.data.fd = client->sock;
+       ev.data.u64 = reinterpret_cast<uint64_t>(client);
 
        if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
                perror("epoll_ctl(EPOLL_CTL_MOD)");
@@ -475,8 +472,7 @@ void Server::construct_error(Client *client, int error_code)
 
        epoll_event ev;
        ev.events = EPOLLOUT | EPOLLET | EPOLLRDHUP;
-       ev.data.u64 = 0;  // Keep Valgrind happy.
-       ev.data.fd = client->sock;
+       ev.data.u64 = reinterpret_cast<uint64_t>(client);
 
        if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
                perror("epoll_ctl(EPOLL_CTL_MOD)");