Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [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> |
| 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> |
| 44 | #include <grpc/support/useful.h> |
| 45 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 46 | #include "src/core/iomgr/alarm.h" |
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" |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 52 | |
| 53 | static int set_non_block(SOCKET sock) { |
| 54 | int status; |
| 55 | unsigned long param = 1; |
| 56 | DWORD ret; |
| 57 | status = WSAIoctl(sock, FIONBIO, ¶m, sizeof(param), NULL, 0, &ret, |
| 58 | NULL, NULL); |
| 59 | return status == 0; |
| 60 | } |
| 61 | |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 62 | static int set_dualstack(SOCKET sock) { |
| 63 | int status; |
| 64 | unsigned long param = 0; |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 65 | status = setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, |
| 66 | (const char *) ¶m, sizeof(param)); |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 67 | return status == 0; |
| 68 | } |
| 69 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 70 | int grpc_tcp_prepare_socket(SOCKET sock) { |
| 71 | if (!set_non_block(sock)) |
| 72 | return 0; |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 73 | if (!set_dualstack(sock)) |
| 74 | return 0; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | typedef struct grpc_tcp { |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 79 | /* This is our C++ class derivation emulation. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 80 | grpc_endpoint base; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 81 | /* The one socket this endpoint is using. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 82 | grpc_winsocket *socket; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 83 | /* Refcounting how many operations are in progress. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 84 | gpr_refcount refcount; |
| 85 | |
| 86 | grpc_endpoint_read_cb read_cb; |
| 87 | void *read_user_data; |
| 88 | gpr_slice read_slice; |
| 89 | int outstanding_read; |
| 90 | |
| 91 | grpc_endpoint_write_cb write_cb; |
| 92 | void *write_user_data; |
| 93 | gpr_slice_buffer write_slices; |
| 94 | int outstanding_write; |
| 95 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 96 | /* The IO Completion Port runs from another thread. We need some mechanism |
| 97 | to protect ourselves when requesting a shutdown. */ |
| 98 | gpr_mu mu; |
| 99 | int shutting_down; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 100 | } grpc_tcp; |
| 101 | |
| 102 | static void tcp_ref(grpc_tcp *tcp) { |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 103 | gpr_ref(&tcp->refcount); |
| 104 | } |
| 105 | |
| 106 | static void tcp_unref(grpc_tcp *tcp) { |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 107 | if (gpr_unref(&tcp->refcount)) { |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 108 | gpr_slice_buffer_destroy(&tcp->write_slices); |
| 109 | grpc_winsocket_orphan(tcp->socket); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 110 | gpr_mu_destroy(&tcp->mu); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 111 | gpr_free(tcp); |
| 112 | } |
| 113 | } |
| 114 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 115 | /* Asynchronous callback from the IOCP, or the background thread. */ |
| 116 | static void on_read(void *tcpp, int from_iocp) { |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 117 | grpc_tcp *tcp = (grpc_tcp *) tcpp; |
| 118 | grpc_winsocket *socket = tcp->socket; |
| 119 | gpr_slice sub; |
| 120 | gpr_slice *slice = NULL; |
| 121 | size_t nslices = 0; |
| 122 | grpc_endpoint_cb_status status; |
| 123 | grpc_endpoint_read_cb cb = tcp->read_cb; |
| 124 | grpc_winsocket_callback_info *info = &socket->read_info; |
| 125 | void *opaque = tcp->read_user_data; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 126 | int do_abort = 0; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 127 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 128 | gpr_mu_lock(&tcp->mu); |
| 129 | if (!from_iocp || tcp->shutting_down) { |
| 130 | /* If we are here with from_iocp set to true, it means we got raced to |
| 131 | shutting down the endpoint. No actual abort callback will happen |
| 132 | though, so we're going to do it from here. */ |
| 133 | do_abort = 1; |
| 134 | } |
| 135 | gpr_mu_unlock(&tcp->mu); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 136 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 137 | if (do_abort) { |
| 138 | if (from_iocp) gpr_slice_unref(tcp->read_slice); |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 139 | tcp_unref(tcp); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 140 | cb(opaque, NULL, 0, GRPC_ENDPOINT_CB_SHUTDOWN); |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 141 | return; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 144 | GPR_ASSERT(tcp->outstanding_read); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 145 | |
| 146 | if (socket->read_info.wsa_error != 0) { |
| 147 | char *utf8_message = gpr_format_message(info->wsa_error); |
Jan Tattermusch | 638c1ee | 2015-04-21 17:14:44 -0700 | [diff] [blame] | 148 | gpr_log(GPR_ERROR, "ReadFile overlapped error: %s", utf8_message); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 149 | gpr_free(utf8_message); |
| 150 | status = GRPC_ENDPOINT_CB_ERROR; |
Nicolas "Pixel" Noble | 404fc6a | 2015-05-02 02:34:39 -0700 | [diff] [blame] | 151 | socket->closed_early = 1; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 152 | } else { |
| 153 | if (info->bytes_transfered != 0) { |
| 154 | sub = gpr_slice_sub(tcp->read_slice, 0, info->bytes_transfered); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 155 | status = GRPC_ENDPOINT_CB_OK; |
| 156 | slice = ⊂ |
| 157 | nslices = 1; |
| 158 | } else { |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 159 | gpr_slice_unref(tcp->read_slice); |
| 160 | status = GRPC_ENDPOINT_CB_EOF; |
| 161 | } |
| 162 | } |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 163 | |
| 164 | tcp->outstanding_read = 0; |
| 165 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 166 | tcp_unref(tcp); |
| 167 | cb(opaque, slice, nslices, status); |
| 168 | } |
| 169 | |
| 170 | static void win_notify_on_read(grpc_endpoint *ep, |
| 171 | grpc_endpoint_read_cb cb, void *arg) { |
| 172 | grpc_tcp *tcp = (grpc_tcp *) ep; |
| 173 | grpc_winsocket *handle = tcp->socket; |
| 174 | grpc_winsocket_callback_info *info = &handle->read_info; |
| 175 | int status; |
| 176 | DWORD bytes_read = 0; |
| 177 | DWORD flags = 0; |
| 178 | int error; |
| 179 | WSABUF buffer; |
| 180 | |
| 181 | GPR_ASSERT(!tcp->outstanding_read); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 182 | GPR_ASSERT(!tcp->shutting_down); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 183 | tcp_ref(tcp); |
| 184 | tcp->outstanding_read = 1; |
| 185 | tcp->read_cb = cb; |
| 186 | tcp->read_user_data = arg; |
| 187 | |
| 188 | tcp->read_slice = gpr_slice_malloc(8192); |
| 189 | |
| 190 | buffer.len = GPR_SLICE_LENGTH(tcp->read_slice); |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 191 | buffer.buf = (char *)GPR_SLICE_START_PTR(tcp->read_slice); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 192 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 193 | /* First let's try a synchronous, non-blocking read. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 194 | status = WSARecv(tcp->socket->socket, &buffer, 1, &bytes_read, &flags, |
| 195 | NULL, NULL); |
| 196 | info->wsa_error = status == 0 ? 0 : WSAGetLastError(); |
| 197 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 198 | /* Did we get data immediately ? Yay. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 199 | if (info->wsa_error != WSAEWOULDBLOCK) { |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 200 | info->bytes_transfered = bytes_read; |
| 201 | /* This might heavily recurse. */ |
| 202 | on_read(tcp, 1); |
| 203 | return; |
| 204 | } |
| 205 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 206 | /* Otherwise, let's retry, by queuing a read. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 207 | memset(&tcp->socket->read_info.overlapped, 0, sizeof(OVERLAPPED)); |
| 208 | status = WSARecv(tcp->socket->socket, &buffer, 1, &bytes_read, &flags, |
| 209 | &info->overlapped, NULL); |
| 210 | |
| 211 | if (status == 0) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 212 | grpc_socket_notify_on_read(tcp->socket, on_read, tcp); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | |
| 216 | error = WSAGetLastError(); |
| 217 | |
| 218 | if (error != WSA_IO_PENDING) { |
| 219 | char *utf8_message = gpr_format_message(WSAGetLastError()); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 220 | gpr_log(GPR_ERROR, "WSARecv error: %s - this means we're going to leak.", |
| 221 | utf8_message); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 222 | gpr_free(utf8_message); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 223 | /* I'm pretty sure this is a very bad situation there. Hence the log. |
| 224 | What will happen now is that the socket will neither wait for read |
| 225 | or write, unless the caller retry, which is unlikely, but I am not |
| 226 | sure if that's guaranteed. And there might also be a write pending. |
| 227 | This means that the future orphanage of that socket will be in limbo, |
| 228 | and we're going to leak it. I have no idea what could cause this |
| 229 | specific case however, aside from a parameter error from our call. |
| 230 | Normal read errors would actually happen during the overlapped |
| 231 | operation, which is the supported way to go for that. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 232 | tcp->outstanding_read = 0; |
| 233 | tcp_unref(tcp); |
| 234 | cb(arg, NULL, 0, GRPC_ENDPOINT_CB_ERROR); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 235 | /* Per the comment above, I'm going to treat that case as a hard failure |
| 236 | for now, and leave the option to catch that and debug. */ |
| 237 | __debugbreak(); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 238 | return; |
| 239 | } |
| 240 | |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 241 | grpc_socket_notify_on_read(tcp->socket, on_read, tcp); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 242 | } |
| 243 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 244 | /* Asynchronous callback from the IOCP, or the background thread. */ |
| 245 | static void on_write(void *tcpp, int from_iocp) { |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 246 | grpc_tcp *tcp = (grpc_tcp *) tcpp; |
| 247 | grpc_winsocket *handle = tcp->socket; |
| 248 | grpc_winsocket_callback_info *info = &handle->write_info; |
| 249 | grpc_endpoint_cb_status status = GRPC_ENDPOINT_CB_OK; |
| 250 | grpc_endpoint_write_cb cb = tcp->write_cb; |
| 251 | void *opaque = tcp->write_user_data; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 252 | int do_abort = 0; |
| 253 | |
| 254 | gpr_mu_lock(&tcp->mu); |
| 255 | if (!from_iocp || tcp->shutting_down) { |
| 256 | /* If we are here with from_iocp set to true, it means we got raced to |
| 257 | shutting down the endpoint. No actual abort callback will happen |
| 258 | though, so we're going to do it from here. */ |
| 259 | do_abort = 1; |
| 260 | } |
| 261 | gpr_mu_unlock(&tcp->mu); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 262 | |
| 263 | GPR_ASSERT(tcp->outstanding_write); |
| 264 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 265 | if (do_abort) { |
| 266 | if (from_iocp) gpr_slice_buffer_reset_and_unref(&tcp->write_slices); |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 267 | tcp_unref(tcp); |
Nicolas "Pixel" Noble | ba410fa | 2015-02-05 00:54:15 +0100 | [diff] [blame] | 268 | cb(opaque, GRPC_ENDPOINT_CB_SHUTDOWN); |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 269 | return; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (info->wsa_error != 0) { |
| 273 | char *utf8_message = gpr_format_message(info->wsa_error); |
| 274 | gpr_log(GPR_ERROR, "WSASend overlapped error: %s", utf8_message); |
| 275 | gpr_free(utf8_message); |
| 276 | status = GRPC_ENDPOINT_CB_ERROR; |
Nicolas "Pixel" Noble | 404fc6a | 2015-05-02 02:34:39 -0700 | [diff] [blame] | 277 | tcp->socket->closed_early = 1; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 278 | } else { |
| 279 | GPR_ASSERT(info->bytes_transfered == tcp->write_slices.length); |
| 280 | } |
| 281 | |
| 282 | gpr_slice_buffer_reset_and_unref(&tcp->write_slices); |
| 283 | tcp->outstanding_write = 0; |
| 284 | |
| 285 | tcp_unref(tcp); |
| 286 | cb(opaque, status); |
| 287 | } |
| 288 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 289 | /* Initiates a write. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 290 | static grpc_endpoint_write_status win_write(grpc_endpoint *ep, |
| 291 | gpr_slice *slices, size_t nslices, |
| 292 | grpc_endpoint_write_cb cb, |
| 293 | void *arg) { |
| 294 | grpc_tcp *tcp = (grpc_tcp *) ep; |
| 295 | grpc_winsocket *socket = tcp->socket; |
| 296 | grpc_winsocket_callback_info *info = &socket->write_info; |
| 297 | unsigned i; |
| 298 | DWORD bytes_sent; |
| 299 | int status; |
| 300 | WSABUF local_buffers[16]; |
| 301 | WSABUF *allocated = NULL; |
| 302 | WSABUF *buffers = local_buffers; |
| 303 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 304 | GPR_ASSERT(!tcp->outstanding_write); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 305 | GPR_ASSERT(!tcp->shutting_down); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 306 | tcp_ref(tcp); |
| 307 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 308 | tcp->outstanding_write = 1; |
| 309 | tcp->write_cb = cb; |
| 310 | tcp->write_user_data = arg; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 311 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 312 | gpr_slice_buffer_addn(&tcp->write_slices, slices, nslices); |
| 313 | |
| 314 | if (tcp->write_slices.count > GPR_ARRAY_SIZE(local_buffers)) { |
| 315 | buffers = (WSABUF *) gpr_malloc(sizeof(WSABUF) * tcp->write_slices.count); |
| 316 | allocated = buffers; |
| 317 | } |
| 318 | |
| 319 | for (i = 0; i < tcp->write_slices.count; i++) { |
| 320 | buffers[i].len = GPR_SLICE_LENGTH(tcp->write_slices.slices[i]); |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 321 | buffers[i].buf = (char *)GPR_SLICE_START_PTR(tcp->write_slices.slices[i]); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 324 | /* First, let's try a synchronous, non-blocking write. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 325 | status = WSASend(socket->socket, buffers, tcp->write_slices.count, |
| 326 | &bytes_sent, 0, NULL, NULL); |
| 327 | info->wsa_error = status == 0 ? 0 : WSAGetLastError(); |
| 328 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 329 | /* We would kind of expect to get a WSAEWOULDBLOCK here, especially on a busy |
| 330 | connection that has its send queue filled up. But if we don't, then we can |
| 331 | avoid doing an async write operation at all. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 332 | if (info->wsa_error != WSAEWOULDBLOCK) { |
| 333 | grpc_endpoint_write_status ret = GRPC_ENDPOINT_WRITE_ERROR; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 334 | if (status == 0) { |
| 335 | ret = GRPC_ENDPOINT_WRITE_DONE; |
| 336 | GPR_ASSERT(bytes_sent == tcp->write_slices.length); |
Nicolas "Pixel" Noble | ee0c96c | 2015-02-04 23:35:41 +0100 | [diff] [blame] | 337 | } else { |
| 338 | char *utf8_message = gpr_format_message(info->wsa_error); |
| 339 | gpr_log(GPR_ERROR, "WSASend error: %s", utf8_message); |
| 340 | gpr_free(utf8_message); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 341 | } |
| 342 | if (allocated) gpr_free(allocated); |
| 343 | gpr_slice_buffer_reset_and_unref(&tcp->write_slices); |
| 344 | tcp->outstanding_write = 0; |
| 345 | tcp_unref(tcp); |
| 346 | return ret; |
| 347 | } |
| 348 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 349 | /* If we got a WSAEWOULDBLOCK earlier, then we need to re-do the same |
| 350 | operation, this time asynchronously. */ |
zeliard | 3874ad0 | 2015-04-30 16:05:45 +0900 | [diff] [blame] | 351 | memset(&socket->write_info.overlapped, 0, sizeof(OVERLAPPED)); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 352 | status = WSASend(socket->socket, buffers, tcp->write_slices.count, |
| 353 | &bytes_sent, 0, &socket->write_info.overlapped, NULL); |
| 354 | if (allocated) gpr_free(allocated); |
| 355 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 356 | /* It is possible the operation completed then. But we'd still get an IOCP |
| 357 | notification. So let's ignore it and wait for the IOCP. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 358 | if (status != 0) { |
| 359 | int error = WSAGetLastError(); |
| 360 | if (error != WSA_IO_PENDING) { |
| 361 | char *utf8_message = gpr_format_message(WSAGetLastError()); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 362 | gpr_log(GPR_ERROR, "WSASend error: %s - this means we're going to leak.", |
| 363 | utf8_message); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 364 | gpr_free(utf8_message); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 365 | /* I'm pretty sure this is a very bad situation there. Hence the log. |
| 366 | What will happen now is that the socket will neither wait for read |
| 367 | or write, unless the caller retry, which is unlikely, but I am not |
| 368 | sure if that's guaranteed. And there might also be a read pending. |
| 369 | This means that the future orphanage of that socket will be in limbo, |
| 370 | and we're going to leak it. I have no idea what could cause this |
| 371 | specific case however, aside from a parameter error from our call. |
| 372 | Normal read errors would actually happen during the overlapped |
| 373 | operation, which is the supported way to go for that. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 374 | tcp->outstanding_write = 0; |
| 375 | tcp_unref(tcp); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 376 | /* Per the comment above, I'm going to treat that case as a hard failure |
| 377 | for now, and leave the option to catch that and debug. */ |
| 378 | __debugbreak(); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 379 | return GRPC_ENDPOINT_WRITE_ERROR; |
| 380 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 381 | } |
| 382 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 383 | /* As all is now setup, we can now ask for the IOCP notification. It may |
| 384 | trigger the callback immediately however, but no matter. */ |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 385 | grpc_socket_notify_on_write(socket, on_write, tcp); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 386 | return GRPC_ENDPOINT_WRITE_PENDING; |
| 387 | } |
| 388 | |
| 389 | static void win_add_to_pollset(grpc_endpoint *ep, grpc_pollset *pollset) { |
| 390 | grpc_tcp *tcp = (grpc_tcp *) ep; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 391 | grpc_iocp_add_socket(tcp->socket); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 394 | /* Initiates a shutdown of the TCP endpoint. This will queue abort callbacks |
| 395 | for the potential read and write operations. It is up to the caller to |
| 396 | guarantee this isn't called in parallel to a read or write request, so |
| 397 | we're not going to protect against these. However the IO Completion Port |
| 398 | callback will happen from another thread, so we need to protect against |
| 399 | concurrent access of the data structure in that regard. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 400 | static void win_shutdown(grpc_endpoint *ep) { |
| 401 | grpc_tcp *tcp = (grpc_tcp *) ep; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 402 | gpr_mu_lock(&tcp->mu); |
| 403 | /* At that point, what may happen is that we're already inside the IOCP |
| 404 | callback. See the comments in on_read and on_write. */ |
| 405 | tcp->shutting_down = 1; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 406 | grpc_winsocket_shutdown(tcp->socket); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 407 | gpr_mu_unlock(&tcp->mu); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | static void win_destroy(grpc_endpoint *ep) { |
| 411 | grpc_tcp *tcp = (grpc_tcp *) ep; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 412 | tcp_unref(tcp); |
| 413 | } |
| 414 | |
| 415 | static grpc_endpoint_vtable vtable = { |
| 416 | win_notify_on_read, win_write, win_add_to_pollset, win_shutdown, win_destroy |
| 417 | }; |
| 418 | |
| 419 | grpc_endpoint *grpc_tcp_create(grpc_winsocket *socket) { |
| 420 | grpc_tcp *tcp = (grpc_tcp *) gpr_malloc(sizeof(grpc_tcp)); |
| 421 | memset(tcp, 0, sizeof(grpc_tcp)); |
| 422 | tcp->base.vtable = &vtable; |
| 423 | tcp->socket = socket; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 424 | gpr_mu_init(&tcp->mu); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 425 | gpr_slice_buffer_init(&tcp->write_slices); |
| 426 | gpr_ref_init(&tcp->refcount, 1); |
| 427 | return &tcp->base; |
| 428 | } |
| 429 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 430 | #endif /* GPR_WINSOCK_SOCKET */ |