grpc 1.18.0 → 1.19.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (146) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +301 -33
  3. data/include/grpc/grpc_security.h +195 -0
  4. data/include/grpc/impl/codegen/grpc_types.h +17 -1
  5. data/include/grpc/impl/codegen/port_platform.h +36 -0
  6. data/include/grpc/impl/codegen/slice.h +1 -1
  7. data/src/core/ext/filters/client_channel/channel_connectivity.cc +2 -0
  8. data/src/core/ext/filters/client_channel/client_channel.cc +74 -69
  9. data/src/core/ext/filters/client_channel/client_channel.h +2 -2
  10. data/src/core/ext/filters/client_channel/client_channel_channelz.cc +5 -6
  11. data/src/core/ext/filters/client_channel/client_channel_channelz.h +5 -4
  12. data/src/core/ext/filters/client_channel/client_channel_factory.cc +2 -2
  13. data/src/core/ext/filters/client_channel/client_channel_factory.h +4 -4
  14. data/src/core/ext/filters/client_channel/client_channel_plugin.cc +3 -3
  15. data/src/core/ext/filters/client_channel/global_subchannel_pool.cc +176 -0
  16. data/src/core/ext/filters/client_channel/global_subchannel_pool.h +68 -0
  17. data/src/core/ext/filters/client_channel/health/health_check_client.cc +10 -8
  18. data/src/core/ext/filters/client_channel/health/health_check_client.h +1 -1
  19. data/src/core/ext/filters/client_channel/http_connect_handshaker.cc +146 -156
  20. data/src/core/ext/filters/client_channel/lb_policy.cc +30 -1
  21. data/src/core/ext/filters/client_channel/lb_policy.h +29 -1
  22. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc +28 -30
  23. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.cc +5 -8
  24. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.cc +5 -8
  25. data/src/core/ext/filters/client_channel/lb_policy/subchannel_list.h +23 -24
  26. data/src/core/ext/filters/client_channel/lb_policy/xds/xds.cc +80 -15
  27. data/src/core/ext/filters/client_channel/lb_policy_factory.h +6 -1
  28. data/src/core/ext/filters/client_channel/lb_policy_registry.cc +2 -2
  29. data/src/core/ext/filters/client_channel/lb_policy_registry.h +1 -1
  30. data/src/core/ext/filters/client_channel/local_subchannel_pool.cc +96 -0
  31. data/src/core/ext/filters/client_channel/local_subchannel_pool.h +56 -0
  32. data/src/core/ext/filters/client_channel/parse_address.cc +24 -5
  33. data/src/core/ext/filters/client_channel/request_routing.cc +13 -3
  34. data/src/core/ext/filters/client_channel/request_routing.h +5 -1
  35. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc +11 -6
  36. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc +2 -2
  37. data/src/core/ext/filters/client_channel/resolver_result_parsing.cc +7 -35
  38. data/src/core/ext/filters/client_channel/subchannel.cc +698 -791
  39. data/src/core/ext/filters/client_channel/subchannel.h +213 -123
  40. data/src/core/ext/filters/client_channel/subchannel_pool_interface.cc +97 -0
  41. data/src/core/ext/filters/client_channel/subchannel_pool_interface.h +94 -0
  42. data/src/core/ext/filters/http/client_authority_filter.cc +5 -2
  43. data/src/core/ext/filters/max_age/max_age_filter.cc +1 -1
  44. data/src/core/ext/transport/chttp2/client/chttp2_connector.cc +13 -12
  45. data/src/core/ext/transport/chttp2/client/insecure/channel_create.cc +5 -7
  46. data/src/core/ext/transport/chttp2/client/secure/secure_channel_create.cc +19 -27
  47. data/src/core/ext/transport/chttp2/server/chttp2_server.cc +18 -19
  48. data/src/core/ext/transport/chttp2/transport/chttp2_transport.cc +27 -6
  49. data/src/core/ext/transport/chttp2/transport/flow_control.cc +1 -1
  50. data/src/core/ext/transport/chttp2/transport/frame_window_update.cc +3 -2
  51. data/src/core/ext/transport/chttp2/transport/hpack_encoder.cc +1 -1
  52. data/src/core/ext/transport/chttp2/transport/writing.cc +8 -5
  53. data/src/core/lib/channel/handshaker.cc +141 -214
  54. data/src/core/lib/channel/handshaker.h +110 -101
  55. data/src/core/lib/channel/handshaker_factory.h +11 -19
  56. data/src/core/lib/channel/handshaker_registry.cc +64 -52
  57. data/src/core/lib/channel/handshaker_registry.h +21 -16
  58. data/src/core/lib/gpr/log_posix.cc +2 -1
  59. data/src/core/lib/gpr/time.cc +8 -0
  60. data/src/core/lib/gpr/time_posix.cc +8 -2
  61. data/src/core/lib/gprpp/optional.h +47 -0
  62. data/src/core/lib/http/httpcli_security_connector.cc +13 -14
  63. data/src/core/lib/iomgr/buffer_list.cc +182 -24
  64. data/src/core/lib/iomgr/buffer_list.h +70 -8
  65. data/src/core/lib/iomgr/combiner.cc +11 -3
  66. data/src/core/lib/iomgr/error.cc +9 -5
  67. data/src/core/lib/iomgr/ev_epoll1_linux.cc +3 -0
  68. data/src/core/lib/iomgr/ev_epollex_linux.cc +136 -162
  69. data/src/core/lib/iomgr/ev_poll_posix.cc +3 -0
  70. data/src/core/lib/iomgr/ev_posix.cc +4 -0
  71. data/src/core/lib/iomgr/ev_posix.h +4 -0
  72. data/src/core/lib/iomgr/exec_ctx.cc +1 -0
  73. data/src/core/lib/iomgr/exec_ctx.h +137 -8
  74. data/src/core/lib/iomgr/executor.cc +122 -87
  75. data/src/core/lib/iomgr/executor.h +53 -48
  76. data/src/core/lib/iomgr/fork_posix.cc +6 -4
  77. data/src/core/lib/iomgr/{network_status_tracker.cc → grpc_if_nametoindex.h} +8 -14
  78. data/src/core/lib/iomgr/grpc_if_nametoindex_posix.cc +42 -0
  79. data/src/core/lib/iomgr/{network_status_tracker.h → grpc_if_nametoindex_unsupported.cc} +15 -9
  80. data/src/core/lib/iomgr/internal_errqueue.h +105 -3
  81. data/src/core/lib/iomgr/iomgr.cc +6 -5
  82. data/src/core/lib/iomgr/iomgr.h +8 -0
  83. data/src/core/lib/iomgr/iomgr_custom.cc +6 -2
  84. data/src/core/lib/iomgr/iomgr_internal.cc +4 -0
  85. data/src/core/lib/iomgr/iomgr_internal.h +4 -0
  86. data/src/core/lib/iomgr/iomgr_posix.cc +10 -1
  87. data/src/core/lib/iomgr/iomgr_windows.cc +8 -1
  88. data/src/core/lib/iomgr/port.h +1 -0
  89. data/src/core/lib/iomgr/resolve_address_posix.cc +4 -3
  90. data/src/core/lib/iomgr/resolve_address_windows.cc +2 -1
  91. data/src/core/lib/iomgr/tcp_custom.cc +0 -4
  92. data/src/core/lib/iomgr/tcp_posix.cc +58 -44
  93. data/src/core/lib/iomgr/tcp_uv.cc +0 -1
  94. data/src/core/lib/iomgr/tcp_windows.cc +0 -4
  95. data/src/core/lib/iomgr/timer_manager.cc +8 -0
  96. data/src/core/lib/iomgr/udp_server.cc +6 -4
  97. data/src/core/lib/json/json.cc +1 -4
  98. data/src/core/lib/security/credentials/alts/alts_credentials.cc +1 -1
  99. data/src/core/lib/security/credentials/alts/check_gcp_environment_no_op.cc +2 -2
  100. data/src/core/lib/security/credentials/composite/composite_credentials.h +4 -0
  101. data/src/core/lib/security/credentials/credentials.h +9 -1
  102. data/src/core/lib/security/credentials/google_default/google_default_credentials.cc +15 -2
  103. data/src/core/lib/security/credentials/google_default/google_default_credentials.h +2 -0
  104. data/src/core/lib/security/credentials/jwt/json_token.cc +1 -1
  105. data/src/core/lib/security/credentials/jwt/jwt_credentials.cc +1 -0
  106. data/src/core/lib/security/credentials/jwt/jwt_verifier.cc +3 -2
  107. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.cc +2 -2
  108. data/src/core/lib/security/credentials/plugin/plugin_credentials.cc +1 -0
  109. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.cc +192 -0
  110. data/src/core/lib/security/credentials/tls/grpc_tls_credentials_options.h +213 -0
  111. data/src/core/lib/security/security_connector/alts/alts_security_connector.cc +10 -8
  112. data/src/core/lib/security/security_connector/fake/fake_security_connector.cc +6 -10
  113. data/src/core/lib/security/security_connector/local/local_security_connector.cc +10 -8
  114. data/src/core/lib/security/security_connector/security_connector.h +2 -2
  115. data/src/core/lib/security/security_connector/ssl/ssl_security_connector.cc +4 -6
  116. data/src/core/lib/security/security_connector/ssl_utils.h +33 -0
  117. data/src/core/lib/security/transport/security_handshaker.cc +267 -300
  118. data/src/core/lib/security/transport/security_handshaker.h +11 -2
  119. data/src/core/lib/security/transport/server_auth_filter.cc +1 -0
  120. data/src/core/lib/surface/call.cc +5 -1
  121. data/src/core/lib/surface/channel_init.h +5 -0
  122. data/src/core/lib/surface/completion_queue.cc +4 -7
  123. data/src/core/lib/surface/init.cc +5 -3
  124. data/src/core/lib/surface/init_secure.cc +1 -1
  125. data/src/core/lib/surface/server.cc +19 -17
  126. data/src/core/lib/surface/version.cc +1 -1
  127. data/src/core/lib/transport/service_config.h +1 -0
  128. data/src/core/lib/transport/static_metadata.cc +279 -279
  129. data/src/core/lib/transport/transport.cc +5 -3
  130. data/src/core/tsi/ssl_transport_security.cc +10 -4
  131. data/src/ruby/ext/grpc/extconf.rb +12 -4
  132. data/src/ruby/ext/grpc/rb_call_credentials.c +8 -5
  133. data/src/ruby/ext/grpc/rb_channel.c +14 -10
  134. data/src/ruby/ext/grpc/rb_channel_credentials.c +8 -4
  135. data/src/ruby/ext/grpc/rb_compression_options.c +9 -7
  136. data/src/ruby/ext/grpc/rb_event_thread.c +2 -0
  137. data/src/ruby/ext/grpc/rb_grpc.c +22 -23
  138. data/src/ruby/ext/grpc/rb_grpc.h +4 -2
  139. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +18 -0
  140. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +27 -0
  141. data/src/ruby/ext/grpc/rb_server.c +8 -4
  142. data/src/ruby/lib/grpc/version.rb +1 -1
  143. metadata +46 -39
  144. data/src/core/ext/filters/client_channel/subchannel_index.cc +0 -248
  145. data/src/core/ext/filters/client_channel/subchannel_index.h +0 -76
  146. data/src/core/lib/channel/handshaker_factory.cc +0 -42
@@ -111,7 +111,7 @@ void FlowControlTrace::Finish() {
111
111
  saw_str = gpr_leftpad("", ' ', kTracePadding);
112
112
  }
113
113
  gpr_log(GPR_DEBUG,
114
- "%p[%u][%s] | %s | trw:%s, ttw:%s, taw:%s, srw:%s, slw:%s, saw:%s",
114
+ "%p[%u][%s] | %s | trw:%s, tlw:%s, taw:%s, srw:%s, slw:%s, saw:%s",
115
115
  tfc_, sfc_ != nullptr ? sfc_->stream()->id : 0,
116
116
  tfc_->transport()->is_client ? "cli" : "svr", reason_, trw_str,
117
117
  tlw_str, taw_str, srw_str, slw_str, saw_str);
@@ -88,8 +88,9 @@ grpc_error* grpc_chttp2_window_update_parser_parse(void* parser,
88
88
  }
89
89
 
90
90
  if (p->byte == 4) {
91
- uint32_t received_update = p->amount;
92
- if (received_update == 0 || (received_update & 0x80000000u)) {
91
+ // top bit is reserved and must be ignored.
92
+ uint32_t received_update = p->amount & 0x7fffffffu;
93
+ if (received_update == 0) {
93
94
  char* msg;
94
95
  gpr_asprintf(&msg, "invalid window update bytes: %d", p->amount);
95
96
  grpc_error* err = GRPC_ERROR_CREATE_FROM_COPIED_STRING(msg);
@@ -59,7 +59,7 @@
59
59
  static grpc_slice_refcount terminal_slice_refcount = {nullptr, nullptr};
60
60
  static const grpc_slice terminal_slice = {
61
61
  &terminal_slice_refcount, /* refcount */
62
- {{nullptr, 0}} /* data.refcounted */
62
+ {{0, nullptr}} /* data.refcounted */
63
63
  };
64
64
 
65
65
  typedef struct {
@@ -363,7 +363,6 @@ class DataSendContext {
363
363
  grpc_chttp2_encode_data(s_->id, &s_->compressed_data_buffer, send_bytes,
364
364
  is_last_frame_, &s_->stats.outgoing, &t_->outbuf);
365
365
  s_->flow_control->SentData(send_bytes);
366
- s_->byte_counter += send_bytes;
367
366
  if (s_->compressed_data_buffer.length == 0) {
368
367
  s_->sending_bytes += s_->uncompressed_data_size;
369
368
  }
@@ -498,9 +497,6 @@ class StreamWriteContext {
498
497
  data_send_context.CompressMoreBytes();
499
498
  }
500
499
  }
501
- if (s_->traced && grpc_endpoint_can_track_err(t_->ep)) {
502
- grpc_core::ContextList::Append(&t_->cl, s_);
503
- }
504
500
  write_context_->ResetPingClock();
505
501
  if (data_send_context.is_last_frame()) {
506
502
  SentLastFrame();
@@ -610,11 +606,18 @@ grpc_chttp2_begin_write_result grpc_chttp2_begin_write(
610
606
  (according to available window sizes) and add to the output buffer */
611
607
  while (grpc_chttp2_stream* s = ctx.NextStream()) {
612
608
  StreamWriteContext stream_ctx(&ctx, s);
609
+ size_t orig_len = t->outbuf.length;
613
610
  stream_ctx.FlushInitialMetadata();
614
611
  stream_ctx.FlushWindowUpdates();
615
612
  stream_ctx.FlushData();
616
613
  stream_ctx.FlushTrailingMetadata();
617
-
614
+ if (t->outbuf.length > orig_len) {
615
+ /* Add this stream to the list of the contexts to be traced at TCP */
616
+ s->byte_counter += t->outbuf.length - orig_len;
617
+ if (s->traced && grpc_endpoint_can_track_err(t->ep)) {
618
+ grpc_core::ContextList::Append(&t->cl, s);
619
+ }
620
+ }
618
621
  if (stream_ctx.stream_became_writable()) {
619
622
  if (!grpc_chttp2_list_add_writing_stream(t, s)) {
620
623
  /* already in writing list: drop ref */
@@ -30,302 +30,229 @@
30
30
  #include "src/core/lib/iomgr/timer.h"
31
31
  #include "src/core/lib/slice/slice_internal.h"
32
32
 
33
- grpc_core::TraceFlag grpc_handshaker_trace(false, "handshaker");
33
+ namespace grpc_core {
34
34
 
35
- //
36
- // grpc_handshaker
37
- //
35
+ TraceFlag grpc_handshaker_trace(false, "handshaker");
38
36
 
39
- void grpc_handshaker_init(const grpc_handshaker_vtable* vtable,
40
- grpc_handshaker* handshaker) {
41
- handshaker->vtable = vtable;
42
- }
43
-
44
- void grpc_handshaker_destroy(grpc_handshaker* handshaker) {
45
- handshaker->vtable->destroy(handshaker);
46
- }
47
-
48
- void grpc_handshaker_shutdown(grpc_handshaker* handshaker, grpc_error* why) {
49
- handshaker->vtable->shutdown(handshaker, why);
50
- }
51
-
52
- void grpc_handshaker_do_handshake(grpc_handshaker* handshaker,
53
- grpc_tcp_server_acceptor* acceptor,
54
- grpc_closure* on_handshake_done,
55
- grpc_handshaker_args* args) {
56
- handshaker->vtable->do_handshake(handshaker, acceptor, on_handshake_done,
57
- args);
58
- }
37
+ namespace {
59
38
 
60
- const char* grpc_handshaker_name(grpc_handshaker* handshaker) {
61
- return handshaker->vtable->name;
39
+ char* HandshakerArgsString(HandshakerArgs* args) {
40
+ char* args_str = grpc_channel_args_string(args->args);
41
+ size_t num_args = args->args != nullptr ? args->args->num_args : 0;
42
+ size_t read_buffer_length =
43
+ args->read_buffer != nullptr ? args->read_buffer->length : 0;
44
+ char* str;
45
+ gpr_asprintf(&str,
46
+ "{endpoint=%p, args=%p {size=%" PRIuPTR
47
+ ": %s}, read_buffer=%p (length=%" PRIuPTR "), exit_early=%d}",
48
+ args->endpoint, args->args, num_args, args_str,
49
+ args->read_buffer, read_buffer_length, args->exit_early);
50
+ gpr_free(args_str);
51
+ return str;
62
52
  }
63
53
 
64
- //
65
- // grpc_handshake_manager
66
- //
54
+ } // namespace
67
55
 
68
- struct grpc_handshake_manager {
69
- gpr_mu mu;
70
- gpr_refcount refs;
71
- bool shutdown;
72
- // An array of handshakers added via grpc_handshake_manager_add().
73
- size_t count;
74
- grpc_handshaker** handshakers;
75
- // The index of the handshaker to invoke next and closure to invoke it.
76
- size_t index;
77
- grpc_closure call_next_handshaker;
78
- // The acceptor to call the handshakers with.
79
- grpc_tcp_server_acceptor* acceptor;
80
- // Deadline timer across all handshakers.
81
- grpc_timer deadline_timer;
82
- grpc_closure on_timeout;
83
- // The final callback and user_data to invoke after the last handshaker.
84
- grpc_closure on_handshake_done;
85
- void* user_data;
86
- // Handshaker args.
87
- grpc_handshaker_args args;
88
- // Links to the previous and next managers in a list of all pending handshakes
89
- // Used at server side only.
90
- grpc_handshake_manager* prev;
91
- grpc_handshake_manager* next;
92
- };
56
+ HandshakeManager::HandshakeManager() { gpr_mu_init(&mu_); }
93
57
 
94
- grpc_handshake_manager* grpc_handshake_manager_create() {
95
- grpc_handshake_manager* mgr = static_cast<grpc_handshake_manager*>(
96
- gpr_zalloc(sizeof(grpc_handshake_manager)));
97
- gpr_mu_init(&mgr->mu);
98
- gpr_ref_init(&mgr->refs, 1);
99
- return mgr;
100
- }
101
-
102
- void grpc_handshake_manager_pending_list_add(grpc_handshake_manager** head,
103
- grpc_handshake_manager* mgr) {
104
- GPR_ASSERT(mgr->prev == nullptr);
105
- GPR_ASSERT(mgr->next == nullptr);
106
- mgr->next = *head;
58
+ /// Add \a mgr to the server side list of all pending handshake managers, the
59
+ /// list starts with \a *head.
60
+ // Not thread-safe. Caller needs to synchronize.
61
+ void HandshakeManager::AddToPendingMgrList(HandshakeManager** head) {
62
+ GPR_ASSERT(prev_ == nullptr);
63
+ GPR_ASSERT(next_ == nullptr);
64
+ next_ = *head;
107
65
  if (*head) {
108
- (*head)->prev = mgr;
66
+ (*head)->prev_ = this;
109
67
  }
110
- *head = mgr;
68
+ *head = this;
111
69
  }
112
70
 
113
- void grpc_handshake_manager_pending_list_remove(grpc_handshake_manager** head,
114
- grpc_handshake_manager* mgr) {
115
- if (mgr->next != nullptr) {
116
- mgr->next->prev = mgr->prev;
71
+ /// Remove \a mgr from the server side list of all pending handshake managers.
72
+ // Not thread-safe. Caller needs to synchronize.
73
+ void HandshakeManager::RemoveFromPendingMgrList(HandshakeManager** head) {
74
+ if (next_ != nullptr) {
75
+ next_->prev_ = prev_;
117
76
  }
118
- if (mgr->prev != nullptr) {
119
- mgr->prev->next = mgr->next;
77
+ if (prev_ != nullptr) {
78
+ prev_->next_ = next_;
120
79
  } else {
121
- GPR_ASSERT(*head == mgr);
122
- *head = mgr->next;
80
+ GPR_ASSERT(*head == this);
81
+ *head = next_;
123
82
  }
124
83
  }
125
84
 
126
- void grpc_handshake_manager_pending_list_shutdown_all(
127
- grpc_handshake_manager* head, grpc_error* why) {
85
+ /// Shutdown all pending handshake managers starting at head on the server
86
+ /// side. Not thread-safe. Caller needs to synchronize.
87
+ void HandshakeManager::ShutdownAllPending(grpc_error* why) {
88
+ auto* head = this;
128
89
  while (head != nullptr) {
129
- grpc_handshake_manager_shutdown(head, GRPC_ERROR_REF(why));
130
- head = head->next;
90
+ head->Shutdown(GRPC_ERROR_REF(why));
91
+ head = head->next_;
131
92
  }
132
93
  GRPC_ERROR_UNREF(why);
133
94
  }
134
95
 
135
- static bool is_power_of_2(size_t n) { return (n & (n - 1)) == 0; }
136
-
137
- void grpc_handshake_manager_add(grpc_handshake_manager* mgr,
138
- grpc_handshaker* handshaker) {
96
+ void HandshakeManager::Add(RefCountedPtr<Handshaker> handshaker) {
139
97
  if (grpc_handshaker_trace.enabled()) {
140
98
  gpr_log(
141
99
  GPR_INFO,
142
100
  "handshake_manager %p: adding handshaker %s [%p] at index %" PRIuPTR,
143
- mgr, grpc_handshaker_name(handshaker), handshaker, mgr->count);
144
- }
145
- gpr_mu_lock(&mgr->mu);
146
- // To avoid allocating memory for each handshaker we add, we double
147
- // the number of elements every time we need more.
148
- size_t realloc_count = 0;
149
- if (mgr->count == 0) {
150
- realloc_count = 2;
151
- } else if (mgr->count >= 2 && is_power_of_2(mgr->count)) {
152
- realloc_count = mgr->count * 2;
153
- }
154
- if (realloc_count > 0) {
155
- mgr->handshakers = static_cast<grpc_handshaker**>(gpr_realloc(
156
- mgr->handshakers, realloc_count * sizeof(grpc_handshaker*)));
101
+ this, handshaker->name(), handshaker.get(), handshakers_.size());
157
102
  }
158
- mgr->handshakers[mgr->count++] = handshaker;
159
- gpr_mu_unlock(&mgr->mu);
103
+ MutexLock lock(&mu_);
104
+ handshakers_.push_back(std::move(handshaker));
160
105
  }
161
106
 
162
- static void grpc_handshake_manager_unref(grpc_handshake_manager* mgr) {
163
- if (gpr_unref(&mgr->refs)) {
164
- for (size_t i = 0; i < mgr->count; ++i) {
165
- grpc_handshaker_destroy(mgr->handshakers[i]);
166
- }
167
- gpr_free(mgr->handshakers);
168
- gpr_mu_destroy(&mgr->mu);
169
- gpr_free(mgr);
170
- }
171
- }
172
-
173
- void grpc_handshake_manager_destroy(grpc_handshake_manager* mgr) {
174
- grpc_handshake_manager_unref(mgr);
107
+ HandshakeManager::~HandshakeManager() {
108
+ handshakers_.clear();
109
+ gpr_mu_destroy(&mu_);
175
110
  }
176
111
 
177
- void grpc_handshake_manager_shutdown(grpc_handshake_manager* mgr,
178
- grpc_error* why) {
179
- gpr_mu_lock(&mgr->mu);
180
- // Shutdown the handshaker that's currently in progress, if any.
181
- if (!mgr->shutdown && mgr->index > 0) {
182
- mgr->shutdown = true;
183
- grpc_handshaker_shutdown(mgr->handshakers[mgr->index - 1],
184
- GRPC_ERROR_REF(why));
112
+ void HandshakeManager::Shutdown(grpc_error* why) {
113
+ {
114
+ MutexLock lock(&mu_);
115
+ // Shutdown the handshaker that's currently in progress, if any.
116
+ if (!is_shutdown_ && index_ > 0) {
117
+ is_shutdown_ = true;
118
+ handshakers_[index_ - 1]->Shutdown(GRPC_ERROR_REF(why));
119
+ }
185
120
  }
186
- gpr_mu_unlock(&mgr->mu);
187
121
  GRPC_ERROR_UNREF(why);
188
122
  }
189
123
 
190
- static char* handshaker_args_string(grpc_handshaker_args* args) {
191
- char* args_str = grpc_channel_args_string(args->args);
192
- size_t num_args = args->args != nullptr ? args->args->num_args : 0;
193
- size_t read_buffer_length =
194
- args->read_buffer != nullptr ? args->read_buffer->length : 0;
195
- char* str;
196
- gpr_asprintf(&str,
197
- "{endpoint=%p, args=%p {size=%" PRIuPTR
198
- ": %s}, read_buffer=%p (length=%" PRIuPTR "), exit_early=%d}",
199
- args->endpoint, args->args, num_args, args_str,
200
- args->read_buffer, read_buffer_length, args->exit_early);
201
- gpr_free(args_str);
202
- return str;
203
- }
204
-
205
124
  // Helper function to call either the next handshaker or the
206
125
  // on_handshake_done callback.
207
126
  // Returns true if we've scheduled the on_handshake_done callback.
208
- static bool call_next_handshaker_locked(grpc_handshake_manager* mgr,
209
- grpc_error* error) {
127
+ bool HandshakeManager::CallNextHandshakerLocked(grpc_error* error) {
210
128
  if (grpc_handshaker_trace.enabled()) {
211
- char* args_str = handshaker_args_string(&mgr->args);
129
+ char* args_str = HandshakerArgsString(&args_);
212
130
  gpr_log(GPR_INFO,
213
131
  "handshake_manager %p: error=%s shutdown=%d index=%" PRIuPTR
214
132
  ", args=%s",
215
- mgr, grpc_error_string(error), mgr->shutdown, mgr->index, args_str);
133
+ this, grpc_error_string(error), is_shutdown_, index_, args_str);
216
134
  gpr_free(args_str);
217
135
  }
218
- GPR_ASSERT(mgr->index <= mgr->count);
136
+ GPR_ASSERT(index_ <= handshakers_.size());
219
137
  // If we got an error or we've been shut down or we're exiting early or
220
138
  // we've finished the last handshaker, invoke the on_handshake_done
221
139
  // callback. Otherwise, call the next handshaker.
222
- if (error != GRPC_ERROR_NONE || mgr->shutdown || mgr->args.exit_early ||
223
- mgr->index == mgr->count) {
224
- if (error == GRPC_ERROR_NONE && mgr->shutdown) {
140
+ if (error != GRPC_ERROR_NONE || is_shutdown_ || args_.exit_early ||
141
+ index_ == handshakers_.size()) {
142
+ if (error == GRPC_ERROR_NONE && is_shutdown_) {
225
143
  error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("handshaker shutdown");
226
144
  // It is possible that the endpoint has already been destroyed by
227
145
  // a shutdown call while this callback was sitting on the ExecCtx
228
146
  // with no error.
229
- if (mgr->args.endpoint != nullptr) {
147
+ if (args_.endpoint != nullptr) {
230
148
  // TODO(roth): It is currently necessary to shutdown endpoints
231
149
  // before destroying then, even when we know that there are no
232
150
  // pending read/write callbacks. This should be fixed, at which
233
151
  // point this can be removed.
234
- grpc_endpoint_shutdown(mgr->args.endpoint, GRPC_ERROR_REF(error));
235
- grpc_endpoint_destroy(mgr->args.endpoint);
236
- mgr->args.endpoint = nullptr;
237
- grpc_channel_args_destroy(mgr->args.args);
238
- mgr->args.args = nullptr;
239
- grpc_slice_buffer_destroy_internal(mgr->args.read_buffer);
240
- gpr_free(mgr->args.read_buffer);
241
- mgr->args.read_buffer = nullptr;
152
+ grpc_endpoint_shutdown(args_.endpoint, GRPC_ERROR_REF(error));
153
+ grpc_endpoint_destroy(args_.endpoint);
154
+ args_.endpoint = nullptr;
155
+ grpc_channel_args_destroy(args_.args);
156
+ args_.args = nullptr;
157
+ grpc_slice_buffer_destroy_internal(args_.read_buffer);
158
+ gpr_free(args_.read_buffer);
159
+ args_.read_buffer = nullptr;
242
160
  }
243
161
  }
244
162
  if (grpc_handshaker_trace.enabled()) {
245
163
  gpr_log(GPR_INFO,
246
164
  "handshake_manager %p: handshaking complete -- scheduling "
247
165
  "on_handshake_done with error=%s",
248
- mgr, grpc_error_string(error));
166
+ this, grpc_error_string(error));
249
167
  }
250
168
  // Cancel deadline timer, since we're invoking the on_handshake_done
251
169
  // callback now.
252
- grpc_timer_cancel(&mgr->deadline_timer);
253
- GRPC_CLOSURE_SCHED(&mgr->on_handshake_done, error);
254
- mgr->shutdown = true;
170
+ grpc_timer_cancel(&deadline_timer_);
171
+ GRPC_CLOSURE_SCHED(&on_handshake_done_, error);
172
+ is_shutdown_ = true;
255
173
  } else {
174
+ auto handshaker = handshakers_[index_];
256
175
  if (grpc_handshaker_trace.enabled()) {
257
176
  gpr_log(
258
177
  GPR_INFO,
259
178
  "handshake_manager %p: calling handshaker %s [%p] at index %" PRIuPTR,
260
- mgr, grpc_handshaker_name(mgr->handshakers[mgr->index]),
261
- mgr->handshakers[mgr->index], mgr->index);
179
+ this, handshaker->name(), handshaker.get(), index_);
262
180
  }
263
- grpc_handshaker_do_handshake(mgr->handshakers[mgr->index], mgr->acceptor,
264
- &mgr->call_next_handshaker, &mgr->args);
181
+ handshaker->DoHandshake(acceptor_, &call_next_handshaker_, &args_);
265
182
  }
266
- ++mgr->index;
267
- return mgr->shutdown;
183
+ ++index_;
184
+ return is_shutdown_;
268
185
  }
269
186
 
270
- // A function used as the handshaker-done callback when chaining
271
- // handshakers together.
272
- static void call_next_handshaker(void* arg, grpc_error* error) {
273
- grpc_handshake_manager* mgr = static_cast<grpc_handshake_manager*>(arg);
274
- gpr_mu_lock(&mgr->mu);
275
- bool done = call_next_handshaker_locked(mgr, GRPC_ERROR_REF(error));
276
- gpr_mu_unlock(&mgr->mu);
187
+ void HandshakeManager::CallNextHandshakerFn(void* arg, grpc_error* error) {
188
+ auto* mgr = static_cast<HandshakeManager*>(arg);
189
+ bool done;
190
+ {
191
+ MutexLock lock(&mgr->mu_);
192
+ done = mgr->CallNextHandshakerLocked(GRPC_ERROR_REF(error));
193
+ }
277
194
  // If we're invoked the final callback, we won't be coming back
278
195
  // to this function, so we can release our reference to the
279
196
  // handshake manager.
280
197
  if (done) {
281
- grpc_handshake_manager_unref(mgr);
198
+ mgr->Unref();
282
199
  }
283
200
  }
284
201
 
285
- // Callback invoked when deadline is exceeded.
286
- static void on_timeout(void* arg, grpc_error* error) {
287
- grpc_handshake_manager* mgr = static_cast<grpc_handshake_manager*>(arg);
288
- if (error == GRPC_ERROR_NONE) { // Timer fired, rather than being cancelled.
289
- grpc_handshake_manager_shutdown(
290
- mgr, GRPC_ERROR_CREATE_FROM_STATIC_STRING("Handshake timed out"));
202
+ void HandshakeManager::OnTimeoutFn(void* arg, grpc_error* error) {
203
+ auto* mgr = static_cast<HandshakeManager*>(arg);
204
+ if (error == GRPC_ERROR_NONE) { // Timer fired, rather than being cancelled
205
+ mgr->Shutdown(GRPC_ERROR_CREATE_FROM_STATIC_STRING("Handshake timed out"));
291
206
  }
292
- grpc_handshake_manager_unref(mgr);
207
+ mgr->Unref();
293
208
  }
294
209
 
295
- void grpc_handshake_manager_do_handshake(grpc_handshake_manager* mgr,
296
- grpc_endpoint* endpoint,
297
- const grpc_channel_args* channel_args,
298
- grpc_millis deadline,
299
- grpc_tcp_server_acceptor* acceptor,
300
- grpc_iomgr_cb_func on_handshake_done,
301
- void* user_data) {
302
- gpr_mu_lock(&mgr->mu);
303
- GPR_ASSERT(mgr->index == 0);
304
- GPR_ASSERT(!mgr->shutdown);
305
- // Construct handshaker args. These will be passed through all
306
- // handshakers and eventually be freed by the on_handshake_done callback.
307
- mgr->args.endpoint = endpoint;
308
- mgr->args.args = grpc_channel_args_copy(channel_args);
309
- mgr->args.user_data = user_data;
310
- mgr->args.read_buffer = static_cast<grpc_slice_buffer*>(
311
- gpr_malloc(sizeof(*mgr->args.read_buffer)));
312
- grpc_slice_buffer_init(mgr->args.read_buffer);
313
- // Initialize state needed for calling handshakers.
314
- mgr->acceptor = acceptor;
315
- GRPC_CLOSURE_INIT(&mgr->call_next_handshaker, call_next_handshaker, mgr,
316
- grpc_schedule_on_exec_ctx);
317
- GRPC_CLOSURE_INIT(&mgr->on_handshake_done, on_handshake_done, &mgr->args,
318
- grpc_schedule_on_exec_ctx);
319
- // Start deadline timer, which owns a ref.
320
- gpr_ref(&mgr->refs);
321
- GRPC_CLOSURE_INIT(&mgr->on_timeout, on_timeout, mgr,
322
- grpc_schedule_on_exec_ctx);
323
- grpc_timer_init(&mgr->deadline_timer, deadline, &mgr->on_timeout);
324
- // Start first handshaker, which also owns a ref.
325
- gpr_ref(&mgr->refs);
326
- bool done = call_next_handshaker_locked(mgr, GRPC_ERROR_NONE);
327
- gpr_mu_unlock(&mgr->mu);
210
+ void HandshakeManager::DoHandshake(grpc_endpoint* endpoint,
211
+ const grpc_channel_args* channel_args,
212
+ grpc_millis deadline,
213
+ grpc_tcp_server_acceptor* acceptor,
214
+ grpc_iomgr_cb_func on_handshake_done,
215
+ void* user_data) {
216
+ bool done;
217
+ {
218
+ MutexLock lock(&mu_);
219
+ GPR_ASSERT(index_ == 0);
220
+ GPR_ASSERT(!is_shutdown_);
221
+ // Construct handshaker args. These will be passed through all
222
+ // handshakers and eventually be freed by the on_handshake_done callback.
223
+ args_.endpoint = endpoint;
224
+ args_.args = grpc_channel_args_copy(channel_args);
225
+ args_.user_data = user_data;
226
+ args_.read_buffer =
227
+ static_cast<grpc_slice_buffer*>(gpr_malloc(sizeof(*args_.read_buffer)));
228
+ grpc_slice_buffer_init(args_.read_buffer);
229
+ // Initialize state needed for calling handshakers.
230
+ acceptor_ = acceptor;
231
+ GRPC_CLOSURE_INIT(&call_next_handshaker_,
232
+ &HandshakeManager::CallNextHandshakerFn, this,
233
+ grpc_schedule_on_exec_ctx);
234
+ GRPC_CLOSURE_INIT(&on_handshake_done_, on_handshake_done, &args_,
235
+ grpc_schedule_on_exec_ctx);
236
+ // Start deadline timer, which owns a ref.
237
+ Ref().release();
238
+ GRPC_CLOSURE_INIT(&on_timeout_, &HandshakeManager::OnTimeoutFn, this,
239
+ grpc_schedule_on_exec_ctx);
240
+ grpc_timer_init(&deadline_timer_, deadline, &on_timeout_);
241
+ // Start first handshaker, which also owns a ref.
242
+ Ref().release();
243
+ done = CallNextHandshakerLocked(GRPC_ERROR_NONE);
244
+ }
328
245
  if (done) {
329
- grpc_handshake_manager_unref(mgr);
246
+ Unref();
330
247
  }
331
248
  }
249
+
250
+ } // namespace grpc_core
251
+
252
+ void grpc_handshake_manager_add(grpc_handshake_manager* mgr,
253
+ grpc_handshaker* handshaker) {
254
+ // This is a transition method to aid the API change for handshakers.
255
+ using namespace grpc_core;
256
+ RefCountedPtr<Handshaker> refd_hs(static_cast<Handshaker*>(handshaker));
257
+ mgr->Add(refd_hs);
258
+ }