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