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_STREAM_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_TRANSPORT_BINDER_STREAM_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include "src/core/ext/transport/binder/transport/binder_transport.h"
21 
22 struct RecvInitialMetadataArgs {
23   grpc_binder_stream* gbs;
24   grpc_binder_transport* gbt;
25   int tx_code;
26   absl::StatusOr<grpc_binder::Metadata> initial_metadata;
27 };
28 
29 struct RecvMessageArgs {
30   grpc_binder_stream* gbs;
31   grpc_binder_transport* gbt;
32   int tx_code;
33   absl::StatusOr<std::string> message;
34 };
35 
36 struct RecvTrailingMetadataArgs {
37   grpc_binder_stream* gbs;
38   grpc_binder_transport* gbt;
39   int tx_code;
40   absl::StatusOr<grpc_binder::Metadata> trailing_metadata;
41   int status;
42 };
43 
44 struct RegisterStreamArgs {
45   grpc_binder_stream* gbs;
46   grpc_binder_transport* gbt;
47 };
48 
49 // TODO(mingcl): Figure out if we want to use class instead of struct here
50 struct grpc_binder_stream {
51   // server_data will be null for client, and for server it will be whatever
52   // passed in to the accept_stream_fn callback by client.
grpc_binder_streamgrpc_binder_stream53   grpc_binder_stream(grpc_binder_transport* t, grpc_stream_refcount* refcount,
54                      const void* /*server_data*/, grpc_core::Arena* arena,
55                      int tx_code, bool is_client)
56       : t(t),
57         refcount(refcount),
58         arena(arena),
59         tx_code(tx_code),
60         is_client(is_client),
61         is_closed(false) {
62     recv_initial_metadata_args.gbs = this;
63     recv_initial_metadata_args.gbt = t;
64     recv_message_args.gbs = this;
65     recv_message_args.gbt = t;
66     recv_trailing_metadata_args.gbs = this;
67     recv_trailing_metadata_args.gbt = t;
68   }
69 
~grpc_binder_streamgrpc_binder_stream70   ~grpc_binder_stream() {
71     if (destroy_stream_then_closure != nullptr) {
72       grpc_core::ExecCtx::Run(DEBUG_LOCATION, destroy_stream_then_closure,
73                               absl::OkStatus());
74     }
75   }
76 
GetTxCodegrpc_binder_stream77   int GetTxCode() const { return tx_code; }
78 
79   grpc_binder_transport* t;
80   grpc_stream_refcount* refcount;
81   grpc_core::Arena* arena;
82   int tx_code;
83   const bool is_client;
84   bool is_closed;
85 
86   grpc_closure* destroy_stream_then_closure = nullptr;
87   grpc_closure destroy_stream;
88 
89   // The reason why this stream is cancelled and closed.
90   grpc_error_handle cancel_self_error;
91 
92   grpc_closure recv_initial_metadata_closure;
93   RecvInitialMetadataArgs recv_initial_metadata_args;
94   grpc_closure recv_message_closure;
95   RecvMessageArgs recv_message_args;
96   grpc_closure recv_trailing_metadata_closure;
97   RecvTrailingMetadataArgs recv_trailing_metadata_args;
98 
99   grpc_closure register_stream_closure;
100   RegisterStreamArgs register_stream_args;
101 
102   // We store these fields passed from op batch, in order to access them through
103   // grpc_binder_stream
104   grpc_metadata_batch* recv_initial_metadata;
105   grpc_closure* recv_initial_metadata_ready = nullptr;
106   bool* trailing_metadata_available = nullptr;
107   absl::optional<grpc_core::SliceBuffer>* recv_message;
108   grpc_closure* recv_message_ready = nullptr;
109   bool* call_failed_before_recv_message = nullptr;
110   grpc_metadata_batch* recv_trailing_metadata;
111   grpc_closure* recv_trailing_metadata_finished = nullptr;
112 
113   bool trailing_metadata_sent = false;
114   bool need_to_call_trailing_metadata_callback = false;
115 };
116 
117 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_TRANSPORT_BINDER_STREAM_H
118