static int calculate_poll_result(connection_t *conn, int poll_result) { if (poll_result == 0) { return TIMEOUT_EXN; } else if (poll_result < 0 && errno != EINTR) { return read_exception_status(conn, errno); } }
static int std_read(connection_t *conn, char *buf, int len, int timeout) { int fd; int result; int retry = 3; int poll_result; int poll_timeout; if (! conn) { return -1; } fd = conn->fd; if (fd <= 0 || conn->is_read_shutdown) { return -1; } if (timeout >= 0) { poll_timeout = timeout; } else { poll_timeout = conn->socket_timeout; } if (conn->is_recv_timeout) { if (conn->recv_timeout != poll_timeout) { conn->recv_timeout = poll_timeout; resin_tcp_set_recv_timeout(conn, poll_timeout); } } else { poll_result = poll_read(fd, poll_timeout); if (poll_result <= 0) { return calculate_poll_result(conn, poll_result); } } do { /* recv returns 0 on end of file */ result = recv(fd, buf, len, 0); // fprintf(stderr, "rcv %d\n", result); if (result > 0) { return result; } else if (result == 0) { /* recv returns 0 on end of file */ return -1; } if (errno == EINTR) { /* EAGAIN is returned by a timeout */ poll_result = poll_read(fd, conn->socket_timeout); if (poll_result <= 0) { return calculate_poll_result(conn, poll_result); } } else { return read_exception_status(conn, errno); } } while (retry-- >= 0); if (result > 0) { return result; } else if (result == 0) { return -1; } else { return read_exception_status(conn, errno); } }