| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef _WIN32_WINNT |
| 23 |
#define _WIN32_WINNT 0x0501 |
| 24 |
#endif |
| 25 |
#if _WIN32_WINNT < 0x0501 |
| 26 |
#error "Proton requires Windows API support for XP or later." |
| 27 |
#endif |
| 28 |
#include <winsock2.h> |
| 29 |
#include <mswsock.h> |
| 30 |
#include <Ws2tcpip.h> |
| 31 |
|
| 32 |
#include "reactor/io.h" |
| 33 |
#include "reactor/selector.h" |
| 34 |
|
| 35 |
#include "iocp.h" |
| 36 |
#include "platform/platform.h" |
| 37 |
#include "core/util.h" |
| 38 |
|
| 39 |
#include <proton/object.h> |
| 40 |
#include <proton/error.h> |
| 41 |
#include <proton/transport.h> |
| 42 |
|
| 43 |
#include <assert.h> |
| 44 |
|
| 45 |
/* |
| 46 |
* Windows IO Completion Port support for Proton. |
| 47 |
* |
| 48 |
* Overlapped writes are used to avoid lengthy stalls between write |
| 49 |
* completion and starting a new write. Non-overlapped reads are used |
| 50 |
* since Windows accumulates inbound traffic without stalling and |
| 51 |
* managing read buffers would not avoid a memory copy at the pn_read |
| 52 |
* boundary. |
| 53 |
* |
| 54 |
* A socket must not get a Windows closesocket() unless the |
| 55 |
* application has called pn_close on the socket or a global |
| 56 |
* pn_io_finalize(). On error, the internal accounting for |
| 57 |
* write_closed or read_closed may be updated along with the external |
| 58 |
* event notification. A socket may be closed if it is never added to |
| 59 |
* the iocpdesc_map or is on its way out of the map. |
| 60 |
*/ |
| 61 |
|
| 62 |
// Max number of overlapped accepts per listener |
| 63 |
#define IOCP_MAX_ACCEPTS 10 |
| 64 |
|
| 65 |
// AcceptEx squishes the local and remote addresses and optional data |
| 66 |
// all together when accepting the connection. Reserve enough for |
| 67 |
// IPv6 addresses, even if the socket is IPv4. The 16 bytes padding |
| 68 |
// per address is required by AcceptEx. |
| 69 |
#define IOCP_SOCKADDRMAXLEN (sizeof(struct sockaddr_in6) + 16) |
| 70 |
#define IOCP_SOCKADDRBUFLEN (2 * IOCP_SOCKADDRMAXLEN) |
| 71 |
|
| 72 |
static void iocp_log(const char *fmt, ...) |
| 73 |
{ |
| 74 |
va_list ap; |
| 75 |
va_start(ap, fmt); |
| 76 |
vfprintf(stderr, fmt, ap); |
| 77 |
va_end(ap); |
| 78 |
fflush(stderr); |
| 79 |
} |
| 80 |
|
| 81 |
static void set_iocp_error_status(pn_error_t *error, int code, HRESULT status) |
| 82 |
{ |
| 83 |
char buf[512]; |
| 84 |
if (FormatMessage(FORMAT_MESSAGE_MAX_WIDTH_MASK | FORMAT_MESSAGE_FROM_SYSTEM, |
| 85 |
0, status, 0, buf, sizeof(buf), 0)) |
| 86 |
pn_error_set(error, code, buf); |
| 87 |
else { |
| 88 |
fprintf(stderr, "pn internal Windows error: %lu\n", GetLastError()); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
static void reap_check(iocpdesc_t *); |
| 93 |
static void bind_to_completion_port(iocpdesc_t *iocpd); |
| 94 |
static void iocp_shutdown(iocpdesc_t *iocpd); |
| 95 |
static void start_reading(iocpdesc_t *iocpd); |
| 96 |
static bool is_listener(iocpdesc_t *iocpd); |
| 97 |
static void release_sys_sendbuf(SOCKET s); |
| 98 |
|
| 99 |
static void iocpdesc_fail(iocpdesc_t *iocpd, HRESULT status, const char* text) |
| 100 |
{ |
| 101 |
pni_win32_error(iocpd->error, text, status); |
| 102 |
if (iocpd->iocp->iocp_trace) { |
| 103 |
iocp_log("connection terminated: %s\n", pn_error_text(iocpd->error)); |
| 104 |
} |
| 105 |
iocpd->write_closed = true; |
| 106 |
iocpd->read_closed = true; |
| 107 |
iocpd->poll_error = true; |
| 108 |
pni_events_update(iocpd, iocpd->events & ~(PN_READABLE | PN_WRITABLE)); |
| 109 |
} |
| 110 |
|
| 111 |
// Helper functions to use specialized IOCP AcceptEx() and ConnectEx() |
| 112 |
static LPFN_ACCEPTEX lookup_accept_ex(SOCKET s) |
| 113 |
{ |
| 114 |
GUID guid = WSAID_ACCEPTEX; |
| 115 |
DWORD bytes = 0; |
| 116 |
LPFN_ACCEPTEX fn; |
| 117 |
WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), |
| 118 |
&fn, sizeof(fn), &bytes, NULL, NULL); |
| 119 |
assert(fn); |
| 120 |
return fn; |
| 121 |
} |
| 122 |
|
| 123 |
static LPFN_CONNECTEX lookup_connect_ex(SOCKET s) |
| 124 |
{ |
| 125 |
GUID guid = WSAID_CONNECTEX; |
| 126 |
DWORD bytes = 0; |
| 127 |
LPFN_CONNECTEX fn; |
| 128 |
WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), |
| 129 |
&fn, sizeof(fn), &bytes, NULL, NULL); |
| 130 |
assert(fn); |
| 131 |
return fn; |
| 132 |
} |
| 133 |
|
| 134 |
static LPFN_GETACCEPTEXSOCKADDRS lookup_get_accept_ex_sockaddrs(SOCKET s) |
| 135 |
{ |
| 136 |
GUID guid = WSAID_GETACCEPTEXSOCKADDRS; |
| 137 |
DWORD bytes = 0; |
| 138 |
LPFN_GETACCEPTEXSOCKADDRS fn; |
| 139 |
WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, &guid, sizeof(guid), |
| 140 |
&fn, sizeof(fn), &bytes, NULL, NULL); |
| 141 |
assert(fn); |
| 142 |
return fn; |
| 143 |
} |
| 144 |
|
| 145 |
// match accept socket to listener socket |
| 146 |
static iocpdesc_t *create_same_type_socket(iocpdesc_t *iocpd) |
| 147 |
{ |
| 148 |
struct sockaddr_storage sa; |
| 149 |
socklen_t salen = sizeof(sa); |
| 150 |
if (getsockname(iocpd->socket, (struct sockaddr*)&sa, &salen) == -1) |
| 151 |
return NULL; |
| 152 |
SOCKET s = socket(sa.ss_family, SOCK_STREAM, 0); // Currently only work with SOCK_STREAM |
| 153 |
if (s == INVALID_SOCKET) |
| 154 |
return NULL; |
| 155 |
return pni_iocpdesc_create(iocpd->iocp, s, false); |
| 156 |
} |
| 157 |
|
| 158 |
static bool is_listener(iocpdesc_t *iocpd) |
| 159 |
{ |
| 160 |
return iocpd && iocpd->acceptor; |
| 161 |
} |
| 162 |
|
| 163 |
// === Async accept processing |
| 164 |
|
| 165 |
typedef struct { |
| 166 |
iocp_result_t base; |
| 167 |
iocpdesc_t *new_sock; |
| 168 |
char address_buffer[IOCP_SOCKADDRBUFLEN]; |
| 169 |
DWORD unused; |
| 170 |
} accept_result_t; |
| 171 |
|
| 172 |
static accept_result_t *accept_result(iocpdesc_t *listen_sock) { |
| 173 |
accept_result_t *result = (accept_result_t *)calloc(1, sizeof(accept_result_t)); |
| 174 |
if (result) { |
| 175 |
result->base.type = IOCP_ACCEPT; |
| 176 |
result->base.iocpd = listen_sock; |
| 177 |
} |
| 178 |
return result; |
| 179 |
} |
| 180 |
|
| 181 |
static void reset_accept_result(accept_result_t *result) { |
| 182 |
memset(&result->base.overlapped, 0, sizeof (OVERLAPPED)); |
| 183 |
memset(&result->address_buffer, 0, IOCP_SOCKADDRBUFLEN); |
| 184 |
} |
| 185 |
|
| 186 |
struct pni_acceptor_t { |
| 187 |
int accept_queue_size; |
| 188 |
pn_list_t *accepts; |
| 189 |
iocpdesc_t *listen_sock; |
| 190 |
bool signalled; |
| 191 |
LPFN_ACCEPTEX fn_accept_ex; |
| 192 |
LPFN_GETACCEPTEXSOCKADDRS fn_get_accept_ex_sockaddrs; |
| 193 |
}; |
| 194 |
|
| 195 |
#define CID_pni_acceptor CID_pn_void |
| 196 |
#define pni_acceptor_compare NULL |
| 197 |
#define pni_acceptor_inspect NULL |
| 198 |
#define pni_acceptor_hashcode NULL |
| 199 |
|
| 200 |
static void pni_acceptor_initialize(void *object) |
| 201 |
{ |
| 202 |
pni_acceptor_t *acceptor = (pni_acceptor_t *) object; |
| 203 |
acceptor->accepts = pn_list(PN_VOID, IOCP_MAX_ACCEPTS); |
| 204 |
} |
| 205 |
|
| 206 |
static void pni_acceptor_finalize(void *object) |
| 207 |
{ |
| 208 |
pni_acceptor_t *acceptor = (pni_acceptor_t *) object; |
| 209 |
size_t len = pn_list_size(acceptor->accepts); |
| 210 |
for (size_t i = 0; i < len; i++) |
| 211 |
free(pn_list_get(acceptor->accepts, i)); |
| 212 |
pn_free(acceptor->accepts); |
| 213 |
} |
| 214 |
|
| 215 |
static pni_acceptor_t *pni_acceptor(iocpdesc_t *iocpd) |
| 216 |
{ |
| 217 |
static const pn_class_t clazz = PN_CLASS(pni_acceptor); |
| 218 |
pni_acceptor_t *acceptor = (pni_acceptor_t *) pn_class_new(&clazz, sizeof(pni_acceptor_t)); |
| 219 |
acceptor->listen_sock = iocpd; |
| 220 |
acceptor->accept_queue_size = 0; |
| 221 |
acceptor->signalled = false; |
| 222 |
pn_socket_t sock = acceptor->listen_sock->socket; |
| 223 |
acceptor->fn_accept_ex = lookup_accept_ex(sock); |
| 224 |
acceptor->fn_get_accept_ex_sockaddrs = lookup_get_accept_ex_sockaddrs(sock); |
| 225 |
return acceptor; |
| 226 |
} |
| 227 |
|
| 228 |
static void begin_accept(pni_acceptor_t *acceptor, accept_result_t *result) |
| 229 |
{ |
| 230 |
if (acceptor->listen_sock->closing) { |
| 231 |
if (result) { |
| 232 |
free(result); |
| 233 |
acceptor->accept_queue_size--; |
| 234 |
} |
| 235 |
if (acceptor->accept_queue_size == 0) |
| 236 |
acceptor->signalled = true; |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
if (result) { |
| 241 |
reset_accept_result(result); |
| 242 |
} else { |
| 243 |
if (acceptor->accept_queue_size < IOCP_MAX_ACCEPTS && |
| 244 |
pn_list_size(acceptor->accepts) == acceptor->accept_queue_size ) { |
| 245 |
result = accept_result(acceptor->listen_sock); |
| 246 |
acceptor->accept_queue_size++; |
| 247 |
} else { |
| 248 |
// an async accept is still pending or max concurrent accepts already hit |
| 249 |
return; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
result->new_sock = create_same_type_socket(acceptor->listen_sock); |
| 254 |
if (result->new_sock) { |
| 255 |
// Not yet connected. |
| 256 |
result->new_sock->read_closed = true; |
| 257 |
result->new_sock->write_closed = true; |
| 258 |
|
| 259 |
bool success = acceptor->fn_accept_ex(acceptor->listen_sock->socket, result->new_sock->socket, |
| 260 |
result->address_buffer, 0, IOCP_SOCKADDRMAXLEN, IOCP_SOCKADDRMAXLEN, |
| 261 |
&result->unused, (LPOVERLAPPED) result); |
| 262 |
if (!success && WSAGetLastError() != ERROR_IO_PENDING) { |
| 263 |
result->base.status = WSAGetLastError(); |
| 264 |
pn_list_add(acceptor->accepts, result); |
| 265 |
pni_events_update(acceptor->listen_sock, acceptor->listen_sock->events | PN_READABLE); |
| 266 |
} else { |
| 267 |
acceptor->listen_sock->ops_in_progress++; |
| 268 |
// This socket is equally involved in the async operation. |
| 269 |
result->new_sock->ops_in_progress++; |
| 270 |
} |
| 271 |
} else { |
| 272 |
iocpdesc_fail(acceptor->listen_sock, WSAGetLastError(), "create accept socket"); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static void complete_accept(accept_result_t *result, HRESULT status) |
| 277 |
{ |
| 278 |
result->new_sock->ops_in_progress--; |
| 279 |
iocpdesc_t *ld = result->base.iocpd; |
| 280 |
if (ld->read_closed) { |
| 281 |
if (!result->new_sock->closing) |
| 282 |
pni_iocp_begin_close(result->new_sock); |
| 283 |
free(result); // discard |
| 284 |
reap_check(ld); |
| 285 |
} else { |
| 286 |
result->base.status = status; |
| 287 |
pn_list_add(ld->acceptor->accepts, result); |
| 288 |
pni_events_update(ld, ld->events | PN_READABLE); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
pn_socket_t pni_iocp_end_accept(iocpdesc_t *ld, struct sockaddr *addr, socklen_t *addrlen, bool *would_block, pn_error_t *error) |
| 293 |
{ |
| 294 |
if (!is_listener(ld)) { |
| 295 |
set_iocp_error_status(error, PN_ERR, WSAEOPNOTSUPP); |
| 296 |
return INVALID_SOCKET; |
| 297 |
} |
| 298 |
if (ld->read_closed) { |
| 299 |
set_iocp_error_status(error, PN_ERR, WSAENOTSOCK); |
| 300 |
return INVALID_SOCKET; |
| 301 |
} |
| 302 |
if (pn_list_size(ld->acceptor->accepts) == 0) { |
| 303 |
if (ld->events & PN_READABLE && ld->iocp->iocp_trace) |
| 304 |
iocp_log("listen socket readable with no available accept completions\n"); |
| 305 |
*would_block = true; |
| 306 |
return INVALID_SOCKET; |
| 307 |
} |
| 308 |
|
| 309 |
accept_result_t *result = (accept_result_t *) pn_list_get(ld->acceptor->accepts, 0); |
| 310 |
pn_list_del(ld->acceptor->accepts, 0, 1); |
| 311 |
if (!pn_list_size(ld->acceptor->accepts)) |
| 312 |
pni_events_update(ld, ld->events & ~PN_READABLE); // No pending accepts |
| 313 |
|
| 314 |
pn_socket_t accept_sock; |
| 315 |
if (result->base.status) { |
| 316 |
accept_sock = INVALID_SOCKET; |
| 317 |
pni_win32_error(ld->error, "accept failure", result->base.status); |
| 318 |
if (ld->iocp->iocp_trace) |
| 319 |
iocp_log("%s\n", pn_error_text(ld->error)); |
| 320 |
// App never sees this socket so close it here. |
| 321 |
pni_iocp_begin_close(result->new_sock); |
| 322 |
} else { |
| 323 |
accept_sock = result->new_sock->socket; |
| 324 |
// AcceptEx special setsockopt: |
| 325 |
setsockopt(accept_sock, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (char*)&ld->socket, |
| 326 |
sizeof (SOCKET)); |
| 327 |
if (addr && addrlen && *addrlen > 0) { |
| 328 |
struct sockaddr_storage *local_addr = NULL; |
| 329 |
struct sockaddr_storage *remote_addr = NULL; |
| 330 |
int local_addrlen, remote_addrlen; |
| 331 |
LPFN_GETACCEPTEXSOCKADDRS fn = ld->acceptor->fn_get_accept_ex_sockaddrs; |
| 332 |
fn(result->address_buffer, 0, IOCP_SOCKADDRMAXLEN, IOCP_SOCKADDRMAXLEN, |
| 333 |
(SOCKADDR **) &local_addr, &local_addrlen, (SOCKADDR **) &remote_addr, |
| 334 |
&remote_addrlen); |
| 335 |
*addrlen = pn_min(*addrlen, remote_addrlen); |
| 336 |
memmove(addr, remote_addr, *addrlen); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
if (accept_sock != INVALID_SOCKET) { |
| 341 |
// Connected. |
| 342 |
result->new_sock->read_closed = false; |
| 343 |
result->new_sock->write_closed = false; |
| 344 |
} |
| 345 |
|
| 346 |
// Done with the completion result, so reuse it |
| 347 |
result->new_sock = NULL; |
| 348 |
begin_accept(ld->acceptor, result); |
| 349 |
return accept_sock; |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
// === Async connect processing |
| 354 |
|
| 355 |
typedef struct { |
| 356 |
iocp_result_t base; |
| 357 |
char address_buffer[IOCP_SOCKADDRBUFLEN]; |
| 358 |
struct addrinfo *addrinfo; |
| 359 |
} connect_result_t; |
| 360 |
|
| 361 |
#define CID_connect_result CID_pn_void |
| 362 |
#define connect_result_initialize NULL |
| 363 |
#define connect_result_compare NULL |
| 364 |
#define connect_result_inspect NULL |
| 365 |
#define connect_result_hashcode NULL |
| 366 |
|
| 367 |
static void connect_result_finalize(void *object) |
| 368 |
{ |
| 369 |
connect_result_t *result = (connect_result_t *) object; |
| 370 |
// Do not release addrinfo until ConnectEx completes |
| 371 |
if (result->addrinfo) |
| 372 |
freeaddrinfo(result->addrinfo); |
| 373 |
} |
| 374 |
|
| 375 |
static connect_result_t *connect_result(iocpdesc_t *iocpd, struct addrinfo *addr) { |
| 376 |
static const pn_class_t clazz = PN_CLASS(connect_result); |
| 377 |
connect_result_t *result = (connect_result_t *) pn_class_new(&clazz, sizeof(connect_result_t)); |
| 378 |
if (result) { |
| 379 |
memset(result, 0, sizeof(connect_result_t)); |
| 380 |
result->base.type = IOCP_CONNECT; |
| 381 |
result->base.iocpd = iocpd; |
| 382 |
result->addrinfo = addr; |
| 383 |
} |
| 384 |
return result; |
| 385 |
} |
| 386 |
|
| 387 |
pn_socket_t pni_iocp_begin_connect(iocp_t *iocp, pn_socket_t sock, struct addrinfo *addr, pn_error_t *error) |
| 388 |
{ |
| 389 |
// addr lives for the duration of the async connect. Caller has passed ownership here. |
| 390 |
// See connect_result_finalize(). |
| 391 |
// Use of Windows-specific ConnectEx() requires our socket to be "loosely" pre-bound: |
| 392 |
struct sockaddr_storage sa; |
| 393 |
memset(&sa, 0, sizeof(sa)); |
| 394 |
sa.ss_family = addr->ai_family; |
| 395 |
if (bind(sock, (SOCKADDR *) &sa, addr->ai_addrlen)) { |
| 396 |
pni_win32_error(error, "begin async connection", WSAGetLastError()); |
| 397 |
if (iocp->iocp_trace) |
| 398 |
iocp_log("%s\n", pn_error_text(error)); |
| 399 |
closesocket(sock); |
| 400 |
freeaddrinfo(addr); |
| 401 |
return INVALID_SOCKET; |
| 402 |
} |
| 403 |
|
| 404 |
iocpdesc_t *iocpd = pni_iocpdesc_create(iocp, sock, false); |
| 405 |
bind_to_completion_port(iocpd); |
| 406 |
LPFN_CONNECTEX fn_connect_ex = lookup_connect_ex(iocpd->socket); |
| 407 |
connect_result_t *result = connect_result(iocpd, addr); |
| 408 |
DWORD unused; |
| 409 |
bool success = fn_connect_ex(iocpd->socket, result->addrinfo->ai_addr, result->addrinfo->ai_addrlen, |
| 410 |
NULL, 0, &unused, (LPOVERLAPPED) result); |
| 411 |
if (!success && WSAGetLastError() != ERROR_IO_PENDING) { |
| 412 |
pni_win32_error(error, "ConnectEx failure", WSAGetLastError()); |
| 413 |
pn_free(result); |
| 414 |
iocpd->write_closed = true; |
| 415 |
iocpd->read_closed = true; |
| 416 |
if (iocp->iocp_trace) |
| 417 |
iocp_log("%s\n", pn_error_text(error)); |
| 418 |
} else { |
| 419 |
iocpd->ops_in_progress++; |
| 420 |
} |
| 421 |
return sock; |
| 422 |
} |
| 423 |
|
| 424 |
static void complete_connect(connect_result_t *result, HRESULT status) |
| 425 |
{ |
| 426 |
iocpdesc_t *iocpd = result->base.iocpd; |
| 427 |
if (iocpd->closing) { |
| 428 |
pn_free(result); |
| 429 |
reap_check(iocpd); |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
if (status) { |
| 434 |
iocpdesc_fail(iocpd, status, "Connect failure"); |
| 435 |
// Posix sets selectable events as follows: |
| 436 |
pni_events_update(iocpd, PN_READABLE | PN_EXPIRED); |
| 437 |
} else { |
| 438 |
release_sys_sendbuf(iocpd->socket); |
| 439 |
if (setsockopt(iocpd->socket, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0)) { |
| 440 |
iocpdesc_fail(iocpd, WSAGetLastError(), "Internal connect failure (update context)"); |
| 441 |
} else { |
| 442 |
pni_events_update(iocpd, PN_WRITABLE); |
| 443 |
start_reading(iocpd); |
| 444 |
} |
| 445 |
} |
| 446 |
pn_free(result); |
| 447 |
return; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
// === Async writes |
| 452 |
|
| 453 |
static bool write_in_progress(iocpdesc_t *iocpd) |
| 454 |
{ |
| 455 |
return pni_write_pipeline_size(iocpd->pipeline) != 0; |
| 456 |
} |
| 457 |
|
| 458 |
write_result_t *pni_write_result(iocpdesc_t *iocpd, const char *buf, size_t buflen) |
| 459 |
{ |
| 460 |
write_result_t *result = (write_result_t *) calloc(sizeof(write_result_t), 1); |
| 461 |
if (result) { |
| 462 |
result->base.type = IOCP_WRITE; |
| 463 |
result->base.iocpd = iocpd; |
| 464 |
result->buffer.start = buf; |
| 465 |
result->buffer.size = buflen; |
| 466 |
} |
| 467 |
return result; |
| 468 |
} |
| 469 |
|
| 470 |
static int submit_write(write_result_t *result, const void *buf, size_t len) |
| 471 |
{ |
| 472 |
WSABUF wsabuf; |
| 473 |
wsabuf.buf = (char *) buf; |
| 474 |
wsabuf.len = len; |
| 475 |
memset(&result->base.overlapped, 0, sizeof (OVERLAPPED)); |
| 476 |
return WSASend(result->base.iocpd->socket, &wsabuf, 1, NULL, 0, |
| 477 |
(LPOVERLAPPED) result, 0); |
| 478 |
} |
| 479 |
|
| 480 |
ssize_t pni_iocp_begin_write(iocpdesc_t *iocpd, const void *buf, size_t len, bool *would_block, pn_error_t *error) |
| 481 |
{ |
| 482 |
if (len == 0) return 0; |
| 483 |
*would_block = false; |
| 484 |
if (is_listener(iocpd)) { |
| 485 |
set_iocp_error_status(error, PN_ERR, WSAEOPNOTSUPP); |
| 486 |
return INVALID_SOCKET; |
| 487 |
} |
| 488 |
if (iocpd->closing) { |
| 489 |
set_iocp_error_status(error, PN_ERR, WSAESHUTDOWN); |
| 490 |
return SOCKET_ERROR; |
| 491 |
} |
| 492 |
if (iocpd->write_closed) { |
| 493 |
assert(pn_error_code(iocpd->error)); |
| 494 |
pn_error_copy(error, iocpd->error); |
| 495 |
if (iocpd->iocp->iocp_trace) |
| 496 |
iocp_log("write error: %s\n", pn_error_text(error)); |
| 497 |
return SOCKET_ERROR; |
| 498 |
} |
| 499 |
if (len == 0) return 0; |
| 500 |
if (!(iocpd->events & PN_WRITABLE)) { |
| 501 |
*would_block = true; |
| 502 |
return SOCKET_ERROR; |
| 503 |
} |
| 504 |
|
| 505 |
size_t written = 0; |
| 506 |
size_t requested = len; |
| 507 |
const char *outgoing = (const char *) buf; |
| 508 |
size_t available = pni_write_pipeline_reserve(iocpd->pipeline, len); |
| 509 |
if (!available) { |
| 510 |
*would_block = true; |
| 511 |
return SOCKET_ERROR; |
| 512 |
} |
| 513 |
|
| 514 |
for (size_t wr_count = 0; wr_count < available; wr_count++) { |
| 515 |
write_result_t *result = pni_write_pipeline_next(iocpd->pipeline); |
| 516 |
assert(result); |
| 517 |
result->base.iocpd = iocpd; |
| 518 |
ssize_t actual_len = pn_min(len, result->buffer.size); |
| 519 |
result->requested = actual_len; |
| 520 |
memmove((void *)result->buffer.start, outgoing, actual_len); |
| 521 |
outgoing += actual_len; |
| 522 |
written += actual_len; |
| 523 |
len -= actual_len; |
| 524 |
|
| 525 |
int werror = submit_write(result, result->buffer.start, actual_len); |
| 526 |
if (werror && WSAGetLastError() != ERROR_IO_PENDING) { |
| 527 |
pni_write_pipeline_return(iocpd->pipeline, result); |
| 528 |
iocpdesc_fail(iocpd, WSAGetLastError(), "overlapped send"); |
| 529 |
return SOCKET_ERROR; |
| 530 |
} |
| 531 |
iocpd->ops_in_progress++; |
| 532 |
} |
| 533 |
|
| 534 |
if (!pni_write_pipeline_writable(iocpd->pipeline)) |
| 535 |
pni_events_update(iocpd, iocpd->events & ~PN_WRITABLE); |
| 536 |
return written; |
| 537 |
} |
| 538 |
|
| 539 |
/* |
| 540 |
* Note: iocp write completion is not "bytes on the wire", it is "peer |
| 541 |
* acked the sent bytes". Completion can be seconds on a slow |
| 542 |
* consuming peer. |
| 543 |
*/ |
| 544 |
static void complete_write(write_result_t *result, DWORD xfer_count, HRESULT status) |
| 545 |
{ |
| 546 |
iocpdesc_t *iocpd = result->base.iocpd; |
| 547 |
if (iocpd->closing) { |
| 548 |
pni_write_pipeline_return(iocpd->pipeline, result); |
| 549 |
if (!iocpd->write_closed && !write_in_progress(iocpd)) |
| 550 |
iocp_shutdown(iocpd); |
| 551 |
reap_check(iocpd); |
| 552 |
return; |
| 553 |
} |
| 554 |
if (status == 0 && xfer_count > 0) { |
| 555 |
if (xfer_count != result->requested) { |
| 556 |
// Is this recoverable? How to preserve order if multiple overlapped writes? |
| 557 |
pni_write_pipeline_return(iocpd->pipeline, result); |
| 558 |
iocpdesc_fail(iocpd, WSA_OPERATION_ABORTED, "Partial overlapped write on socket"); |
| 559 |
return; |
| 560 |
} else { |
| 561 |
// Success. |
| 562 |
pni_write_pipeline_return(iocpd->pipeline, result); |
| 563 |
if (pni_write_pipeline_writable(iocpd->pipeline)) |
| 564 |
pni_events_update(iocpd, iocpd->events | PN_WRITABLE); |
| 565 |
return; |
| 566 |
} |
| 567 |
} |
| 568 |
// Other error |
| 569 |
pni_write_pipeline_return(iocpd->pipeline, result); |
| 570 |
if (status == WSAECONNABORTED || status == WSAECONNRESET || status == WSAENOTCONN |
| 571 |
|| status == ERROR_NETNAME_DELETED) { |
| 572 |
iocpd->write_closed = true; |
| 573 |
iocpd->poll_error = true; |
| 574 |
pni_events_update(iocpd, iocpd->events & ~PN_WRITABLE); |
| 575 |
pni_win32_error(iocpd->error, "Remote close or timeout", status); |
| 576 |
} else { |
| 577 |
iocpdesc_fail(iocpd, status, "IOCP async write error"); |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
// === Async reads |
| 583 |
|
| 584 |
struct read_result_t { |
| 585 |
iocp_result_t base; |
| 586 |
size_t drain_count; |
| 587 |
char unused_buf[1]; |
| 588 |
}; |
| 589 |
|
| 590 |
static read_result_t *read_result(iocpdesc_t *iocpd) |
| 591 |
{ |
| 592 |
read_result_t *result = (read_result_t *) calloc(sizeof(read_result_t), 1); |
| 593 |
if (result) { |
| 594 |
result->base.type = IOCP_READ; |
| 595 |
result->base.iocpd = iocpd; |
| 596 |
} |
| 597 |
return result; |
| 598 |
} |
| 599 |
|
| 600 |
static void begin_zero_byte_read(iocpdesc_t *iocpd) |
| 601 |
{ |
| 602 |
if (iocpd->read_in_progress) return; |
| 603 |
if (iocpd->read_closed) { |
| 604 |
pni_events_update(iocpd, iocpd->events | PN_READABLE); |
| 605 |
return; |
| 606 |
} |
| 607 |
|
| 608 |
read_result_t *result = iocpd->read_result; |
| 609 |
memset(&result->base.overlapped, 0, sizeof (OVERLAPPED)); |
| 610 |
DWORD flags = 0; |
| 611 |
WSABUF wsabuf; |
| 612 |
wsabuf.buf = result->unused_buf; |
| 613 |
wsabuf.len = 0; |
| 614 |
int rc = WSARecv(iocpd->socket, &wsabuf, 1, NULL, &flags, |
| 615 |
&result->base.overlapped, 0); |
| 616 |
if (rc && WSAGetLastError() != ERROR_IO_PENDING) { |
| 617 |
iocpdesc_fail(iocpd, WSAGetLastError(), "IOCP read error"); |
| 618 |
return; |
| 619 |
} |
| 620 |
iocpd->ops_in_progress++; |
| 621 |
iocpd->read_in_progress = true; |
| 622 |
} |
| 623 |
|
| 624 |
static void drain_until_closed(iocpdesc_t *iocpd) { |
| 625 |
size_t max_drain = 16 * 1024; |
| 626 |
char buf[512]; |
| 627 |
read_result_t *result = iocpd->read_result; |
| 628 |
while (result->drain_count < max_drain) { |
| 629 |
int rv = recv(iocpd->socket, buf, 512, 0); |
| 630 |
if (rv > 0) |
| 631 |
result->drain_count += rv; |
| 632 |
else if (rv == 0) { |
| 633 |
iocpd->read_closed = true; |
| 634 |
return; |
| 635 |
} else if (WSAGetLastError() == WSAEWOULDBLOCK) { |
| 636 |
// wait a little longer |
| 637 |
start_reading(iocpd); |
| 638 |
return; |
| 639 |
} |
| 640 |
else |
| 641 |
break; |
| 642 |
} |
| 643 |
// Graceful close indication unlikely, force the issue |
| 644 |
if (iocpd->iocp->iocp_trace) |
| 645 |
if (result->drain_count >= max_drain) |
| 646 |
iocp_log("graceful close on reader abandoned (too many chars)\n"); |
| 647 |
else |
| 648 |
iocp_log("graceful close on reader abandoned: %d\n", WSAGetLastError()); |
| 649 |
iocpd->read_closed = true; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
static void complete_read(read_result_t *result, DWORD xfer_count, HRESULT status) |
| 654 |
{ |
| 655 |
iocpdesc_t *iocpd = result->base.iocpd; |
| 656 |
iocpd->read_in_progress = false; |
| 657 |
|
| 658 |
if (iocpd->closing) { |
| 659 |
// Application no longer reading, but we are looking for a zero length read |
| 660 |
if (!iocpd->read_closed) |
| 661 |
drain_until_closed(iocpd); |
| 662 |
reap_check(iocpd); |
| 663 |
return; |
| 664 |
} |
| 665 |
|
| 666 |
if (status == 0 && xfer_count == 0) { |
| 667 |
// Success. |
| 668 |
pni_events_update(iocpd, iocpd->events | PN_READABLE); |
| 669 |
} else { |
| 670 |
iocpdesc_fail(iocpd, status, "IOCP read complete error"); |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
ssize_t pni_iocp_recv(iocpdesc_t *iocpd, void *buf, size_t size, bool *would_block, pn_error_t *error) |
| 675 |
{ |
| 676 |
if (size == 0) return 0; |
| 677 |
*would_block = false; |
| 678 |
if (is_listener(iocpd)) { |
| 679 |
set_iocp_error_status(error, PN_ERR, WSAEOPNOTSUPP); |
| 680 |
return SOCKET_ERROR; |
| 681 |
} |
| 682 |
if (iocpd->closing) { |
| 683 |
// Previous call to pn_close() |
| 684 |
set_iocp_error_status(error, PN_ERR, WSAESHUTDOWN); |
| 685 |
return SOCKET_ERROR; |
| 686 |
} |
| 687 |
if (iocpd->read_closed) { |
| 688 |
if (pn_error_code(iocpd->error)) |
| 689 |
pn_error_copy(error, iocpd->error); |
| 690 |
else |
| 691 |
set_iocp_error_status(error, PN_ERR, WSAENOTCONN); |
| 692 |
return SOCKET_ERROR; |
| 693 |
} |
| 694 |
|
| 695 |
int count = recv(iocpd->socket, (char *) buf, size, 0); |
| 696 |
if (count > 0) { |
| 697 |
pni_events_update(iocpd, iocpd->events & ~PN_READABLE); |
| 698 |
begin_zero_byte_read(iocpd); |
| 699 |
return (ssize_t) count; |
| 700 |
} else if (count == 0) { |
| 701 |
iocpd->read_closed = true; |
| 702 |
return 0; |
| 703 |
} |
| 704 |
if (WSAGetLastError() == WSAEWOULDBLOCK) |
| 705 |
*would_block = true; |
| 706 |
else { |
| 707 |
set_iocp_error_status(error, PN_ERR, WSAGetLastError()); |
| 708 |
iocpd->read_closed = true; |
| 709 |
} |
| 710 |
return SOCKET_ERROR; |
| 711 |
} |
| 712 |
|
| 713 |
static void start_reading(iocpdesc_t *iocpd) |
| 714 |
{ |
| 715 |
begin_zero_byte_read(iocpd); |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
// === The iocp descriptor |
| 720 |
|
| 721 |
static void pni_iocpdesc_initialize(void *object) |
| 722 |
{ |
| 723 |
iocpdesc_t *iocpd = (iocpdesc_t *) object; |
| 724 |
memset(iocpd, 0, sizeof(iocpdesc_t)); |
| 725 |
iocpd->socket = INVALID_SOCKET; |
| 726 |
} |
| 727 |
|
| 728 |
static void pni_iocpdesc_finalize(void *object) |
| 729 |
{ |
| 730 |
iocpdesc_t *iocpd = (iocpdesc_t *) object; |
| 731 |
pn_free(iocpd->acceptor); |
| 732 |
pn_error_free(iocpd->error); |
| 733 |
if (iocpd->pipeline) |
| 734 |
if (write_in_progress(iocpd)) |
| 735 |
iocp_log("iocp descriptor write leak\n"); |
| 736 |
else |
| 737 |
pn_free(iocpd->pipeline); |
| 738 |
if (iocpd->read_in_progress) |
| 739 |
iocp_log("iocp descriptor read leak\n"); |
| 740 |
else |
| 741 |
free(iocpd->read_result); |
| 742 |
} |
| 743 |
|
| 744 |
static uintptr_t pni_iocpdesc_hashcode(void *object) |
| 745 |
{ |
| 746 |
iocpdesc_t *iocpd = (iocpdesc_t *) object; |
| 747 |
return iocpd->socket; |
| 748 |
} |
| 749 |
|
| 750 |
#define CID_pni_iocpdesc CID_pn_void |
| 751 |
#define pni_iocpdesc_compare NULL |
| 752 |
#define pni_iocpdesc_inspect NULL |
| 753 |
|
| 754 |
// Reference counted in the iocpdesc map, zombie_list, selector. |
| 755 |
static iocpdesc_t *pni_iocpdesc(pn_socket_t s) |
| 756 |
{ |
| 757 |
static pn_class_t clazz = PN_CLASS(pni_iocpdesc); |
| 758 |
iocpdesc_t *iocpd = (iocpdesc_t *) pn_class_new(&clazz, sizeof(iocpdesc_t)); |
| 759 |
assert(iocpd); |
| 760 |
iocpd->socket = s; |
| 761 |
return iocpd; |
| 762 |
} |
| 763 |
|
| 764 |
static bool is_listener_socket(pn_socket_t s) |
| 765 |
{ |
| 766 |
BOOL tval = false; |
| 767 |
int tvalsz = sizeof(tval); |
| 768 |
int code = getsockopt(s, SOL_SOCKET, SO_ACCEPTCONN, (char *)&tval, &tvalsz); |
| 769 |
return code == 0 && tval; |
| 770 |
} |
| 771 |
|
| 772 |
iocpdesc_t *pni_iocpdesc_create(iocp_t *iocp, pn_socket_t s, bool external) { |
| 773 |
assert (s != INVALID_SOCKET); |
| 774 |
assert(!pni_iocpdesc_map_get(iocp, s)); |
| 775 |
bool listening = is_listener_socket(s); |
| 776 |
iocpdesc_t *iocpd = pni_iocpdesc(s); |
| 777 |
iocpd->iocp = iocp; |
| 778 |
if (iocpd) { |
| 779 |
iocpd->external = external; |
| 780 |
iocpd->error = pn_error(); |
| 781 |
if (listening) { |
| 782 |
iocpd->acceptor = pni_acceptor(iocpd); |
| 783 |
} else { |
| 784 |
iocpd->pipeline = pni_write_pipeline(iocpd); |
| 785 |
iocpd->read_result = read_result(iocpd); |
| 786 |
} |
| 787 |
pni_iocpdesc_map_push(iocpd); |
| 788 |
} |
| 789 |
return iocpd; |
| 790 |
} |
| 791 |
|
| 792 |
iocpdesc_t *pni_deadline_desc(iocp_t *iocp) { |
| 793 |
// Non IO descriptor for selector deadlines. Do not add to iocpdesc map or |
| 794 |
// zombie list. Selector responsible to free/decref object. |
| 795 |
iocpdesc_t *iocpd = pni_iocpdesc(PN_INVALID_SOCKET); |
| 796 |
iocpd->iocp = iocp; |
| 797 |
iocpd->deadline_desc = true; |
| 798 |
return iocpd; |
| 799 |
} |
| 800 |
|
| 801 |
// === Fast lookup of a socket's iocpdesc_t |
| 802 |
|
| 803 |
iocpdesc_t *pni_iocpdesc_map_get(iocp_t *iocp, pn_socket_t s) { |
| 804 |
iocpdesc_t *iocpd = (iocpdesc_t *) pn_hash_get(iocp->iocpdesc_map, s); |
| 805 |
return iocpd; |
| 806 |
} |
| 807 |
|
| 808 |
void pni_iocpdesc_map_push(iocpdesc_t *iocpd) { |
| 809 |
pn_hash_put(iocpd->iocp->iocpdesc_map, iocpd->socket, iocpd); |
| 810 |
pn_decref(iocpd); |
| 811 |
assert(pn_refcount(iocpd) == 1); |
| 812 |
} |
| 813 |
|
| 814 |
void pni_iocpdesc_map_del(iocp_t *iocp, pn_socket_t s) { |
| 815 |
pn_hash_del(iocp->iocpdesc_map, (uintptr_t) s); |
| 816 |
} |
| 817 |
|
| 818 |
static void bind_to_completion_port(iocpdesc_t *iocpd) |
| 819 |
{ |
| 820 |
if (iocpd->bound) return; |
| 821 |
if (!iocpd->iocp->completion_port) { |
| 822 |
iocpdesc_fail(iocpd, WSAEINVAL, "Incomplete setup, no completion port."); |
| 823 |
return; |
| 824 |
} |
| 825 |
|
| 826 |
if (CreateIoCompletionPort ((HANDLE) iocpd->socket, iocpd->iocp->completion_port, 0, 0)) |
| 827 |
iocpd->bound = true; |
| 828 |
else { |
| 829 |
iocpdesc_fail(iocpd, GetLastError(), "IOCP socket setup."); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
static void release_sys_sendbuf(SOCKET s) |
| 834 |
{ |
| 835 |
// Set the socket's send buffer size to zero. |
| 836 |
int sz = 0; |
| 837 |
int status = setsockopt(s, SOL_SOCKET, SO_SNDBUF, (const char *)&sz, sizeof(int)); |
| 838 |
assert(status == 0); |
| 839 |
} |
| 840 |
|
| 841 |
void pni_iocpdesc_start(iocpdesc_t *iocpd) |
| 842 |
{ |
| 843 |
if (iocpd->bound) return; |
| 844 |
bind_to_completion_port(iocpd); |
| 845 |
if (is_listener(iocpd)) { |
| 846 |
begin_accept(iocpd->acceptor, NULL); |
| 847 |
} |
| 848 |
else { |
| 849 |
release_sys_sendbuf(iocpd->socket); |
| 850 |
pni_events_update(iocpd, PN_WRITABLE); |
| 851 |
start_reading(iocpd); |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
static void complete(iocp_result_t *result, bool success, DWORD num_transferred) { |
| 856 |
result->iocpd->ops_in_progress--; |
| 857 |
DWORD status = success ? 0 : GetLastError(); |
| 858 |
|
| 859 |
switch (result->type) { |
| 860 |
case IOCP_ACCEPT: |
| 861 |
complete_accept((accept_result_t *) result, status); |
| 862 |
break; |
| 863 |
case IOCP_CONNECT: |
| 864 |
complete_connect((connect_result_t *) result, status); |
| 865 |
break; |
| 866 |
case IOCP_WRITE: |
| 867 |
complete_write((write_result_t *) result, num_transferred, status); |
| 868 |
break; |
| 869 |
case IOCP_READ: |
| 870 |
complete_read((read_result_t *) result, num_transferred, status); |
| 871 |
break; |
| 872 |
default: |
| 873 |
assert(false); |
| 874 |
} |
| 875 |
} |
| 876 |
|
| 877 |
void pni_iocp_drain_completions(iocp_t *iocp) |
| 878 |
{ |
| 879 |
while (true) { |
| 880 |
DWORD timeout_ms = 0; |
| 881 |
DWORD num_transferred = 0; |
| 882 |
ULONG_PTR completion_key = 0; |
| 883 |
OVERLAPPED *overlapped = 0; |
| 884 |
|
| 885 |
bool good_op = GetQueuedCompletionStatus (iocp->completion_port, &num_transferred, |
| 886 |
&completion_key, &overlapped, timeout_ms); |
| 887 |
if (!overlapped) |
| 888 |
return; // timed out |
| 889 |
iocp_result_t *result = (iocp_result_t *) overlapped; |
| 890 |
complete(result, good_op, num_transferred); |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
// returns: -1 on error, 0 on timeout, 1 successful completion |
| 895 |
int pni_iocp_wait_one(iocp_t *iocp, int timeout, pn_error_t *error) { |
| 896 |
DWORD win_timeout = (timeout < 0) ? INFINITE : (DWORD) timeout; |
| 897 |
DWORD num_transferred = 0; |
| 898 |
ULONG_PTR completion_key = 0; |
| 899 |
OVERLAPPED *overlapped = 0; |
| 900 |
|
| 901 |
bool good_op = GetQueuedCompletionStatus (iocp->completion_port, &num_transferred, |
| 902 |
&completion_key, &overlapped, win_timeout); |
| 903 |
if (!overlapped) |
| 904 |
if (GetLastError() == WAIT_TIMEOUT) |
| 905 |
return 0; |
| 906 |
else { |
| 907 |
if (error) |
| 908 |
pni_win32_error(error, "GetQueuedCompletionStatus", GetLastError()); |
| 909 |
return -1; |
| 910 |
} |
| 911 |
|
| 912 |
iocp_result_t *result = (iocp_result_t *) overlapped; |
| 913 |
complete(result, good_op, num_transferred); |
| 914 |
return 1; |
| 915 |
} |
| 916 |
|
| 917 |
// === Close (graceful and otherwise) |
| 918 |
|
| 919 |
// zombie_list is for sockets transitioning out of iocp on their way to zero ops_in_progress |
| 920 |
// and fully closed. |
| 921 |
|
| 922 |
static void zombie_list_add(iocpdesc_t *iocpd) |
| 923 |
{ |
| 924 |
assert(iocpd->closing); |
| 925 |
if (!iocpd->ops_in_progress) { |
| 926 |
// No need to make a zombie. |
| 927 |
if (iocpd->socket != INVALID_SOCKET) { |
| 928 |
closesocket(iocpd->socket); |
| 929 |
iocpd->socket = INVALID_SOCKET; |
| 930 |
iocpd->read_closed = true; |
| 931 |
} |
| 932 |
return; |
| 933 |
} |
| 934 |
// Allow 2 seconds for graceful shutdown before releasing socket resource. |
| 935 |
iocpd->reap_time = pn_i_now() + 2000; |
| 936 |
pn_list_add(iocpd->iocp->zombie_list, iocpd); |
| 937 |
} |
| 938 |
|
| 939 |
static void reap_check(iocpdesc_t *iocpd) |
| 940 |
{ |
| 941 |
if (iocpd->closing && !iocpd->ops_in_progress) { |
| 942 |
if (iocpd->socket != INVALID_SOCKET) { |
| 943 |
closesocket(iocpd->socket); |
| 944 |
iocpd->socket = INVALID_SOCKET; |
| 945 |
} |
| 946 |
pn_list_remove(iocpd->iocp->zombie_list, iocpd); |
| 947 |
// iocpd is decref'ed and possibly released |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
pn_timestamp_t pni_zombie_deadline(iocp_t *iocp) |
| 952 |
{ |
| 953 |
if (pn_list_size(iocp->zombie_list)) { |
| 954 |
iocpdesc_t *iocpd = (iocpdesc_t *) pn_list_get(iocp->zombie_list, 0); |
| 955 |
return iocpd->reap_time; |
| 956 |
} |
| 957 |
return 0; |
| 958 |
} |
| 959 |
|
| 960 |
void pni_zombie_check(iocp_t *iocp, pn_timestamp_t now) |
| 961 |
{ |
| 962 |
pn_list_t *zl = iocp->zombie_list; |
| 963 |
// Look for stale zombies that should have been reaped by "now" |
| 964 |
for (size_t idx = 0; idx < pn_list_size(zl); idx++) { |
| 965 |
iocpdesc_t *iocpd = (iocpdesc_t *) pn_list_get(zl, idx); |
| 966 |
if (iocpd->reap_time > now) |
| 967 |
return; |
| 968 |
if (iocpd->socket == INVALID_SOCKET) |
| 969 |
continue; |
| 970 |
assert(iocpd->ops_in_progress > 0); |
| 971 |
if (iocp->iocp_trace) |
| 972 |
iocp_log("async close: graceful close timeout exceeded\n"); |
| 973 |
closesocket(iocpd->socket); |
| 974 |
iocpd->socket = INVALID_SOCKET; |
| 975 |
iocpd->read_closed = true; |
| 976 |
// outstanding ops should complete immediately now |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
static void drain_zombie_completions(iocp_t *iocp) |
| 981 |
{ |
| 982 |
// No more pn_selector_select() from App, but zombies still need care and feeding |
| 983 |
// until their outstanding async actions complete. |
| 984 |
pni_iocp_drain_completions(iocp); |
| 985 |
|
| 986 |
// Discard any that have no pending async IO |
| 987 |
size_t sz = pn_list_size(iocp->zombie_list); |
| 988 |
for (size_t idx = 0; idx < sz;) { |
| 989 |
iocpdesc_t *iocpd = (iocpdesc_t *) pn_list_get(iocp->zombie_list, idx); |
| 990 |
if (!iocpd->ops_in_progress) { |
| 991 |
pn_list_del(iocp->zombie_list, idx, 1); |
| 992 |
sz--; |
| 993 |
} else { |
| 994 |
idx++; |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
unsigned shutdown_grace = 2000; |
| 999 |
char *override = getenv("PN_SHUTDOWN_GRACE"); |
| 1000 |
if (override) { |
| 1001 |
int grace = atoi(override); |
| 1002 |
if (grace > 0 && grace < 60000) |
| 1003 |
shutdown_grace = (unsigned) grace; |
| 1004 |
} |
| 1005 |
pn_timestamp_t now = pn_i_now(); |
| 1006 |
pn_timestamp_t deadline = now + shutdown_grace; |
| 1007 |
|
| 1008 |
while (pn_list_size(iocp->zombie_list)) { |
| 1009 |
if (now >= deadline) |
| 1010 |
break; |
| 1011 |
int rv = pni_iocp_wait_one(iocp, deadline - now, NULL); |
| 1012 |
if (rv < 0) { |
| 1013 |
iocp_log("unexpected IOCP failure on Proton IO shutdown %d\n", GetLastError()); |
| 1014 |
break; |
| 1015 |
} |
| 1016 |
now = pn_i_now(); |
| 1017 |
} |
| 1018 |
if (now >= deadline && pn_list_size(iocp->zombie_list) && iocp->iocp_trace) |
| 1019 |
// Should only happen if really slow TCP handshakes, i.e. total network failure |
| 1020 |
iocp_log("network failure on Proton shutdown\n"); |
| 1021 |
} |
| 1022 |
|
| 1023 |
static pn_list_t *iocp_map_close_all(iocp_t *iocp) |
| 1024 |
{ |
| 1025 |
// Zombify stragglers, i.e. no pn_close() from the application. |
| 1026 |
pn_list_t *externals = pn_list(PN_OBJECT, 0); |
| 1027 |
for (pn_handle_t entry = pn_hash_head(iocp->iocpdesc_map); entry; |
| 1028 |
entry = pn_hash_next(iocp->iocpdesc_map, entry)) { |
| 1029 |
iocpdesc_t *iocpd = (iocpdesc_t *) pn_hash_value(iocp->iocpdesc_map, entry); |
| 1030 |
// Just listeners first. |
| 1031 |
if (is_listener(iocpd)) { |
| 1032 |
if (iocpd->external) { |
| 1033 |
// Owned by application, just keep a temporary reference to it. |
| 1034 |
// iocp_result_t structs must not be free'd until completed or |
| 1035 |
// the completion port is closed. |
| 1036 |
if (iocpd->ops_in_progress) |
| 1037 |
pn_list_add(externals, iocpd); |
| 1038 |
pni_iocpdesc_map_del(iocp, iocpd->socket); |
| 1039 |
} else { |
| 1040 |
// Make it a zombie. |
| 1041 |
pni_iocp_begin_close(iocpd); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
} |
| 1045 |
pni_iocp_drain_completions(iocp); |
| 1046 |
|
| 1047 |
for (pn_handle_t entry = pn_hash_head(iocp->iocpdesc_map); entry; |
| 1048 |
entry = pn_hash_next(iocp->iocpdesc_map, entry)) { |
| 1049 |
iocpdesc_t *iocpd = (iocpdesc_t *) pn_hash_value(iocp->iocpdesc_map, entry); |
| 1050 |
if (iocpd->external) { |
| 1051 |
iocpd->read_closed = true; // Do not consume from read side |
| 1052 |
iocpd->write_closed = true; // Do not shutdown write side |
| 1053 |
if (iocpd->ops_in_progress) |
| 1054 |
pn_list_add(externals, iocpd); |
| 1055 |
pni_iocpdesc_map_del(iocp, iocpd->socket); |
| 1056 |
} else { |
| 1057 |
// Make it a zombie. |
| 1058 |
pni_iocp_begin_close(iocpd); |
| 1059 |
} |
| 1060 |
} |
| 1061 |
return externals; |
| 1062 |
} |
| 1063 |
|
| 1064 |
static void zombie_list_hard_close_all(iocp_t *iocp) |
| 1065 |
{ |
| 1066 |
pni_iocp_drain_completions(iocp); |
| 1067 |
size_t zs = pn_list_size(iocp->zombie_list); |
| 1068 |
for (size_t i = 0; i < zs; i++) { |
| 1069 |
iocpdesc_t *iocpd = (iocpdesc_t *) pn_list_get(iocp->zombie_list, i); |
| 1070 |
if (iocpd->socket != INVALID_SOCKET) { |
| 1071 |
closesocket(iocpd->socket); |
| 1072 |
iocpd->socket = INVALID_SOCKET; |
| 1073 |
iocpd->read_closed = true; |
| 1074 |
iocpd->write_closed = true; |
| 1075 |
} |
| 1076 |
} |
| 1077 |
pni_iocp_drain_completions(iocp); |
| 1078 |
|
| 1079 |
// Zombies should be all gone. Do a sanity check. |
| 1080 |
zs = pn_list_size(iocp->zombie_list); |
| 1081 |
int remaining = 0; |
| 1082 |
int ops = 0; |
| 1083 |
for (size_t i = 0; i < zs; i++) { |
| 1084 |
iocpdesc_t *iocpd = (iocpdesc_t *) pn_list_get(iocp->zombie_list, i); |
| 1085 |
remaining++; |
| 1086 |
ops += iocpd->ops_in_progress; |
| 1087 |
} |
| 1088 |
if (remaining) |
| 1089 |
iocp_log("Proton: %d unfinished close operations (ops count = %d)\n", remaining, ops); |
| 1090 |
} |
| 1091 |
|
| 1092 |
static void iocp_shutdown(iocpdesc_t *iocpd) |
| 1093 |
{ |
| 1094 |
if (iocpd->socket == PN_INVALID_SOCKET) |
| 1095 |
return; // Hard close in progress |
| 1096 |
if (shutdown(iocpd->socket, SD_SEND)) { |
| 1097 |
int err = WSAGetLastError(); |
| 1098 |
if (err != WSAECONNABORTED && err != WSAECONNRESET && err != WSAENOTCONN) |
| 1099 |
if (iocpd->iocp->iocp_trace) |
| 1100 |
iocp_log("socket shutdown failed %d\n", err); |
| 1101 |
} |
| 1102 |
iocpd->write_closed = true; |
| 1103 |
} |
| 1104 |
|
| 1105 |
void pni_iocp_begin_close(iocpdesc_t *iocpd) |
| 1106 |
{ |
| 1107 |
assert (!iocpd->closing); |
| 1108 |
if (is_listener(iocpd)) { |
| 1109 |
// Listening socket is easy. Close the socket which will cancel async ops. |
| 1110 |
pn_socket_t old_sock = iocpd->socket; |
| 1111 |
iocpd->socket = INVALID_SOCKET; |
| 1112 |
iocpd->closing = true; |
| 1113 |
iocpd->read_closed = true; |
| 1114 |
iocpd->write_closed = true; |
| 1115 |
closesocket(old_sock); |
| 1116 |
// Pending accepts will now complete. Zombie can die when all consumed. |
| 1117 |
zombie_list_add(iocpd); |
| 1118 |
pni_iocpdesc_map_del(iocpd->iocp, old_sock); // may pn_free *iocpd |
| 1119 |
} else { |
| 1120 |
// Continue async operation looking for graceful close confirmation or timeout. |
| 1121 |
pn_socket_t old_sock = iocpd->socket; |
| 1122 |
iocpd->closing = true; |
| 1123 |
if (!iocpd->write_closed && !write_in_progress(iocpd)) |
| 1124 |
iocp_shutdown(iocpd); |
| 1125 |
zombie_list_add(iocpd); |
| 1126 |
pni_iocpdesc_map_del(iocpd->iocp, old_sock); // may pn_free *iocpd |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
|
| 1131 |
// === iocp_t |
| 1132 |
|
| 1133 |
#define CID_pni_iocp CID_pn_void |
| 1134 |
#define pni_iocp_hashcode NULL |
| 1135 |
#define pni_iocp_compare NULL |
| 1136 |
#define pni_iocp_inspect NULL |
| 1137 |
|
| 1138 |
void pni_iocp_initialize(void *obj) |
| 1139 |
{ |
| 1140 |
iocp_t *iocp = (iocp_t *) obj; |
| 1141 |
memset(iocp, 0, sizeof(iocp_t)); |
| 1142 |
pni_shared_pool_create(iocp); |
| 1143 |
iocp->completion_port = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0); |
| 1144 |
assert(iocp->completion_port != NULL); |
| 1145 |
iocp->iocpdesc_map = pn_hash(PN_OBJECT, 0, 0.75); |
| 1146 |
iocp->zombie_list = pn_list(PN_OBJECT, 0); |
| 1147 |
iocp->iocp_trace = pn_env_bool("PN_TRACE_DRV"); |
| 1148 |
iocp->selector = NULL; |
| 1149 |
} |
| 1150 |
|
| 1151 |
void pni_iocp_finalize(void *obj) |
| 1152 |
{ |
| 1153 |
iocp_t *iocp = (iocp_t *) obj; |
| 1154 |
// Move sockets to closed state, except external sockets. |
| 1155 |
pn_list_t *externals = iocp_map_close_all(iocp); |
| 1156 |
// Now everything with ops_in_progress is in the zombie_list or the externals list. |
| 1157 |
assert(!pn_hash_head(iocp->iocpdesc_map)); |
| 1158 |
pn_free(iocp->iocpdesc_map); |
| 1159 |
|
| 1160 |
drain_zombie_completions(iocp); // Last chance for graceful close |
| 1161 |
zombie_list_hard_close_all(iocp); |
| 1162 |
CloseHandle(iocp->completion_port); // This cancels all our async ops |
| 1163 |
iocp->completion_port = NULL; |
| 1164 |
|
| 1165 |
if (pn_list_size(externals) && iocp->iocp_trace) |
| 1166 |
iocp_log("%d external sockets not closed and removed from Proton IOCP control\n", pn_list_size(externals)); |
| 1167 |
|
| 1168 |
// Now safe to free everything that might be touched by a former async operation. |
| 1169 |
pn_free(externals); |
| 1170 |
pn_free(iocp->zombie_list); |
| 1171 |
pni_shared_pool_free(iocp); |
| 1172 |
} |
| 1173 |
|
| 1174 |
iocp_t *pni_iocp() |
| 1175 |
{ |
| 1176 |
static const pn_class_t clazz = PN_CLASS(pni_iocp); |
| 1177 |
iocp_t *iocp = (iocp_t *) pn_class_new(&clazz, sizeof(iocp_t)); |
| 1178 |
return iocp; |
| 1179 |
} |