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()
{
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);
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);
}
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);
iov[1].iov_base = const_cast<char *>(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");
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);
}