1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #ifndef GRPC_CORE_LIB_IOMGR_SOCKET_WINDOWS_H 20 #define GRPC_CORE_LIB_IOMGR_SOCKET_WINDOWS_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include "src/core/lib/iomgr/port.h" 25 26 #ifdef GRPC_WINSOCK_SOCKET 27 #include <winsock2.h> 28 29 #include <grpc/support/atm.h> 30 #include <grpc/support/sync.h> 31 32 #include "src/core/lib/iomgr/closure.h" 33 #include "src/core/lib/iomgr/iomgr_internal.h" 34 35 /* This holds the data for an outstanding read or write on a socket. 36 The mutex to protect the concurrent access to that data is the one 37 inside the winsocket wrapper. */ 38 typedef struct grpc_winsocket_callback_info { 39 /* This is supposed to be a WSAOVERLAPPED, but in order to get that 40 definition, we need to include ws2tcpip.h, which needs to be included 41 from the top, otherwise it'll clash with a previous inclusion of 42 windows.h that in turns includes winsock.h. If anyone knows a way 43 to do it properly, feel free to send a patch. */ 44 OVERLAPPED overlapped; 45 /* The callback information for the pending operation. May be empty if the 46 caller hasn't registered a callback yet. */ 47 grpc_closure* closure; 48 /* A boolean to describe if the IO Completion Port got a notification for 49 that operation. This will happen if the operation completed before the 50 called had time to register a callback. We could avoid that behavior 51 altogether by forcing the caller to always register its callback before 52 proceeding queue an operation, but it is frequent for an IO Completion 53 Port to trigger quickly. This way we avoid a context switch for calling 54 the callback. We also simplify the read / write operations to avoid having 55 to hold a mutex for a long amount of time. */ 56 int has_pending_iocp; 57 /* The results of the overlapped operation. */ 58 DWORD bytes_transfered; 59 int wsa_error; 60 } grpc_winsocket_callback_info; 61 62 /* This is a wrapper to a Windows socket. A socket can have one outstanding 63 read, and one outstanding write. Doing an asynchronous accept means waiting 64 for a read operation. Doing an asynchronous connect means waiting for a 65 write operation. These are completely arbitrary ties between the operation 66 and the kind of event, because we can have one overlapped per pending 67 operation, whichever its nature is. So we could have more dedicated pending 68 operation callbacks for connect and listen. But given the scope of listen 69 and accept, we don't need to go to that extent and waste memory. Also, this 70 is closer to what happens in posix world. */ 71 typedef struct grpc_winsocket { 72 SOCKET socket; 73 bool destroy_called; 74 75 grpc_winsocket_callback_info write_info; 76 grpc_winsocket_callback_info read_info; 77 78 gpr_mu state_mu; 79 bool shutdown_called; 80 81 /* You can't add the same socket twice to the same IO Completion Port. 82 This prevents that. */ 83 int added_to_iocp; 84 85 grpc_closure shutdown_closure; 86 87 /* A label for iomgr to track outstanding objects */ 88 grpc_iomgr_object iomgr_object; 89 } grpc_winsocket; 90 91 /* Create a wrapped windows handle. This takes ownership of it, meaning that 92 it will be responsible for closing it. */ 93 grpc_winsocket* grpc_winsocket_create(SOCKET socket, const char* name); 94 95 SOCKET grpc_winsocket_wrapped_socket(grpc_winsocket* socket); 96 97 /* Initiate an asynchronous shutdown of the socket. Will call off any pending 98 operation to cancel them. */ 99 void grpc_winsocket_shutdown(grpc_winsocket* socket); 100 101 /* Destroy a socket. Should only be called if there's no pending operation. */ 102 void grpc_winsocket_destroy(grpc_winsocket* socket); 103 104 void grpc_socket_notify_on_write(grpc_winsocket* winsocket, 105 grpc_closure* closure); 106 107 void grpc_socket_notify_on_read(grpc_winsocket* winsocket, 108 grpc_closure* closure); 109 110 void grpc_socket_become_ready(grpc_winsocket* winsocket, 111 grpc_winsocket_callback_info* ci); 112 113 /* Returns true if this system can create AF_INET6 sockets bound to ::1. 114 The value is probed once, and cached for the life of the process. */ 115 int grpc_ipv6_loopback_available(void); 116 117 #endif 118 119 #endif /* GRPC_CORE_LIB_IOMGR_SOCKET_WINDOWS_H */ 120