X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=server.cpp;h=02efdbbdb6c165612f2ac45219072bbfbd414982;hb=d1ad2cf60ca694f742e2061a596bc27c7e58f5c8;hp=a11b55f72e6f62d7e1401925e4c113a58df9ceec;hpb=ca5f208423d42bc6d9a8601d5f9a6ffae0119f40;p=cubemap diff --git a/server.cpp b/server.cpp index a11b55f..02efdbb 100644 --- a/server.cpp +++ b/server.cpp @@ -14,6 +14,7 @@ #include #include +#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(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(&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(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 && ret == bytes_to_send) { + } 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(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(client); if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) { perror("epoll_ctl(EPOLL_CTL_MOD)");