Debian Codegraph @

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

raw
1 // Copyright 2022 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_WIN_SOCKET_H
15 #define GRPC_CORE_LIB_EVENT_ENGINE_WINDOWS_WIN_SOCKET_H
16
17 #include <grpc/support/port_platform.h>
18
19 #ifdef GPR_WINDOWS
20
21 #include "absl/base/thread_annotations.h"
22 #include "absl/functional/any_invocable.h"
23
24 #include <grpc/event_engine/event_engine.h>
25
26 #include "src/core/lib/event_engine/executor/executor.h"
27 #include "src/core/lib/event_engine/socket_notifier.h"
28 #include "src/core/lib/gprpp/sync.h"
29
30 namespace grpc_event_engine {
31 namespace experimental {
32
33 class WinSocket final : public SocketNotifier {
34 public:
35 // State related to a Read or Write socket operation
36 class OpState {
37 public:
38 explicit OpState(WinSocket* win_socket) noexcept;
39 // Signal a result has returned
40 // If a callback is already primed for notification, it will be executed via
41 // the WinSocket's Executor. Otherwise, a "pending iocp" flag will
42 // be set.
43 void SetReady();
44 // Set error results for a completed op
45 void SetError(int wsa_error);
46 // Retrieve results of overlapped operation (via Winsock API)
47 void GetOverlappedResult();
48 // OVERLAPPED, needed for Winsock API calls
49 LPOVERLAPPED overlapped() { return &overlapped_; }
50 // Data from the previous operation, set via GetOverlappedResult
51 DWORD bytes_transferred() const { return bytes_transferred_; }
52 // Previous error if set.
53 int wsa_error() const { return wsa_error_; }
54 EventEngine::Closure* closure() { return closure_; }
55
56 private:
57 friend class WinSocket;
58
59 OVERLAPPED overlapped_;
60 WinSocket* win_socket_ = nullptr;
61 EventEngine::Closure* closure_ = nullptr;
62 bool has_pending_iocp_ = false;
63 DWORD bytes_transferred_;
64 int wsa_error_;
65 };
66
67 WinSocket(SOCKET socket, Executor* executor) noexcept;
68 ~WinSocket();
69 // Calling NotifyOnRead means either of two things:
70 // - The IOCP already completed in the background, and we need to call
71 // the callback now.
72 // - The IOCP hasn't completed yet, and we're queuing it for later.
73 void NotifyOnRead(EventEngine::Closure* on_read) override;
74 void NotifyOnWrite(EventEngine::Closure* on_write) override;
75 void SetReadable() override;
76 void SetWritable() override;
77 // Schedule a shutdown of the socket operations. Will call the pending
78 // operations to abort them. We need to do that this way because of the
79 // various callsites of that function, which happens to be in various
80 // mutex hold states, and that'd be unsafe to call them directly.
81 void MaybeShutdown(absl::Status why) override;
82 bool IsShutdown() override;
83
84 // Return the appropriate OpState for a given OVERLAPPED
85 // Returns nullptr if the overlapped does not match either read or write ops.
86 OpState* GetOpInfoForOverlapped(OVERLAPPED* overlapped);
87 // Getters for the operation state data.
88 OpState* read_info() { return &read_info_; }
89 OpState* write_info() { return &write_info_; }
90 // Accessor method for underlying socket
91 SOCKET socket();
92
93 private:
94 void NotifyOnReady(OpState& info, EventEngine::Closure* closure);
95
96 SOCKET socket_;
97 std::atomic<bool> is_shutdown_{false};
98 Executor* executor_;
99 // These OpStates are effectively synchronized using their respective
100 // OVERLAPPED structures and the Overlapped I/O APIs. For example, OpState
101 // users should not attempt to read their bytes_transeferred until
102 // GetOverlappedResult has returned, to ensure there are no two threads
103 // reading and writing the same values concurrently.
104 //
105 // Callers must also ensure that at most one read and write operation are
106 // occurring at a time. Attempting to do multiple concurrent reads/writes will
107 // have undefined behavior.
108 OpState read_info_;
109 OpState write_info_;
110 };
111
112 // Attempt to configure default socket settings
113 absl::Status PrepareSocket(SOCKET sock);
114
115 } // namespace experimental
116 } // namespace grpc_event_engine
117
118 #endif
119
120 #endif // GRPC_CORE_LIB_EVENT_ENGINE_WINDOWS_WIN_SOCKET_H