GRPC Core  9.0.0
handshaker.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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_CHANNEL_HANDSHAKER_H
20 #define GRPC_CORE_LIB_CHANNEL_HANDSHAKER_H
21 
23 
25 
27 
37 
38 namespace grpc_core {
39 
46 
62  grpc_endpoint* endpoint = nullptr;
63  grpc_channel_args* args = nullptr;
65  // A handshaker may set this to true before invoking on_handshake_done
66  // to indicate that subsequent handshakers should be skipped.
67  bool exit_early = false;
68  // User data passed through the handshake manager. Not used by
69  // individual handshakers.
70  void* user_data = nullptr;
71 };
72 
76 
77 class Handshaker : public RefCounted<Handshaker> {
78  public:
79  virtual ~Handshaker() = default;
80  virtual void Shutdown(grpc_error* why) = 0;
81  virtual void DoHandshake(grpc_tcp_server_acceptor* acceptor,
82  grpc_closure* on_handshake_done,
83  HandshakerArgs* args) = 0;
84  virtual const char* name() const = 0;
85 };
86 
87 //
88 // HandshakeManager
89 //
90 
91 class HandshakeManager : public RefCounted<HandshakeManager> {
92  public:
95 
98  // Not thread-safe. Caller needs to synchronize.
100 
102  // Not thread-safe. Caller needs to synchronize.
104 
107  void ShutdownAllPending(grpc_error* why);
108 
111  void Add(RefCountedPtr<Handshaker> handshaker);
112 
115  void Shutdown(grpc_error* why);
116 
129  void DoHandshake(grpc_endpoint* endpoint,
130  const grpc_channel_args* channel_args, grpc_millis deadline,
131  grpc_tcp_server_acceptor* acceptor,
132  grpc_iomgr_cb_func on_handshake_done, void* user_data);
133 
134  private:
135  bool CallNextHandshakerLocked(grpc_error* error);
136 
137  // A function used as the handshaker-done callback when chaining
138  // handshakers together.
139  static void CallNextHandshakerFn(void* arg, grpc_error* error);
140 
141  // Callback invoked when deadline is exceeded.
142  static void OnTimeoutFn(void* arg, grpc_error* error);
143 
144  static const size_t HANDSHAKERS_INIT_SIZE = 2;
145 
146  gpr_mu mu_;
147  bool is_shutdown_ = false;
148  // An array of handshakers added via grpc_handshake_manager_add().
149  InlinedVector<RefCountedPtr<Handshaker>, HANDSHAKERS_INIT_SIZE> handshakers_;
150  // The index of the handshaker to invoke next and closure to invoke it.
151  size_t index_ = 0;
152  grpc_closure call_next_handshaker_;
153  // The acceptor to call the handshakers with.
154  grpc_tcp_server_acceptor* acceptor_;
155  // Deadline timer across all handshakers.
156  grpc_timer deadline_timer_;
157  grpc_closure on_timeout_;
158  // The final callback and user_data to invoke after the last handshaker.
159  grpc_closure on_handshake_done_;
160  // Handshaker args.
161  HandshakerArgs args_;
162  // Links to the previous and next managers in a list of all pending handshakes
163  // Used at server side only.
164  HandshakeManager* prev_ = nullptr;
165  HandshakeManager* next_ = nullptr;
166 };
167 
168 } // namespace grpc_core
169 
170 // TODO(arjunroy): These are transitional to account for the new handshaker API
171 // and will eventually be removed entirely.
175  grpc_handshaker* handshaker);
176 
177 #endif /* GRPC_CORE_LIB_CHANNEL_HANDSHAKER_H */
Definition: handshaker.h:91
~HandshakeManager()
Definition: handshaker.cc:108
void ShutdownAllPending(grpc_error *why)
Shutdown all pending handshake managers starting at head on the server side.
Definition: handshaker.cc:88
void DoHandshake(grpc_endpoint *endpoint, const grpc_channel_args *channel_args, grpc_millis deadline, grpc_tcp_server_acceptor *acceptor, grpc_iomgr_cb_func on_handshake_done, void *user_data)
Invokes handshakers in the order they were added.
Definition: handshaker.cc:211
void RemoveFromPendingMgrList(HandshakeManager **head)
Remove mgr from the server side list of all pending handshake managers.
Definition: handshaker.cc:74
void AddToPendingMgrList(HandshakeManager **head)
Add mgr to the server side list of all pending handshake managers, the list starts with *head.
Definition: handshaker.cc:62
void Add(RefCountedPtr< Handshaker > handshaker)
Adds a handshaker to the handshake manager.
Definition: handshaker.cc:97
void Shutdown(grpc_error *why)
Shuts down the handshake manager (e.g., to clean up when the operation is aborted in the middle).
Definition: handshaker.cc:113
HandshakeManager()
Definition: handshaker.cc:57
Handshaker.
Definition: handshaker.h:77
virtual void DoHandshake(grpc_tcp_server_acceptor *acceptor, grpc_closure *on_handshake_done, HandshakerArgs *args)=0
virtual const char * name() const =0
virtual ~Handshaker()=default
virtual void Shutdown(grpc_error *why)=0
Definition: inlined_vector.h:60
Definition: ref_counted.h:248
Definition: ref_counted_ptr.h:35
void(* grpc_iomgr_cb_func)(void *arg, grpc_error *error)
gRPC Callback definition.
Definition: closure.h:53
int64_t grpc_millis
Definition: exec_ctx.h:35
grpc_core::Handshaker grpc_handshaker
Definition: handshaker.h:173
grpc_core::HandshakeManager grpc_handshake_manager
Definition: handshaker.h:172
void grpc_handshake_manager_add(grpc_handshake_manager *mgr, grpc_handshaker *handshaker)
Definition: handshaker.cc:258
Round Robin Policy.
Definition: backend_metric.cc:24
Definition: sync_windows.h:26
An array of arguments that can be passed around.
Definition: grpc_types.h:132
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
Handshakers are used to perform initial handshakes on a connection before the client sends the initia...
Definition: handshaker.h:61
grpc_slice_buffer * read_buffer
Definition: handshaker.h:64
void * user_data
Definition: handshaker.h:70
grpc_endpoint * endpoint
Definition: handshaker.h:62
grpc_channel_args * args
Definition: handshaker.h:63
bool exit_early
Definition: handshaker.h:67
Definition: endpoint.h:102
Definition: error_internal.h:39
Represents an expandable array of slices, to be interpreted as a single item.
Definition: slice.h:78
Definition: tcp_server.h:34
Definition: timer.h:30