binder_connector.cc
Go to the documentation of this file.
1 // Copyright 2021 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 
16 
17 #ifndef GRPC_NO_BINDER
18 
21 
22 #ifdef GRPC_HAVE_UNIX_SOCKET
23 #include <sys/un.h>
24 #endif
25 
26 #include <functional>
27 #include <map>
28 
30 
37 
38 namespace {
39 
40 // TODO(mingcl): Currently this does no error handling and assumes the
41 // connection always succeeds in reasonable amount of time.
42 class BinderConnector : public grpc_core::SubchannelConnector {
43  public:
44  BinderConnector() {}
45  ~BinderConnector() override {}
46  void Connect(const Args& args, Result* result,
47  grpc_closure* notify) override {
48 #ifdef GRPC_HAVE_UNIX_SOCKET
49  {
50  struct sockaddr_un* un =
51  reinterpret_cast<struct sockaddr_un*>(args.address->addr);
52  // length of identifier, including null terminator
53  size_t id_length = args.address->len - sizeof(un->sun_family);
54  // The c-style string at least will have a null terminator, and the
55  // connection id itself should not be empty
56  GPR_ASSERT(id_length >= 2);
57  // Make sure there is null terminator at the expected location before
58  // reading from it
59  GPR_ASSERT(un->sun_path[id_length - 1] == '\0');
60  conn_id_ = un->sun_path;
61  }
62 #else
63  GPR_ASSERT(0);
64 #endif
65  gpr_log(GPR_INFO, "BinderConnector %p conn_id_ = %s", this,
66  conn_id_.c_str());
67 
68  args_ = args;
69  GPR_ASSERT(notify_ == nullptr);
70  GPR_ASSERT(notify != nullptr);
71  notify_ = notify;
72  result_ = result;
73 
74  Ref().release(); // Ref held by the following callback
75 
77  conn_id_,
78  std::bind(&BinderConnector::OnConnected, this, std::placeholders::_1));
79  }
80 
81  void OnConnected(std::unique_ptr<grpc_binder::Binder> endpoint_binder) {
82  GPR_ASSERT(endpoint_binder != nullptr);
84  std::move(endpoint_binder),
86  GPR_ASSERT(transport != nullptr);
87  result_->channel_args = grpc_channel_args_copy(args_.channel_args);
88  result_->transport = transport;
89 
90  GPR_ASSERT(notify_ != nullptr);
91  // ExecCtx is required here for running grpc_closure because this callback
92  // might be invoked from non-gRPC code
93  if (grpc_core::ExecCtx::Get() == nullptr) {
96  } else {
98  }
99 
100  Unref(); // Was referenced in BinderConnector::Connect
101  }
102  void Shutdown(grpc_error_handle error) override { (void)error; }
103 
104  private:
105  Args args_;
106  grpc_closure* notify_ = nullptr;
107  Result* result_ = nullptr;
108 
109  std::string conn_id_;
110 };
111 
112 } // namespace
113 
114 namespace grpc_core {
115 
117  const grpc_resolved_address& address, const grpc_channel_args* args) {
118  gpr_log(GPR_INFO, "BinderClientChannelFactory creating subchannel %p", this);
119  grpc_arg default_authority_arg = grpc_channel_arg_string_create(
120  const_cast<char*>(GRPC_ARG_DEFAULT_AUTHORITY),
121  const_cast<char*>("binder.authority"));
122  grpc_channel_args* new_args =
123  grpc_channel_args_copy_and_add(args, &default_authority_arg, 1);
124 
126  Subchannel::Create(MakeOrphanable<BinderConnector>(), address, new_args);
127 
128  return s;
129 }
130 
131 } // namespace grpc_core
132 
133 #endif // GRPC_NO_BINDER
grpc_arg
Definition: grpc_types.h:103
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
connector.h
GRPC_ERROR_NONE
#define GRPC_ERROR_NONE
Definition: error.h:234
security_policy_setting.h
binder.h
subchannel.h
grpc_core::InternallyRefCounted< SubchannelConnector >::Unref
void Unref()
Definition: orphanable.h:100
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::SubchannelConnector
Definition: connector.h:39
grpc_core::SubchannelConnector::Shutdown
virtual void Shutdown(grpc_error_handle error)=0
grpc_binder::EndpointBinderPool::GetEndpointBinder
void GetEndpointBinder(std::string conn_id, std::function< void(std::unique_ptr< grpc_binder::Binder >)> cb)
Definition: endpoint_binder_pool.cc:56
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_resolved_address
Definition: resolved_address.h:34
grpc_channel_arg_string_create
grpc_arg grpc_channel_arg_string_create(char *name, char *value)
Definition: channel_args.cc:476
args_
grpc_channel_args * args_
Definition: grpclb.cc:513
grpc_create_binder_transport_client
grpc_transport * grpc_create_binder_transport_client(std::unique_ptr< grpc_binder::Binder > endpoint_binder, std::shared_ptr< grpc::experimental::binder::SecurityPolicy > security_policy)
Definition: binder_transport.cc:737
grpc_channel_args
Definition: grpc_types.h:132
gen_build_yaml.struct
def struct(**kwargs)
Definition: test/core/end2end/gen_build_yaml.py:30
absl::synchronization_internal::Get
static GraphId Get(const IdMap &id, int num)
Definition: abseil-cpp/absl/synchronization/internal/graphcycles_test.cc:44
DEBUG_LOCATION
#define DEBUG_LOCATION
Definition: debug_location.h:41
GRPC_ARG_DEFAULT_AUTHORITY
#define GRPC_ARG_DEFAULT_AUTHORITY
Definition: grpc_types.h:251
binder_security_policy.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
endpoint_binder_pool.h
hpack_encoder_fixtures::Args
Args({0, 16384})
re2::Result
TestInstance::Result Result
Definition: bloaty/third_party/re2/re2/testing/tester.cc:96
grpc_binder::GetEndpointBinderPool
EndpointBinderPool * GetEndpointBinderPool()
Definition: endpoint_binder_pool.cc:108
grpc_core::InternallyRefCounted< SubchannelConnector >::Ref
RefCountedPtr< SubchannelConnector > Ref() GRPC_MUST_USE_RESULT
Definition: orphanable.h:90
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
transport
grpc_transport transport
Definition: filter_fuzzer.cc:146
grpc_channel_args_copy
grpc_channel_args * grpc_channel_args_copy(const grpc_channel_args *src)
Definition: channel_args.cc:285
grpc_binder::GetSecurityPolicySetting
SecurityPolicySetting * GetSecurityPolicySetting()
Definition: security_policy_setting.cc:39
grpc_core::Subchannel::Create
static RefCountedPtr< Subchannel > Create(OrphanablePtr< SubchannelConnector > connector, const grpc_resolved_address &address, const grpc_channel_args *args)
Definition: subchannel.cc:712
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::SubchannelConnector::Connect
virtual void Connect(const Args &args, Result *result, grpc_closure *notify)=0
port.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
grpc_transport
Definition: transport_impl.h:89
binder_connector.h
grpc_core::BinderClientChannelFactory::CreateSubchannel
RefCountedPtr< Subchannel > CreateSubchannel(const grpc_resolved_address &address, const grpc_channel_args *args) override
Definition: binder_connector.cc:116
grpc_error
Definition: error_internal.h:42
grpc_closure
Definition: closure.h:56
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
grpc_channel_args_copy_and_add
grpc_channel_args * grpc_channel_args_copy_and_add(const grpc_channel_args *src, const grpc_arg *to_add, size_t num_to_add)
Definition: channel_args.cc:224
binder_transport.h
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:48