| 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 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/iomgr/port.h" |
| 22 |
|
| 23 |
#ifdef GRPC_WINSOCK_SOCKET |
| 24 |
|
| 25 |
#include <grpc/support/log.h> |
| 26 |
|
| 27 |
#include "src/core/lib/iomgr/iocp_windows.h" |
| 28 |
#include "src/core/lib/iomgr/iomgr.h" |
| 29 |
#include "src/core/lib/iomgr/pollset_windows.h" |
| 30 |
#include "src/core/lib/iomgr/resolve_address.h" |
| 31 |
#include "src/core/lib/iomgr/resolve_address_windows.h" |
| 32 |
#include "src/core/lib/iomgr/sockaddr_windows.h" |
| 33 |
#include "src/core/lib/iomgr/socket_windows.h" |
| 34 |
#include "src/core/lib/iomgr/tcp_client.h" |
| 35 |
#include "src/core/lib/iomgr/tcp_server.h" |
| 36 |
#include "src/core/lib/iomgr/timer.h" |
| 37 |
|
| 38 |
extern grpc_tcp_server_vtable grpc_windows_tcp_server_vtable; |
| 39 |
extern grpc_tcp_client_vtable grpc_windows_tcp_client_vtable; |
| 40 |
extern grpc_timer_vtable grpc_generic_timer_vtable; |
| 41 |
extern grpc_pollset_vtable grpc_windows_pollset_vtable; |
| 42 |
extern grpc_pollset_set_vtable grpc_windows_pollset_set_vtable; |
| 43 |
|
| 44 |
/* Windows' io manager is going to be fully designed using IO completion |
| 45 |
ports. All of what we're doing here is basically make sure that |
| 46 |
Windows sockets are initialized in and out. */ |
| 47 |
|
| 48 |
static void winsock_init(void) { |
| 49 |
WSADATA wsaData; |
| 50 |
int status = WSAStartup(MAKEWORD(2, 0), &wsaData); |
| 51 |
GPR_ASSERT(status == 0); |
| 52 |
} |
| 53 |
|
| 54 |
static void winsock_shutdown(void) { |
| 55 |
int status = WSACleanup(); |
| 56 |
GPR_ASSERT(status == 0); |
| 57 |
} |
| 58 |
|
| 59 |
static void iomgr_platform_init(void) { |
| 60 |
winsock_init(); |
| 61 |
grpc_iocp_init(); |
| 62 |
grpc_pollset_global_init(); |
| 63 |
grpc_wsa_socket_flags_init(); |
| 64 |
grpc_core::ResetDNSResolver(std::make_unique<grpc_core::NativeDNSResolver>()); |
| 65 |
} |
| 66 |
|
| 67 |
static void iomgr_platform_flush(void) { grpc_iocp_flush(); } |
| 68 |
|
| 69 |
static void iomgr_platform_shutdown(void) { |
| 70 |
grpc_pollset_global_shutdown(); |
| 71 |
grpc_iocp_shutdown(); |
| 72 |
winsock_shutdown(); |
| 73 |
grpc_core::ResetDNSResolver(nullptr); // delete the resolver |
| 74 |
} |
| 75 |
|
| 76 |
static void iomgr_platform_shutdown_background_closure(void) {} |
| 77 |
|
| 78 |
static bool iomgr_platform_is_any_background_poller_thread(void) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
static bool iomgr_platform_add_closure_to_background_poller( |
| 83 |
grpc_closure* closure, grpc_error_handle error) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
static grpc_iomgr_platform_vtable vtable = { |
| 88 |
iomgr_platform_init, |
| 89 |
iomgr_platform_flush, |
| 90 |
iomgr_platform_shutdown, |
| 91 |
iomgr_platform_shutdown_background_closure, |
| 92 |
iomgr_platform_is_any_background_poller_thread, |
| 93 |
iomgr_platform_add_closure_to_background_poller}; |
| 94 |
|
| 95 |
void grpc_set_default_iomgr_platform() { |
| 96 |
grpc_set_tcp_client_impl(&grpc_windows_tcp_client_vtable); |
| 97 |
grpc_set_tcp_server_impl(&grpc_windows_tcp_server_vtable); |
| 98 |
grpc_set_timer_impl(&grpc_generic_timer_vtable); |
| 99 |
grpc_set_pollset_vtable(&grpc_windows_pollset_vtable); |
| 100 |
grpc_set_pollset_set_vtable(&grpc_windows_pollset_set_vtable); |
| 101 |
grpc_set_iomgr_platform_vtable(&vtable); |
| 102 |
} |
| 103 |
|
| 104 |
bool grpc_iomgr_run_in_background() { return false; } |
| 105 |
|
| 106 |
#endif /* GRPC_WINSOCK_SOCKET */ |