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 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_IOMGR_SOCKET_WINDOWS_H |
| 35 | #define GRPC_CORE_LIB_IOMGR_SOCKET_WINDOWS_H |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 36 | |
Craig Tiller | ae65f1a | 2015-07-01 11:23:59 -0700 | [diff] [blame] | 37 | #include <grpc/support/port_platform.h> |
Craig Tiller | 684d8f6 | 2015-07-01 11:31:48 -0700 | [diff] [blame] | 38 | #include <winsock2.h> |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 39 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 40 | #include <grpc/support/atm.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 41 | #include <grpc/support/sync.h> |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 42 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 43 | #include "src/core/lib/iomgr/exec_ctx.h" |
| 44 | #include "src/core/lib/iomgr/iomgr_internal.h" |
David Garcia Quintas | 0c47d80 | 2015-05-28 00:33:57 -0700 | [diff] [blame] | 45 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 46 | /* This holds the data for an outstanding read or write on a socket. |
| 47 | The mutex to protect the concurrent access to that data is the one |
| 48 | inside the winsocket wrapper. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 49 | typedef struct grpc_winsocket_callback_info { |
Nicolas "Pixel" Noble | 3d8e34d | 2015-02-10 11:05:48 +0100 | [diff] [blame] | 50 | /* This is supposed to be a WSAOVERLAPPED, but in order to get that |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 51 | definition, we need to include ws2tcpip.h, which needs to be included |
| 52 | from the top, otherwise it'll clash with a previous inclusion of |
| 53 | windows.h that in turns includes winsock.h. If anyone knows a way |
| 54 | to do it properly, feel free to send a patch. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 55 | OVERLAPPED overlapped; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 56 | /* The callback information for the pending operation. May be empty if the |
| 57 | caller hasn't registered a callback yet. */ |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 58 | grpc_closure *closure; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 59 | /* A boolean to describe if the IO Completion Port got a notification for |
| 60 | that operation. This will happen if the operation completed before the |
| 61 | called had time to register a callback. We could avoid that behavior |
| 62 | altogether by forcing the caller to always register its callback before |
| 63 | proceeding queue an operation, but it is frequent for an IO Completion |
| 64 | Port to trigger quickly. This way we avoid a context switch for calling |
| 65 | the callback. We also simplify the read / write operations to avoid having |
| 66 | to hold a mutex for a long amount of time. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 67 | int has_pending_iocp; |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 68 | /* The results of the overlapped operation. */ |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 69 | DWORD bytes_transfered; |
| 70 | int wsa_error; |
| 71 | } grpc_winsocket_callback_info; |
| 72 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 73 | /* This is a wrapper to a Windows socket. A socket can have one outstanding |
| 74 | read, and one outstanding write. Doing an asynchronous accept means waiting |
| 75 | for a read operation. Doing an asynchronous connect means waiting for a |
Nicolas "Pixel" Noble | e34a45a | 2015-05-07 18:41:07 +0200 | [diff] [blame] | 76 | write operation. These are completely arbitrary ties between the operation |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 77 | and the kind of event, because we can have one overlapped per pending |
| 78 | operation, whichever its nature is. So we could have more dedicated pending |
| 79 | operation callbacks for connect and listen. But given the scope of listen |
| 80 | and accept, we don't need to go to that extent and waste memory. Also, this |
| 81 | is closer to what happens in posix world. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | typedef struct grpc_winsocket { |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 83 | SOCKET socket; |
Craig Tiller | 5352ff1 | 2016-05-18 10:44:38 -0700 | [diff] [blame] | 84 | bool destroy_called; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 85 | |
| 86 | grpc_winsocket_callback_info write_info; |
| 87 | grpc_winsocket_callback_info read_info; |
| 88 | |
| 89 | gpr_mu state_mu; |
Craig Tiller | d112ed9 | 2016-11-30 09:16:07 -0800 | [diff] [blame] | 90 | bool shutdown_called; |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 91 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 92 | /* You can't add the same socket twice to the same IO Completion Port. |
| 93 | This prevents that. */ |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 94 | int added_to_iocp; |
David Garcia Quintas | 5f228f5 | 2015-05-26 19:58:50 -0700 | [diff] [blame] | 95 | |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 96 | grpc_closure shutdown_closure; |
Craig Tiller | 33da332 | 2015-06-02 10:29:40 -0700 | [diff] [blame] | 97 | |
| 98 | /* A label for iomgr to track outstanding objects */ |
| 99 | grpc_iomgr_object iomgr_object; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 100 | } grpc_winsocket; |
| 101 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 102 | /* Create a wrapped windows handle. This takes ownership of it, meaning that |
| 103 | it will be responsible for closing it. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 104 | grpc_winsocket *grpc_winsocket_create(SOCKET socket, const char *name); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 105 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 106 | /* Initiate an asynchronous shutdown of the socket. Will call off any pending |
Craig Tiller | 9f80fcf | 2015-08-28 08:22:48 -0700 | [diff] [blame] | 107 | operation to cancel them. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | void grpc_winsocket_shutdown(grpc_winsocket *socket); |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 109 | |
Craig Tiller | 9f80fcf | 2015-08-28 08:22:48 -0700 | [diff] [blame] | 110 | /* Destroy a socket. Should only be called if there's no pending operation. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | void grpc_winsocket_destroy(grpc_winsocket *socket); |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 112 | |
Craig Tiller | 5352ff1 | 2016-05-18 10:44:38 -0700 | [diff] [blame] | 113 | void grpc_socket_notify_on_write(grpc_exec_ctx *exec_ctx, |
| 114 | grpc_winsocket *winsocket, |
| 115 | grpc_closure *closure); |
| 116 | |
| 117 | void grpc_socket_notify_on_read(grpc_exec_ctx *exec_ctx, |
| 118 | grpc_winsocket *winsocket, |
| 119 | grpc_closure *closure); |
| 120 | |
Craig Tiller | 95b9064 | 2016-05-18 12:12:44 -0700 | [diff] [blame] | 121 | void grpc_socket_become_ready(grpc_exec_ctx *exec_ctx, |
| 122 | grpc_winsocket *winsocket, |
| 123 | grpc_winsocket_callback_info *ci); |
Craig Tiller | 5352ff1 | 2016-05-18 10:44:38 -0700 | [diff] [blame] | 124 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 125 | #endif /* GRPC_CORE_LIB_IOMGR_SOCKET_WINDOWS_H */ |