Debian Codegraph @

src/core/lib/event_engine/windows/windows_engine.h

raw
1 // Copyright 2022 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef GRPC_CORE_LIB_EVENT_ENGINE_WINDOWS_WINDOWS_ENGINE_H
15 #define GRPC_CORE_LIB_EVENT_ENGINE_WINDOWS_WINDOWS_ENGINE_H
16 #include <grpc/support/port_platform.h>
17
18 #ifdef GPR_WINDOWS
19
20 #include <memory>
21
22 #include "absl/status/status.h"
23 #include "absl/status/statusor.h"
24 #include "absl/strings/string_view.h"
25
26 #include <grpc/event_engine/endpoint_config.h>
27 #include <grpc/event_engine/event_engine.h>
28 #include <grpc/event_engine/memory_allocator.h>
29 #include <grpc/event_engine/slice_buffer.h>
30
31 #include "src/core/lib/event_engine/handle_containers.h"
32 #include "src/core/lib/event_engine/posix_engine/timer_manager.h"
33 #include "src/core/lib/event_engine/thread_pool.h"
34 #include "src/core/lib/event_engine/windows/iocp.h"
35 #include "src/core/lib/gprpp/sync.h"
36 #include "src/core/lib/gprpp/time.h"
37 #include "src/core/lib/surface/init_internally.h"
38
39 namespace grpc_event_engine {
40 namespace experimental {
41
42 // TODO(ctiller): KeepsGrpcInitialized is an interim measure to ensure that
43 // event engine is shut down before we shut down iomgr.
44 class WindowsEventEngine : public EventEngine,
45 public grpc_core::KeepsGrpcInitialized {
46 public:
47 class WindowsEndpoint : public EventEngine::Endpoint {
48 public:
49 ~WindowsEndpoint() override;
50 void Read(absl::AnyInvocable<void(absl::Status)> on_read,
51 SliceBuffer* buffer, const ReadArgs* args) override;
52 void Write(absl::AnyInvocable<void(absl::Status)> on_writable,
53 SliceBuffer* data, const WriteArgs* args) override;
54 const ResolvedAddress& GetPeerAddress() const override;
55 const ResolvedAddress& GetLocalAddress() const override;
56 };
57 class WindowsListener : public EventEngine::Listener {
58 public:
59 ~WindowsListener() override;
60 absl::StatusOr<int> Bind(const ResolvedAddress& addr) override;
61 absl::Status Start() override;
62 };
63 class WindowsDNSResolver : public EventEngine::DNSResolver {
64 public:
65 ~WindowsDNSResolver() override;
66 LookupTaskHandle LookupHostname(LookupHostnameCallback on_resolve,
67 absl::string_view name,
68 absl::string_view default_port,
69 Duration timeout) override;
70 LookupTaskHandle LookupSRV(LookupSRVCallback on_resolve,
71 absl::string_view name,
72 Duration timeout) override;
73 LookupTaskHandle LookupTXT(LookupTXTCallback on_resolve,
74 absl::string_view name,
75 Duration timeout) override;
76 bool CancelLookup(LookupTaskHandle handle) override;
77 };
78
79 WindowsEventEngine();
80 ~WindowsEventEngine() override;
81
82 absl::StatusOr<std::unique_ptr<Listener>> CreateListener(
83 Listener::AcceptCallback on_accept,
84 absl::AnyInvocable<void(absl::Status)> on_shutdown,
85 const EndpointConfig& config,
86 std::unique_ptr<MemoryAllocatorFactory> memory_allocator_factory)
87 override;
88
89 ConnectionHandle Connect(OnConnectCallback on_connect,
90 const ResolvedAddress& addr,
91 const EndpointConfig& args,
92 MemoryAllocator memory_allocator,
93 Duration timeout) override;
94
95 bool CancelConnect(ConnectionHandle handle) override;
96 bool IsWorkerThread() override;
97 std::unique_ptr<DNSResolver> GetDNSResolver(
98 const DNSResolver::ResolverOptions& options) override;
99 void Run(Closure* closure) override;
100 void Run(absl::AnyInvocable<void()> closure) override;
101 TaskHandle RunAfter(Duration when, Closure* closure) override;
102 TaskHandle RunAfter(Duration when,
103 absl::AnyInvocable<void()> closure) override;
104 bool Cancel(TaskHandle handle) override;
105
106 private:
107 struct Closure;
108 EventEngine::TaskHandle RunAfterInternal(Duration when,
109 absl::AnyInvocable<void()> cb);
110 grpc_core::Mutex mu_;
111 TaskHandleSet known_handles_ ABSL_GUARDED_BY(mu_);
112 std::atomic<intptr_t> aba_token_{0};
113
114 std::shared_ptr<ThreadPool> executor_;
115 IOCP iocp_;
116 posix_engine::TimerManager timer_manager_;
117 };
118
119 } // namespace experimental
120 } // namespace grpc_event_engine
121
122 #endif
123
124 #endif // GRPC_CORE_LIB_EVENT_ENGINE_WINDOWS_WINDOWS_ENGINE_H