OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 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 #ifndef GRPC_INTERNAL_CORE_IOMGR_SOCKET_WINDOWS_H |
| 35 #define GRPC_INTERNAL_CORE_IOMGR_SOCKET_WINDOWS_H |
| 36 |
| 37 #include <grpc/support/port_platform.h> |
| 38 #include <winsock2.h> |
| 39 |
| 40 #include <grpc/support/sync.h> |
| 41 #include <grpc/support/atm.h> |
| 42 |
| 43 #include "src/core/iomgr/iomgr_internal.h" |
| 44 #include "src/core/iomgr/exec_ctx.h" |
| 45 |
| 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. */ |
| 49 typedef struct grpc_winsocket_callback_info { |
| 50 /* This is supposed to be a WSAOVERLAPPED, but in order to get that |
| 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. */ |
| 55 OVERLAPPED overlapped; |
| 56 /* The callback information for the pending operation. May be empty if the |
| 57 caller hasn't registered a callback yet. */ |
| 58 grpc_closure *closure; |
| 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. */ |
| 67 int has_pending_iocp; |
| 68 /* The results of the overlapped operation. */ |
| 69 DWORD bytes_transfered; |
| 70 int wsa_error; |
| 71 } grpc_winsocket_callback_info; |
| 72 |
| 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 |
| 76 write operation. These are completely arbitrary ties between the operation |
| 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. */ |
| 82 typedef struct grpc_winsocket { |
| 83 SOCKET socket; |
| 84 |
| 85 grpc_winsocket_callback_info write_info; |
| 86 grpc_winsocket_callback_info read_info; |
| 87 |
| 88 gpr_mu state_mu; |
| 89 |
| 90 /* You can't add the same socket twice to the same IO Completion Port. |
| 91 This prevents that. */ |
| 92 int added_to_iocp; |
| 93 |
| 94 grpc_closure shutdown_closure; |
| 95 |
| 96 /* A label for iomgr to track outstanding objects */ |
| 97 grpc_iomgr_object iomgr_object; |
| 98 } grpc_winsocket; |
| 99 |
| 100 /* Create a wrapped windows handle. This takes ownership of it, meaning that |
| 101 it will be responsible for closing it. */ |
| 102 grpc_winsocket *grpc_winsocket_create(SOCKET socket, const char *name); |
| 103 |
| 104 /* Initiate an asynchronous shutdown of the socket. Will call off any pending |
| 105 operation to cancel them. */ |
| 106 void grpc_winsocket_shutdown(grpc_winsocket *socket); |
| 107 |
| 108 /* Destroy a socket. Should only be called if there's no pending operation. */ |
| 109 void grpc_winsocket_destroy(grpc_winsocket *socket); |
| 110 |
| 111 #endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKET_WINDOWS_H */ |
OLD | NEW |