Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * |
Nicolas "Pixel" Noble | 929523a | 2016-01-28 09:55:14 +0100 | [diff] [blame] | 3 | * Copyright 2015-2016, 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> |
| 35 | |
| 36 | #ifdef GPR_WINSOCK_SOCKET |
| 37 | |
| 38 | #include "src/core/iomgr/sockaddr_win32.h" |
| 39 | |
| 40 | #include <grpc/support/alloc.h> |
| 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/log_win32.h> |
| 43 | #include <grpc/support/slice_buffer.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 44 | #include <grpc/support/string_util.h> |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 45 | #include <grpc/support/useful.h> |
| 46 | |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 47 | #include "src/core/iomgr/iocp_windows.h" |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 48 | #include "src/core/iomgr/sockaddr.h" |
| 49 | #include "src/core/iomgr/sockaddr_utils.h" |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 50 | #include "src/core/iomgr/socket_windows.h" |
| 51 | #include "src/core/iomgr/tcp_client.h" |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 52 | #include "src/core/iomgr/timer.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 | static int set_non_block(SOCKET sock) { |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 55 | int status; |
| 56 | unsigned long param = 1; |
| 57 | DWORD ret; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 58 | status = |
| 59 | WSAIoctl(sock, FIONBIO, ¶m, sizeof(param), NULL, 0, &ret, NULL, NULL); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 60 | return status == 0; |
| 61 | } |
| 62 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 63 | static int set_dualstack(SOCKET sock) { |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 64 | int status; |
| 65 | unsigned long param = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 66 | status = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)¶m, |
| 67 | sizeof(param)); |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 68 | return status == 0; |
| 69 | } |
| 70 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 71 | int grpc_tcp_prepare_socket(SOCKET sock) { |
| 72 | if (!set_non_block(sock)) return 0; |
| 73 | if (!set_dualstack(sock)) return 0; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 74 | return 1; |
| 75 | } |
| 76 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | typedef struct grpc_tcp { |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 78 | /* This is our C++ class derivation emulation. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 79 | grpc_endpoint base; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 80 | /* The one socket this endpoint is using. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 81 | grpc_winsocket *socket; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 82 | /* Refcounting how many operations are in progress. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 83 | gpr_refcount refcount; |
| 84 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 85 | grpc_closure on_read; |
| 86 | grpc_closure on_write; |
| 87 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 88 | grpc_closure *read_cb; |
| 89 | grpc_closure *write_cb; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 90 | gpr_slice read_slice; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 91 | gpr_slice_buffer *write_slices; |
| 92 | gpr_slice_buffer *read_slices; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 93 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 94 | /* The IO Completion Port runs from another thread. We need some mechanism |
| 95 | to protect ourselves when requesting a shutdown. */ |
| 96 | gpr_mu mu; |
| 97 | int shutting_down; |
Craig Tiller | 81bcc4c | 2015-07-20 14:04:18 -0700 | [diff] [blame] | 98 | |
| 99 | char *peer_string; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 100 | } grpc_tcp; |
| 101 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 102 | static void tcp_free(grpc_tcp *tcp) { |
| 103 | grpc_winsocket_destroy(tcp->socket); |
| 104 | gpr_mu_destroy(&tcp->mu); |
| 105 | gpr_free(tcp->peer_string); |
| 106 | gpr_free(tcp); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 107 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 108 | |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 109 | /*#define GRPC_TCP_REFCOUNT_DEBUG*/ |
| 110 | #ifdef GRPC_TCP_REFCOUNT_DEBUG |
| 111 | #define TCP_UNREF(tcp, reason) tcp_unref((tcp), (reason), __FILE__, __LINE__) |
| 112 | #define TCP_REF(tcp, reason) tcp_ref((tcp), (reason), __FILE__, __LINE__) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 113 | static void tcp_unref(grpc_tcp *tcp, const char *reason, const char *file, |
| 114 | int line) { |
| 115 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP unref %p : %s %d -> %d", tcp, |
| 116 | reason, tcp->refcount.count, tcp->refcount.count - 1); |
| 117 | if (gpr_unref(&tcp->refcount)) { |
| 118 | tcp_free(tcp); |
| 119 | } |
Craig Tiller | 0882a35 | 2015-08-26 13:21:14 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 122 | static void tcp_ref(grpc_tcp *tcp, const char *reason, const char *file, |
| 123 | int line) { |
| 124 | gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, "TCP ref %p : %s %d -> %d", tcp, |
| 125 | reason, tcp->refcount.count, tcp->refcount.count + 1); |
| 126 | gpr_ref(&tcp->refcount); |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 127 | } |
| 128 | #else |
| 129 | #define TCP_UNREF(tcp, reason) tcp_unref((tcp)) |
| 130 | #define TCP_REF(tcp, reason) tcp_ref((tcp)) |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 131 | static void tcp_unref(grpc_tcp *tcp) { |
| 132 | if (gpr_unref(&tcp->refcount)) { |
| 133 | tcp_free(tcp); |
| 134 | } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | static void tcp_ref(grpc_tcp *tcp) { gpr_ref(&tcp->refcount); } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 138 | #endif |
| 139 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 140 | /* Asynchronous callback from the IOCP, or the background thread. */ |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 141 | static void on_read(grpc_exec_ctx *exec_ctx, void *tcpp, bool success) { |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 142 | grpc_tcp *tcp = tcpp; |
| 143 | grpc_closure *cb = tcp->read_cb; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 144 | grpc_winsocket *socket = tcp->socket; |
| 145 | gpr_slice sub; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 146 | grpc_winsocket_callback_info *info = &socket->read_info; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 147 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 148 | if (success) { |
Jan Tattermusch | 6412479 | 2016-03-25 09:16:22 -0700 | [diff] [blame] | 149 | if (info->wsa_error != 0 && !tcp->shutting_down) { |
| 150 | if (info->wsa_error != WSAECONNRESET) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 151 | char *utf8_message = gpr_format_message(info->wsa_error); |
| 152 | gpr_log(GPR_ERROR, "ReadFile overlapped error: %s", utf8_message); |
| 153 | gpr_free(utf8_message); |
| 154 | } |
| 155 | success = 0; |
| 156 | gpr_slice_unref(tcp->read_slice); |
| 157 | } else { |
Nicolas "Pixel" Noble | 3a26c5b | 2015-09-22 00:07:14 +0200 | [diff] [blame] | 158 | if (info->bytes_transfered != 0 && !tcp->shutting_down) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | sub = gpr_slice_sub_no_ref(tcp->read_slice, 0, info->bytes_transfered); |
| 160 | gpr_slice_buffer_add(tcp->read_slices, sub); |
| 161 | success = 1; |
| 162 | } else { |
| 163 | gpr_slice_unref(tcp->read_slice); |
| 164 | success = 0; |
| 165 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 166 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 167 | } |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 168 | |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 169 | tcp->read_cb = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 170 | TCP_UNREF(tcp, "read"); |
| 171 | if (cb) { |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 172 | cb->cb(exec_ctx, cb->cb_arg, success); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 173 | } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 176 | static void win_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 177 | gpr_slice_buffer *read_slices, grpc_closure *cb) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 178 | grpc_tcp *tcp = (grpc_tcp *)ep; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 179 | grpc_winsocket *handle = tcp->socket; |
| 180 | grpc_winsocket_callback_info *info = &handle->read_info; |
| 181 | int status; |
| 182 | DWORD bytes_read = 0; |
| 183 | DWORD flags = 0; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 184 | WSABUF buffer; |
| 185 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | if (tcp->shutting_down) { |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 187 | grpc_exec_ctx_enqueue(exec_ctx, cb, false, NULL); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 188 | return; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | } |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 190 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 191 | tcp->read_cb = cb; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 192 | tcp->read_slices = read_slices; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 193 | gpr_slice_buffer_reset_and_unref(read_slices); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 194 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 195 | tcp->read_slice = gpr_slice_malloc(8192); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 196 | |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 197 | buffer.len = (ULONG)GPR_SLICE_LENGTH( |
| 198 | tcp->read_slice); // we know slice size fits in 32bit. |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 199 | buffer.buf = (char *)GPR_SLICE_START_PTR(tcp->read_slice); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 200 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 201 | TCP_REF(tcp, "read"); |
| 202 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 203 | /* First let's try a synchronous, non-blocking read. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 204 | status = |
| 205 | WSARecv(tcp->socket->socket, &buffer, 1, &bytes_read, &flags, NULL, NULL); |
| 206 | info->wsa_error = status == 0 ? 0 : WSAGetLastError(); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 207 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 208 | /* Did we get data immediately ? Yay. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 209 | if (info->wsa_error != WSAEWOULDBLOCK) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 210 | info->bytes_transfered = bytes_read; |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 211 | grpc_exec_ctx_enqueue(exec_ctx, &tcp->on_read, true, NULL); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 212 | return; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 213 | } |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 214 | |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 215 | /* Otherwise, let's retry, by queuing a read. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 216 | memset(&tcp->socket->read_info.overlapped, 0, sizeof(OVERLAPPED)); |
| 217 | status = WSARecv(tcp->socket->socket, &buffer, 1, &bytes_read, &flags, |
| 218 | &info->overlapped, NULL); |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 219 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 220 | if (status != 0) { |
| 221 | int wsa_error = WSAGetLastError(); |
| 222 | if (wsa_error != WSA_IO_PENDING) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | info->wsa_error = wsa_error; |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 224 | grpc_exec_ctx_enqueue(exec_ctx, &tcp->on_read, false, NULL); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 225 | return; |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 226 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 227 | } |
Craig Tiller | 45724b3 | 2015-09-22 10:42:19 -0700 | [diff] [blame] | 228 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 229 | grpc_socket_notify_on_read(exec_ctx, tcp->socket, &tcp->on_read); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 232 | /* Asynchronous callback from the IOCP, or the background thread. */ |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 233 | static void on_write(grpc_exec_ctx *exec_ctx, void *tcpp, bool success) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 234 | grpc_tcp *tcp = (grpc_tcp *)tcpp; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 235 | grpc_winsocket *handle = tcp->socket; |
| 236 | grpc_winsocket_callback_info *info = &handle->write_info; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 237 | grpc_closure *cb; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 238 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 239 | gpr_mu_lock(&tcp->mu); |
Nicolas Noble | e144536 | 2015-05-11 17:40:26 -0700 | [diff] [blame] | 240 | cb = tcp->write_cb; |
| 241 | tcp->write_cb = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 242 | gpr_mu_unlock(&tcp->mu); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 243 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 244 | if (success) { |
| 245 | if (info->wsa_error != 0) { |
| 246 | if (info->wsa_error != WSAECONNRESET) { |
| 247 | char *utf8_message = gpr_format_message(info->wsa_error); |
| 248 | gpr_log(GPR_ERROR, "WSASend overlapped error: %s", utf8_message); |
| 249 | gpr_free(utf8_message); |
| 250 | } |
| 251 | success = 0; |
| 252 | } else { |
| 253 | GPR_ASSERT(info->bytes_transfered == tcp->write_slices->length); |
Nicolas Noble | e144536 | 2015-05-11 17:40:26 -0700 | [diff] [blame] | 254 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 255 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 256 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 257 | TCP_UNREF(tcp, "write"); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 258 | cb->cb(exec_ctx, cb->cb_arg, success); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 259 | } |
| 260 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 261 | /* Initiates a write. */ |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 262 | static void win_write(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 263 | gpr_slice_buffer *slices, grpc_closure *cb) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 264 | grpc_tcp *tcp = (grpc_tcp *)ep; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 265 | grpc_winsocket *socket = tcp->socket; |
| 266 | grpc_winsocket_callback_info *info = &socket->write_info; |
| 267 | unsigned i; |
| 268 | DWORD bytes_sent; |
| 269 | int status; |
| 270 | WSABUF local_buffers[16]; |
| 271 | WSABUF *allocated = NULL; |
| 272 | WSABUF *buffers = local_buffers; |
Jan Tattermusch | f67f071 | 2015-12-07 18:09:49 -0800 | [diff] [blame] | 273 | size_t len; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 274 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 275 | if (tcp->shutting_down) { |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 276 | grpc_exec_ctx_enqueue(exec_ctx, cb, false, NULL); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 277 | return; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 278 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 279 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 280 | tcp->write_cb = cb; |
Craig Tiller | b029859 | 2015-08-27 07:38:01 -0700 | [diff] [blame] | 281 | tcp->write_slices = slices; |
Jan Tattermusch | f67f071 | 2015-12-07 18:09:49 -0800 | [diff] [blame] | 282 | GPR_ASSERT(tcp->write_slices->count <= UINT_MAX); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 283 | if (tcp->write_slices->count > GPR_ARRAY_SIZE(local_buffers)) { |
| 284 | buffers = (WSABUF *)gpr_malloc(sizeof(WSABUF) * tcp->write_slices->count); |
| 285 | allocated = buffers; |
| 286 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 287 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 288 | for (i = 0; i < tcp->write_slices->count; i++) { |
Jan Tattermusch | f67f071 | 2015-12-07 18:09:49 -0800 | [diff] [blame] | 289 | len = GPR_SLICE_LENGTH(tcp->write_slices->slices[i]); |
| 290 | GPR_ASSERT(len <= ULONG_MAX); |
Craig Tiller | 620e965 | 2015-12-14 12:02:50 -0800 | [diff] [blame] | 291 | buffers[i].len = (ULONG)len; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 292 | buffers[i].buf = (char *)GPR_SLICE_START_PTR(tcp->write_slices->slices[i]); |
| 293 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 294 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 295 | /* First, let's try a synchronous, non-blocking write. */ |
Jan Tattermusch | 4b3ecdf | 2015-12-04 09:33:05 -0800 | [diff] [blame] | 296 | status = WSASend(socket->socket, buffers, (DWORD)tcp->write_slices->count, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 297 | &bytes_sent, 0, NULL, NULL); |
| 298 | info->wsa_error = status == 0 ? 0 : WSAGetLastError(); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 299 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 300 | /* We would kind of expect to get a WSAEWOULDBLOCK here, especially on a busy |
| 301 | connection that has its send queue filled up. But if we don't, then we can |
| 302 | avoid doing an async write operation at all. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 303 | if (info->wsa_error != WSAEWOULDBLOCK) { |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 304 | bool ok = false; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 305 | if (status == 0) { |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 306 | ok = true; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 307 | GPR_ASSERT(bytes_sent == tcp->write_slices->length); |
| 308 | } else { |
Jan Tattermusch | 6412479 | 2016-03-25 09:16:22 -0700 | [diff] [blame] | 309 | if (info->wsa_error != WSAECONNRESET) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 310 | char *utf8_message = gpr_format_message(info->wsa_error); |
| 311 | gpr_log(GPR_ERROR, "WSASend error: %s", utf8_message); |
| 312 | gpr_free(utf8_message); |
| 313 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 314 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 315 | if (allocated) gpr_free(allocated); |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 316 | grpc_exec_ctx_enqueue(exec_ctx, cb, ok, NULL); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 317 | return; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 318 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 319 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 320 | TCP_REF(tcp, "write"); |
Craig Tiller | 9f80fcf | 2015-08-28 08:22:48 -0700 | [diff] [blame] | 321 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 322 | /* If we got a WSAEWOULDBLOCK earlier, then we need to re-do the same |
| 323 | operation, this time asynchronously. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 324 | memset(&socket->write_info.overlapped, 0, sizeof(OVERLAPPED)); |
Jan Tattermusch | 4b3ecdf | 2015-12-04 09:33:05 -0800 | [diff] [blame] | 325 | status = WSASend(socket->socket, buffers, (DWORD)tcp->write_slices->count, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 326 | &bytes_sent, 0, &socket->write_info.overlapped, NULL); |
| 327 | if (allocated) gpr_free(allocated); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 328 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 329 | if (status != 0) { |
| 330 | int wsa_error = WSAGetLastError(); |
| 331 | if (wsa_error != WSA_IO_PENDING) { |
| 332 | TCP_UNREF(tcp, "write"); |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 333 | grpc_exec_ctx_enqueue(exec_ctx, cb, false, NULL); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 334 | return; |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 335 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 336 | } |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 337 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 338 | /* As all is now setup, we can now ask for the IOCP notification. It may |
| 339 | trigger the callback immediately however, but no matter. */ |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 340 | grpc_socket_notify_on_write(exec_ctx, socket, &tcp->on_write); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 341 | } |
| 342 | |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 343 | static void win_add_to_pollset(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 344 | grpc_pollset *ps) { |
Jan Tattermusch | deab29e | 2015-08-03 12:32:44 -0700 | [diff] [blame] | 345 | grpc_tcp *tcp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 346 | (void)ps; |
| 347 | tcp = (grpc_tcp *)ep; |
| 348 | grpc_iocp_add_socket(tcp->socket); |
Nicolas "Pixel" Noble | f29e9db | 2015-07-29 06:00:22 +0200 | [diff] [blame] | 349 | } |
| 350 | |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 351 | static void win_add_to_pollset_set(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep, |
| 352 | grpc_pollset_set *pss) { |
Jan Tattermusch | deab29e | 2015-08-03 12:32:44 -0700 | [diff] [blame] | 353 | grpc_tcp *tcp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 354 | (void)pss; |
| 355 | tcp = (grpc_tcp *)ep; |
| 356 | grpc_iocp_add_socket(tcp->socket); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 359 | /* Initiates a shutdown of the TCP endpoint. This will queue abort callbacks |
| 360 | for the potential read and write operations. It is up to the caller to |
| 361 | guarantee this isn't called in parallel to a read or write request, so |
| 362 | we're not going to protect against these. However the IO Completion Port |
| 363 | callback will happen from another thread, so we need to protect against |
| 364 | concurrent access of the data structure in that regard. */ |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 365 | static void win_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 366 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 367 | gpr_mu_lock(&tcp->mu); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 368 | /* At that point, what may happen is that we're already inside the IOCP |
| 369 | callback. See the comments in on_read and on_write. */ |
| 370 | tcp->shutting_down = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 371 | grpc_winsocket_shutdown(tcp->socket); |
| 372 | gpr_mu_unlock(&tcp->mu); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 373 | } |
| 374 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 375 | static void win_destroy(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 376 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 377 | TCP_UNREF(tcp, "destroy"); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 380 | static char *win_get_peer(grpc_endpoint *ep) { |
| 381 | grpc_tcp *tcp = (grpc_tcp *)ep; |
| 382 | return gpr_strdup(tcp->peer_string); |
Craig Tiller | 81bcc4c | 2015-07-20 14:04:18 -0700 | [diff] [blame] | 383 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 384 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 385 | static grpc_endpoint_vtable vtable = { |
| 386 | win_read, win_write, win_add_to_pollset, win_add_to_pollset_set, |
| 387 | win_shutdown, win_destroy, win_get_peer}; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 388 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 389 | grpc_endpoint *grpc_tcp_create(grpc_winsocket *socket, char *peer_string) { |
| 390 | grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp)); |
| 391 | memset(tcp, 0, sizeof(grpc_tcp)); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 392 | tcp->base.vtable = &vtable; |
| 393 | tcp->socket = socket; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 394 | gpr_mu_init(&tcp->mu); |
| 395 | gpr_ref_init(&tcp->refcount, 1); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 396 | grpc_closure_init(&tcp->on_read, on_read, tcp); |
Craig Tiller | 258f8de | 2015-09-23 12:32:40 -0700 | [diff] [blame] | 397 | grpc_closure_init(&tcp->on_write, on_write, tcp); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 398 | tcp->peer_string = gpr_strdup(peer_string); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 399 | return &tcp->base; |
| 400 | } |
| 401 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 402 | #endif /* GPR_WINSOCK_SOCKET */ |