1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10#include <ngx_event.h>
11
12
13ssize_t
14ngx_wsarecv(ngx_connection_t *c, u_char *buf, size_t size)
15{
16    int           rc;
17    u_long        bytes, flags;
18    WSABUF        wsabuf[1];
19    ngx_err_t     err;
20    ngx_int_t     n;
21    ngx_event_t  *rev;
22
23    wsabuf[0].buf = (char *) buf;
24    wsabuf[0].len = size;
25    flags = 0;
26    bytes = 0;
27
28    rc = WSARecv(c->fd, wsabuf, 1, &bytes, &flags, NULL, NULL);
29
30    ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
31                   "WSARecv: fd:%d rc:%d %ul of %z", c->fd, rc, bytes, size);
32
33    rev = c->read;
34
35    if (rc == -1) {
36        rev->ready = 0;
37        err = ngx_socket_errno;
38
39        if (err == WSAEWOULDBLOCK) {
40            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
41                           "WSARecv() not ready");
42            return NGX_AGAIN;
43        }
44
45        n = ngx_connection_error(c, err, "WSARecv() failed");
46
47        if (n == NGX_ERROR) {
48            rev->error = 1;
49        }
50
51        return n;
52    }
53
54    if (bytes < size) {
55        rev->ready = 0;
56    }
57
58    if (bytes == 0) {
59        rev->eof = 1;
60    }
61
62    return bytes;
63}
64
65
66ssize_t
67ngx_overlapped_wsarecv(ngx_connection_t *c, u_char *buf, size_t size)
68{
69    int               rc;
70    u_long            bytes, flags;
71    WSABUF            wsabuf[1];
72    ngx_err_t         err;
73    ngx_int_t         n;
74    ngx_event_t      *rev;
75    LPWSAOVERLAPPED   ovlp;
76
77    rev = c->read;
78
79    if (!rev->ready) {
80        ngx_log_error(NGX_LOG_ALERT, c->log, 0, "second wsa post");
81        return NGX_AGAIN;
82    }
83
84    ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
85                   "rev->complete: %d", rev->complete);
86
87    if (rev->complete) {
88        rev->complete = 0;
89
90        if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
91            if (rev->ovlp.error) {
92                ngx_connection_error(c, rev->ovlp.error, "WSARecv() failed");
93                return NGX_ERROR;
94            }
95
96            ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
97                           "WSARecv ovlp: fd:%d %ul of %z",
98                           c->fd, rev->available, size);
99
100            return rev->available;
101        }
102
103        if (WSAGetOverlappedResult(c->fd, (LPWSAOVERLAPPED) &rev->ovlp,
104                                   &bytes, 0, NULL)
105            == 0)
106        {
107            ngx_connection_error(c, ngx_socket_errno,
108                               "WSARecv() or WSAGetOverlappedResult() failed");
109            return NGX_ERROR;
110        }
111
112        ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
113                       "WSARecv: fd:%d %ul of %z", c->fd, bytes, size);
114
115        return bytes;
116    }
117
118    ovlp = (LPWSAOVERLAPPED) &rev->ovlp;
119    ngx_memzero(ovlp, sizeof(WSAOVERLAPPED));
120    wsabuf[0].buf = (char *) buf;
121    wsabuf[0].len = size;
122    flags = 0;
123    bytes = 0;
124
125    rc = WSARecv(c->fd, wsabuf, 1, &bytes, &flags, ovlp, NULL);
126
127    rev->complete = 0;
128
129    ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
130                   "WSARecv ovlp: fd:%d rc:%d %ul of %z",
131                   c->fd, rc, bytes, size);
132
133    if (rc == -1) {
134        err = ngx_socket_errno;
135        if (err == WSA_IO_PENDING) {
136            rev->active = 1;
137            ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
138                           "WSARecv() posted");
139            return NGX_AGAIN;
140        }
141
142        n = ngx_connection_error(c, err, "WSARecv() failed");
143
144        if (n == NGX_ERROR) {
145            rev->error = 1;
146        }
147
148        return n;
149    }
150
151    if (ngx_event_flags & NGX_USE_IOCP_EVENT) {
152
153        /*
154         * if a socket was bound with I/O completion port
155         * then GetQueuedCompletionStatus() would anyway return its status
156         * despite that WSARecv() was already complete
157         */
158
159        rev->active = 1;
160        return NGX_AGAIN;
161    }
162
163    if (bytes == 0) {
164        rev->eof = 1;
165        rev->ready = 0;
166
167    } else {
168        rev->ready = 1;
169    }
170
171    rev->active = 0;
172
173    return bytes;
174}
175