Another state transition optimization due to NBIO:

Go from recv_request to send_header without visiting select()
This commit is contained in:
Emil Mikulic 2006-12-13 12:47:20 +00:00
parent ba91f1cdb3
commit 844dcc6b9d

View File

@ -298,6 +298,7 @@ static const char default_mimetype[] = "application/octet-stream";
/* Prototypes. */ /* Prototypes. */
static void poll_recv_request(struct connection *conn); static void poll_recv_request(struct connection *conn);
static void poll_send_header(struct connection *conn);
@ -1175,7 +1176,9 @@ static void accept_connection(void)
inet_ntoa(addrin.sin_addr), inet_ntoa(addrin.sin_addr),
ntohs(addrin.sin_port) ); ntohs(addrin.sin_port) );
/* try to read straight away rather than going through select() */ /* try to read straight away rather than going through another iteration
* of the select() loop.
*/
poll_recv_request(conn); poll_recv_request(conn);
} }
@ -1996,6 +1999,7 @@ static void poll_recv_request(struct connection *conn)
char buf[BUFSIZE]; char buf[BUFSIZE];
ssize_t recvd; ssize_t recvd;
assert(conn->state == RECV_REQUEST);
recvd = recv(conn->socket, buf, BUFSIZE, 0); recvd = recv(conn->socket, buf, BUFSIZE, 0);
if (debug) printf("poll_recv_request(%d) got %d bytes\n", if (debug) printf("poll_recv_request(%d) got %d bytes\n",
conn->socket, (int)recvd); conn->socket, (int)recvd);
@ -2038,6 +2042,12 @@ static void poll_recv_request(struct connection *conn)
"Your request was dropped because it was too long."); "Your request was dropped because it was too long.");
conn->state = SEND_HEADER; conn->state = SEND_HEADER;
} }
/* if we've moved on to the next state, try to send right away, instead of
* going through another iteration of the select() loop.
*/
if (conn->state == SEND_HEADER)
poll_send_header(conn);
} }
@ -2049,6 +2059,7 @@ static void poll_send_header(struct connection *conn)
{ {
ssize_t sent; ssize_t sent;
assert(conn->state == SEND_HEADER);
assert(conn->header_length == strlen(conn->header)); assert(conn->header_length == strlen(conn->header));
sent = send(conn->socket, conn->header + conn->header_sent, sent = send(conn->socket, conn->header + conn->header_sent,
@ -2060,6 +2071,10 @@ static void poll_send_header(struct connection *conn)
/* handle any errors (-1) or closure (0) in send() */ /* handle any errors (-1) or closure (0) in send() */
if (sent < 1) if (sent < 1)
{ {
if ((sent == -1) && (errno == EAGAIN)) {
if (debug) printf("poll_send_header would have blocked\n");
return;
}
if (debug && (sent == -1)) if (debug && (sent == -1))
printf("send(%d) error: %s\n", conn->socket, strerror(errno)); printf("send(%d) error: %s\n", conn->socket, strerror(errno));
conn->conn_close = 1; conn->conn_close = 1;