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 
15 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_TRANSPORT_BINDER_TRANSPORT_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_TRANSPORT_BINDER_TRANSPORT_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <atomic>
21 #include <memory>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 #include "absl/container/flat_hash_map.h"
27 
28 #include <grpc/support/log.h>
29 #include <grpcpp/security/binder_security_policy.h>
30 
31 #include "src/core/ext/transport/binder/utils/transport_stream_receiver.h"
32 #include "src/core/ext/transport/binder/wire_format/binder.h"
33 #include "src/core/ext/transport/binder/wire_format/wire_reader.h"
34 #include "src/core/ext/transport/binder/wire_format/wire_writer.h"
35 #include "src/core/lib/gprpp/crash.h"
36 #include "src/core/lib/iomgr/combiner.h"
37 #include "src/core/lib/transport/transport.h"
38 #include "src/core/lib/transport/transport_impl.h"
39 
40 struct grpc_binder_stream;
41 
42 // TODO(mingcl): Consider putting the struct in a namespace (Eventually this
43 // depends on what style we want to follow)
44 // TODO(mingcl): Decide casing for this class name. Should we use C-style class
45 // name here or just go with C++ style?
46 struct grpc_binder_transport {
47   explicit grpc_binder_transport(
48       std::unique_ptr<grpc_binder::Binder> binder, bool is_client,
49       std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
50           security_policy);
51   ~grpc_binder_transport();
52 
NewStreamTxCodegrpc_binder_transport53   int NewStreamTxCode() {
54     // TODO(mingcl): Wrap around when all tx codes are used. "If we do detect a
55     // collision however, we will fail the new call with UNAVAILABLE, and shut
56     // down the transport gracefully."
57     GPR_ASSERT(next_free_tx_code <= LAST_CALL_TRANSACTION);
58     return next_free_tx_code++;
59   }
60 
61   grpc_transport base;  // must be first
62 
63   std::shared_ptr<grpc_binder::TransportStreamReceiver>
64       transport_stream_receiver;
65   grpc_core::OrphanablePtr<grpc_binder::WireReader> wire_reader;
66   std::shared_ptr<grpc_binder::WireWriter> wire_writer;
67 
68   bool is_client;
69   // A set of currently registered streams (the key is the stream ID).
70   absl::flat_hash_map<int, grpc_binder_stream*> registered_stream;
71   grpc_core::Combiner* combiner;
72 
73   // The callback and the data for the callback when the stream is connected
74   // between client and server.
75   void (*accept_stream_fn)(void* user_data, grpc_transport* transport,
76                            const void* server_data) = nullptr;
77   void* accept_stream_user_data = nullptr;
78   // `accept_stream_locked()` could be called before `accept_stream_fn` has been
79   // set, we need to remember those requests that comes too early and call them
80   // later when we can.
81   int accept_stream_fn_called_count_{0};
82 
83   grpc_core::ConnectivityStateTracker state_tracker;
84   grpc_core::RefCount refs;
85 
86  private:
87   std::atomic<int> next_free_tx_code{grpc_binder::kFirstCallId};
88 };
89 
90 grpc_transport* grpc_create_binder_transport_client(
91     std::unique_ptr<grpc_binder::Binder> endpoint_binder,
92     std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
93         security_policy);
94 grpc_transport* grpc_create_binder_transport_server(
95     std::unique_ptr<grpc_binder::Binder> client_binder,
96     std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
97         security_policy);
98 
99 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_TRANSPORT_BINDER_TRANSPORT_H
100