X-Git-Url: https://git.sesse.net/?p=cubemap;a=blobdiff_plain;f=server.cpp;h=3e639cf7455a470d45072992cb7efd20eeaf30f9;hp=7e4352276ea1b2d1db6a84f88bae5e76239b0824;hb=1db0474e2a914bfc31014c067d9af24f87037784;hpb=17d773d2d45d495704e974b9246eccb21faa8635 diff --git a/server.cpp b/server.cpp index 7e43522..3e639cf 100644 --- a/server.cpp +++ b/server.cpp @@ -102,7 +102,14 @@ Server::Server() Server::~Server() { - close(epoll_fd); + int ret; + do { + ret = close(epoll_fd); + } while (ret == -1 && errno == EINTR); + + if (ret == -1) { + perror("close(epoll_fd)"); + } } void Server::run() @@ -140,6 +147,9 @@ void Server::do_work() { for ( ;; ) { int nfds = epoll_wait(epoll_fd, events, EPOLL_MAX_EVENTS, EPOLL_TIMEOUT_MS); + if (nfds == -1 && errno == EINTR) { + continue; + } if (nfds == -1) { perror("epoll_wait"); exit(1); @@ -279,7 +289,10 @@ void Server::process_client(Client *client) case Client::READING_REQUEST: { // Try to read more of the request. char buf[1024]; - int ret = read(client->sock, buf, sizeof(buf)); + int ret; + do { + ret = read(client->sock, buf, sizeof(buf)); + } while (ret == -1 && errno == EINTR); if (ret == -1) { perror("read"); close_client(client); @@ -330,9 +343,12 @@ void Server::process_client(Client *client) } case Client::SENDING_ERROR: case Client::SENDING_HEADER: { - int ret = write(client->sock, - client->header_or_error.data() + client->header_or_error_bytes_sent, - client->header_or_error.size() - client->header_or_error_bytes_sent); + int ret; + do { + ret = write(client->sock, + client->header_or_error.data() + client->header_or_error_bytes_sent, + client->header_or_error.size() - client->header_or_error_bytes_sent); + } while (ret == -1 && errno == EINTR); if (ret == -1) { perror("write"); close_client(client); @@ -385,11 +401,15 @@ void Server::process_client(Client *client) iov[1].iov_base = const_cast(stream.data); iov[1].iov_len = bytes_to_send - bytes_first_part; - ret = writev(client->sock, iov, 2); + do { + ret = writev(client->sock, iov, 2); + } while (ret == -1 && errno == EINTR); } else { - ret = write(client->sock, - stream.data + (client->bytes_sent % BACKLOG_SIZE), - bytes_to_send); + do { + ret = write(client->sock, + stream.data + (client->bytes_sent % BACKLOG_SIZE), + bytes_to_send); + } while (ret == -1 && errno == EINTR); } if (ret == -1) { perror("write/writev"); @@ -486,7 +506,15 @@ void Server::close_client(Client *client) sleeping_clients.erase(new_end, sleeping_clients.end()); // Bye-bye! - close(client->sock); + int ret; + do { + ret = close(client->sock); + } while (ret == -1 && errno == EINTR); + + if (ret == -1) { + perror("close"); + } + clients.erase(client->sock); }