GRPC Core  18.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 
24 #include "absl/container/inlined_vector.h"
25 
27 
29 
38 
39 namespace grpc_core {
40 
47 
63  grpc_endpoint* endpoint = nullptr;
64  grpc_channel_args* args = nullptr;
66  // A handshaker may set this to true before invoking on_handshake_done
67  // to indicate that subsequent handshakers should be skipped.
68  bool exit_early = false;
69  // User data passed through the handshake manager. Not used by
70  // individual handshakers.
71  void* user_data = nullptr;
72 };
73 
77 
78 class Handshaker : public RefCounted<Handshaker> {
79  public:
80  ~Handshaker() override = default;
81  virtual void Shutdown(grpc_error_handle why) = 0;
82  virtual void DoHandshake(grpc_tcp_server_acceptor* acceptor,
83  grpc_closure* on_handshake_done,
84  HandshakerArgs* args) = 0;
85  virtual const char* name() const = 0;
86 };
87 
88 //
89 // HandshakeManager
90 //
91 
92 class HandshakeManager : public RefCounted<HandshakeManager> {
93  public:
95  ~HandshakeManager() override;
96 
99  void Add(RefCountedPtr<Handshaker> handshaker);
100 
103  void Shutdown(grpc_error_handle why);
104 
117  void DoHandshake(grpc_endpoint* endpoint,
118  const grpc_channel_args* channel_args, grpc_millis deadline,
119  grpc_tcp_server_acceptor* acceptor,
120  grpc_iomgr_cb_func on_handshake_done, void* user_data);
121 
122  private:
123  bool CallNextHandshakerLocked(grpc_error_handle error);
124 
125  // A function used as the handshaker-done callback when chaining
126  // handshakers together.
127  static void CallNextHandshakerFn(void* arg, grpc_error_handle error);
128 
129  // Callback invoked when deadline is exceeded.
130  static void OnTimeoutFn(void* arg, grpc_error_handle error);
131 
132  static const size_t HANDSHAKERS_INIT_SIZE = 2;
133 
134  Mutex mu_;
135  bool is_shutdown_ = false;
136  // An array of handshakers added via grpc_handshake_manager_add().
137  absl::InlinedVector<RefCountedPtr<Handshaker>, HANDSHAKERS_INIT_SIZE>
138  handshakers_;
139  // The index of the handshaker to invoke next and closure to invoke it.
140  size_t index_ = 0;
141  grpc_closure call_next_handshaker_;
142  // The acceptor to call the handshakers with.
143  grpc_tcp_server_acceptor* acceptor_;
144  // Deadline timer across all handshakers.
145  grpc_timer deadline_timer_;
146  grpc_closure on_timeout_;
147  // The final callback and user_data to invoke after the last handshaker.
148  grpc_closure on_handshake_done_;
149  // Handshaker args.
150  HandshakerArgs args_;
151 };
152 
153 } // namespace grpc_core
154 
155 // TODO(arjunroy): These are transitional to account for the new handshaker API
156 // and will eventually be removed entirely.
160  grpc_handshaker* handshaker);
161 
162 #endif /* GRPC_CORE_LIB_CHANNEL_HANDSHAKER_H */
Definition: handshaker.h:92
void Shutdown(grpc_error_handle why)
Shuts down the handshake manager (e.g., to clean up when the operation is aborted in the middle).
Definition: handshaker.cc:71
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:169
void Add(RefCountedPtr< Handshaker > handshaker)
Adds a handshaker to the handshake manager.
Definition: handshaker.cc:58
~HandshakeManager() override
Definition: handshaker.cc:69
HandshakeManager()
Definition: handshaker.cc:56
Handshaker.
Definition: handshaker.h:78
virtual void DoHandshake(grpc_tcp_server_acceptor *acceptor, grpc_closure *on_handshake_done, HandshakerArgs *args)=0
virtual void Shutdown(grpc_error_handle why)=0
virtual const char * name() const =0
~Handshaker() override=default
Definition: sync.h:59
Definition: ref_counted.h:282
Definition: ref_counted_ptr.h:35
void(* grpc_iomgr_cb_func)(void *arg, grpc_error_handle error)
gRPC Callback definition.
Definition: closure.h:53
int64_t grpc_millis
Definition: exec_ctx.h:37
grpc_core::Handshaker grpc_handshaker
Definition: handshaker.h:158
grpc_core::HandshakeManager grpc_handshake_manager
Definition: handshaker.h:157
void grpc_handshake_manager_add(grpc_handshake_manager *mgr, grpc_handshaker *handshaker)
Definition: handshaker.cc:215
grpc_error_handle error
Definition: lame_client.cc:54
Round Robin Policy.
Definition: backend_metric.cc: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:62
grpc_slice_buffer * read_buffer
Definition: handshaker.h:65
void * user_data
Definition: handshaker.h:71
grpc_endpoint * endpoint
Definition: handshaker.h:63
grpc_channel_args * args
Definition: handshaker.h:64
bool exit_early
Definition: handshaker.h:68
Definition: endpoint.h:106
Definition: error_internal.h:41
Represents an expandable array of slices, to be interpreted as a single item.
Definition: slice.h:78
Definition: tcp_server.h:36
Definition: timer.h:32