Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #include <grpc/support/port_platform.h> |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 35 | |
| 36 | #ifdef GPR_WINSOCK_SOCKET |
| 37 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 38 | #include <winsock2.h> |
Nicolas Noble | 77ccdb9 | 2015-09-03 17:35:26 -0700 | [diff] [blame] | 39 | |
Craig Tiller | 534ce4e | 2016-03-21 14:41:29 -0700 | [diff] [blame] | 40 | // must be included after winsock2.h |
| 41 | #include <mswsock.h> |
| 42 | |
Nicolas "Pixel" Noble | 404fc6a | 2015-05-02 02:34:39 -0700 | [diff] [blame] | 43 | #include <grpc/support/alloc.h> |
| 44 | #include <grpc/support/log.h> |
Nicolas Noble | 77ccdb9 | 2015-09-03 17:35:26 -0700 | [diff] [blame] | 45 | #include <grpc/support/log_win32.h> |
Nicolas "Pixel" Noble | e503cd5 | 2015-07-14 02:27:23 +0200 | [diff] [blame] | 46 | #include <grpc/support/string_util.h> |
Nicolas "Pixel" Noble | 404fc6a | 2015-05-02 02:34:39 -0700 | [diff] [blame] | 47 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 48 | #include "src/core/lib/iomgr/iocp_windows.h" |
| 49 | #include "src/core/lib/iomgr/iomgr_internal.h" |
| 50 | #include "src/core/lib/iomgr/pollset.h" |
| 51 | #include "src/core/lib/iomgr/pollset_windows.h" |
| 52 | #include "src/core/lib/iomgr/socket_windows.h" |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 53 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 54 | grpc_winsocket *grpc_winsocket_create(SOCKET socket, const char *name) { |
Jan Tattermusch | 7913dea | 2015-07-09 20:37:17 -0700 | [diff] [blame] | 55 | char *final_name; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 56 | grpc_winsocket *r = gpr_malloc(sizeof(grpc_winsocket)); |
| 57 | memset(r, 0, sizeof(grpc_winsocket)); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 58 | r->socket = socket; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 59 | gpr_mu_init(&r->state_mu); |
| 60 | gpr_asprintf(&final_name, "%s:socket=0x%p", name, r); |
| 61 | grpc_iomgr_register_object(&r->iomgr_object, final_name); |
| 62 | gpr_free(final_name); |
| 63 | grpc_iocp_add_socket(r); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 64 | return r; |
| 65 | } |
| 66 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 67 | /* Schedule a shutdown of the socket operations. Will call the pending |
| 68 | operations to abort them. We need to do that this way because of the |
| 69 | various callsites of that function, which happens to be in various |
| 70 | mutex hold states, and that'd be unsafe to call them directly. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 71 | void grpc_winsocket_shutdown(grpc_winsocket *winsocket) { |
Nicolas Noble | 77ccdb9 | 2015-09-03 17:35:26 -0700 | [diff] [blame] | 72 | /* Grab the function pointer for DisconnectEx for that specific socket. |
| 73 | It may change depending on the interface. */ |
| 74 | int status; |
| 75 | GUID guid = WSAID_DISCONNECTEX; |
| 76 | LPFN_DISCONNECTEX DisconnectEx; |
| 77 | DWORD ioctl_num_bytes; |
| 78 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 79 | status = WSAIoctl(winsocket->socket, SIO_GET_EXTENSION_FUNCTION_POINTER, |
| 80 | &guid, sizeof(guid), &DisconnectEx, sizeof(DisconnectEx), |
| 81 | &ioctl_num_bytes, NULL, NULL); |
Nicolas Noble | 77ccdb9 | 2015-09-03 17:35:26 -0700 | [diff] [blame] | 82 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 83 | if (status == 0) { |
| 84 | DisconnectEx(winsocket->socket, NULL, 0, 0); |
| 85 | } else { |
| 86 | char *utf8_message = gpr_format_message(WSAGetLastError()); |
| 87 | gpr_log(GPR_ERROR, "Unable to retrieve DisconnectEx pointer : %s", |
| 88 | utf8_message); |
| 89 | gpr_free(utf8_message); |
| 90 | } |
| 91 | closesocket(winsocket->socket); |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Craig Tiller | 5352ff1 | 2016-05-18 10:44:38 -0700 | [diff] [blame] | 94 | static void destroy(grpc_winsocket *winsocket) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | grpc_iomgr_unregister_object(&winsocket->iomgr_object); |
| 96 | gpr_mu_destroy(&winsocket->state_mu); |
| 97 | gpr_free(winsocket); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Craig Tiller | 5352ff1 | 2016-05-18 10:44:38 -0700 | [diff] [blame] | 100 | static bool check_destroyable(grpc_winsocket *winsocket) { |
Craig Tiller | 95b9064 | 2016-05-18 12:12:44 -0700 | [diff] [blame^] | 101 | return winsocket->destroy_called == true && |
| 102 | winsocket->write_info.closure == NULL && |
| 103 | winsocket->read_info.closure == NULL; |
Craig Tiller | 5352ff1 | 2016-05-18 10:44:38 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void grpc_winsocket_destroy(grpc_winsocket *winsocket) { |
| 107 | gpr_mu_lock(&winsocket->state_mu); |
| 108 | GPR_ASSERT(!winsocket->destroy_called); |
| 109 | winsocket->destroy_called = true; |
| 110 | bool should_destroy = check_destroyable(winsocket); |
| 111 | gpr_mu_unlock(&winsocket->state_mu); |
| 112 | if (should_destroy) destroy(winsocket); |
| 113 | } |
| 114 | |
| 115 | /* Calling notify_on_read or write means either of two things: |
| 116 | -) The IOCP already completed in the background, and we need to call |
| 117 | the callback now. |
| 118 | -) The IOCP hasn't completed yet, and we're queuing it for later. */ |
| 119 | static void socket_notify_on_iocp(grpc_exec_ctx *exec_ctx, |
| 120 | grpc_winsocket *socket, grpc_closure *closure, |
| 121 | grpc_winsocket_callback_info *info) { |
| 122 | GPR_ASSERT(info->closure == NULL); |
| 123 | gpr_mu_lock(&socket->state_mu); |
| 124 | if (info->has_pending_iocp) { |
| 125 | info->has_pending_iocp = 0; |
| 126 | grpc_exec_ctx_push(exec_ctx, closure, GRPC_ERROR_NONE, NULL); |
| 127 | } else { |
| 128 | info->closure = closure; |
| 129 | } |
| 130 | gpr_mu_unlock(&socket->state_mu); |
| 131 | } |
| 132 | |
| 133 | void grpc_socket_notify_on_write(grpc_exec_ctx *exec_ctx, |
| 134 | grpc_winsocket *socket, |
| 135 | grpc_closure *closure) { |
| 136 | socket_notify_on_iocp(exec_ctx, socket, closure, &socket->write_info); |
| 137 | } |
| 138 | |
| 139 | void grpc_socket_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_winsocket *socket, |
| 140 | grpc_closure *closure) { |
| 141 | socket_notify_on_iocp(exec_ctx, socket, closure, &socket->read_info); |
| 142 | } |
| 143 | |
Craig Tiller | 95b9064 | 2016-05-18 12:12:44 -0700 | [diff] [blame^] | 144 | void grpc_socket_become_ready(grpc_exec_ctx *exec_ctx, grpc_winsocket *socket, |
| 145 | grpc_winsocket_callback_info *info) { |
Craig Tiller | 5352ff1 | 2016-05-18 10:44:38 -0700 | [diff] [blame] | 146 | GPR_ASSERT(!info->has_pending_iocp); |
| 147 | gpr_mu_lock(&socket->state_mu); |
| 148 | if (info->closure) { |
| 149 | grpc_exec_ctx_push(exec_ctx, info->closure, GRPC_ERROR_NONE, NULL); |
| 150 | info->closure = NULL; |
| 151 | } else { |
| 152 | info->has_pending_iocp = 1; |
| 153 | } |
| 154 | bool should_destroy = check_destroyable(socket); |
| 155 | gpr_mu_unlock(&socket->state_mu); |
| 156 | if (should_destroy) destroy(socket); |
| 157 | } |
| 158 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 159 | #endif /* GPR_WINSOCK_SOCKET */ |