2003-11-17 05:49:42 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-11-17 05:49:42 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
|
|
|
|
|
|
|
|
ssize_t ngx_wsarecv_chain(ngx_connection_t *c, ngx_chain_t *chain)
|
|
|
|
{
|
|
|
|
int rc;
|
2004-03-17 05:26:01 +08:00
|
|
|
u_char *prev;
|
2004-03-05 16:34:24 +08:00
|
|
|
u_long bytes, flags;
|
|
|
|
size_t size;
|
2003-11-17 05:49:42 +08:00
|
|
|
WSABUF *wsabuf;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_array_t io;
|
|
|
|
ngx_event_t *rev;
|
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
wsabuf = NULL;
|
|
|
|
flags = 0;
|
|
|
|
size = 0;
|
|
|
|
bytes = 0;
|
|
|
|
|
|
|
|
ngx_init_array(io, c->pool, 10, sizeof(WSABUF), NGX_ERROR);
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
/* coalesce the neighbouring bufs */
|
2003-11-17 05:49:42 +08:00
|
|
|
|
|
|
|
while (chain) {
|
2004-05-28 23:49:23 +08:00
|
|
|
if (prev == chain->buf->last) {
|
|
|
|
wsabuf->len += chain->buf->end - chain->buf->last;
|
2003-11-17 05:49:42 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_test_null(wsabuf, ngx_push_array(&io), NGX_ERROR);
|
2004-05-28 23:49:23 +08:00
|
|
|
wsabuf->buf = (char *) chain->buf->last;
|
|
|
|
wsabuf->len = chain->buf->end - chain->buf->last;
|
2003-11-17 05:49:42 +08:00
|
|
|
}
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
size += chain->buf->end - chain->buf->last;
|
|
|
|
prev = chain->buf->end;
|
2003-11-17 05:49:42 +08:00
|
|
|
chain = chain->next;
|
|
|
|
}
|
|
|
|
|
2004-03-04 15:04:55 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"WSARecv: %d:%d", io.nelts, wsabuf->len);
|
|
|
|
|
2003-11-17 05:49:42 +08:00
|
|
|
|
|
|
|
rc = WSARecv(c->fd, io.elts, io.nelts, &bytes, &flags, NULL, NULL);
|
|
|
|
|
|
|
|
rev = c->read;
|
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
rev->ready = 0;
|
|
|
|
err = ngx_socket_errno;
|
|
|
|
|
|
|
|
if (err == WSAEWOULDBLOCK) {
|
2004-03-04 15:04:55 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
|
|
"WSARecv() not ready");
|
2003-11-17 05:49:42 +08:00
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
rev->error = 1;
|
2004-03-04 15:04:55 +08:00
|
|
|
ngx_connection_error(c, err, "WSARecv() failed");
|
2003-11-17 05:49:42 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes < size) {
|
|
|
|
rev->ready = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes == 0) {
|
|
|
|
rev->eof = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|