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_UTILS_TRANSPORT_STREAM_RECEIVER_H
16 #define GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_UTILS_TRANSPORT_STREAM_RECEIVER_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <functional>
21 #include <string>
22 #include <vector>
23 
24 #include "absl/status/statusor.h"
25 
26 #include "src/core/ext/transport/binder/wire_format/transaction.h"
27 
28 namespace grpc_binder {
29 
30 typedef int StreamIdentifier;
31 
32 class TransportStreamReceiver {
33  public:
34   virtual ~TransportStreamReceiver() = default;
35 
36   using InitialMetadataCallbackType =
37       std::function<void(absl::StatusOr<Metadata>)>;
38   using MessageDataCallbackType =
39       std::function<void(absl::StatusOr<std::string>)>;
40   using TrailingMetadataCallbackType =
41       std::function<void(absl::StatusOr<Metadata>, int)>;
42 
43   // Only handles single time invocation. Callback object will be deleted.
44   // The callback should be valid until invocation or unregister.
45   virtual void RegisterRecvInitialMetadata(StreamIdentifier id,
46                                            InitialMetadataCallbackType cb) = 0;
47   virtual void RegisterRecvMessage(StreamIdentifier id,
48                                    MessageDataCallbackType cb) = 0;
49   virtual void RegisterRecvTrailingMetadata(
50       StreamIdentifier id, TrailingMetadataCallbackType cb) = 0;
51 
52   // For the following functions, the second arguments are the transaction
53   // result received from the lower level. If it is None, that means there's
54   // something wrong when receiving the corresponding transaction. In such case,
55   // we should cancel the gRPC callback as well.
56   virtual void NotifyRecvInitialMetadata(
57       StreamIdentifier id, absl::StatusOr<Metadata> initial_metadata) = 0;
58   virtual void NotifyRecvMessage(StreamIdentifier id,
59                                  absl::StatusOr<std::string> message) = 0;
60   virtual void NotifyRecvTrailingMetadata(
61       StreamIdentifier id, absl::StatusOr<Metadata> trailing_metadata,
62       int status) = 0;
63   // Remove all entries associated with stream number `id`.
64   virtual void CancelStream(StreamIdentifier id) = 0;
65 
66   static const absl::string_view kGrpcBinderTransportCancelledGracefully;
67 };
68 
69 }  // namespace grpc_binder
70 
71 #endif  // GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_UTILS_TRANSPORT_STREAM_RECEIVER_H
72