forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSocketResponder.cpp
More file actions
200 lines (172 loc) · 6.38 KB
/
RSocketResponder.cpp
File metadata and controls
200 lines (172 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (c) Facebook, Inc. and its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "rsocket/RSocketResponder.h"
#include <folly/io/async/EventBase.h>
#include <yarpl/flowable/CancelingSubscriber.h>
namespace rsocket {
using namespace yarpl::flowable;
using namespace yarpl::single;
void RSocketResponderCore::handleRequestStream(
Payload,
StreamId,
std::shared_ptr<Subscriber<Payload>> response) noexcept {
response->onSubscribe(Subscription::create());
response->onError(std::logic_error("handleRequestStream not implemented"));
}
void RSocketResponderCore::handleRequestResponse(
Payload,
StreamId,
std::shared_ptr<SingleObserver<Payload>> responseObserver) noexcept {
responseObserver->onSubscribe(SingleSubscriptions::empty());
responseObserver->onError(
std::logic_error("handleRequestResponse not implemented"));
}
void RSocketResponderCore::handleFireAndForget(Payload, StreamId) {
// No default implementation, no error response to provide.
}
void RSocketResponderCore::handleMetadataPush(std::unique_ptr<folly::IOBuf>) {
// No default implementation, no error response to provide.
}
std::shared_ptr<Subscriber<Payload>> RSocketResponderCore::handleRequestChannel(
Payload,
StreamId,
std::shared_ptr<Subscriber<Payload>> response) noexcept {
response->onSubscribe(Subscription::create());
response->onError(std::logic_error("handleRequestStream not implemented"));
// cancel immediately
return std::make_shared<CancelingSubscriber<Payload>>();
}
std::shared_ptr<Single<Payload>> RSocketResponder::handleRequestResponse(
Payload,
StreamId) {
return Singles::error<Payload>(
std::logic_error("handleRequestResponse not implemented"));
}
std::shared_ptr<Flowable<Payload>> RSocketResponder::handleRequestStream(
Payload,
StreamId) {
return Flowable<Payload>::error(
std::logic_error("handleRequestStream not implemented"));
}
std::shared_ptr<Flowable<Payload>> RSocketResponder::handleRequestChannel(
Payload,
std::shared_ptr<Flowable<Payload>>,
StreamId) {
return Flowable<Payload>::error(
std::logic_error("handleRequestChannel not implemented"));
}
void RSocketResponder::handleFireAndForget(Payload, StreamId) {
// No default implementation, no error response to provide.
}
void RSocketResponder::handleMetadataPush(std::unique_ptr<folly::IOBuf>) {
// No default implementation, no error response to provide.
}
/// Handles a new Channel requested by the other end.
std::shared_ptr<Subscriber<Payload>>
RSocketResponderAdapter::handleRequestChannel(
Payload request,
StreamId streamId,
std::shared_ptr<Subscriber<Payload>> response) noexcept {
class EagerSubscriberBridge : public Subscriber<Payload> {
public:
void onSubscribe(
std::shared_ptr<Subscription> subscription) noexcept override {
CHECK(!subscription_);
subscription_ = std::move(subscription);
if (inner_) {
inner_->onSubscribe(subscription_);
}
}
void onNext(Payload element) noexcept override {
DCHECK(inner_);
inner_->onNext(std::move(element));
}
void onComplete() noexcept override {
if (auto inner = std::move(inner_)) {
inner->onComplete();
subscription_.reset();
} else {
completed_ = true;
}
}
void onError(folly::exception_wrapper ex) noexcept override {
VLOG(3) << "handleRequestChannelCore::onError: " << ex.what();
if (auto inner = std::move(inner_)) {
inner->onError(std::move(ex));
subscription_.reset();
} else {
error_ = std::move(ex);
}
}
void subscribe(std::shared_ptr<Subscriber<Payload>> inner) {
CHECK(!inner_); // only one call to subscribe is supported
CHECK(inner);
inner_ = std::move(inner);
if (subscription_) {
inner_->onSubscribe(subscription_);
// it's possible to get an error or completion before subscribe happens,
// delay sending it but send it when this class gets subscribed
if (completed_) {
onComplete();
} else if (error_) {
onError(std::move(error_));
}
}
}
private:
std::shared_ptr<Subscriber<Payload>> inner_;
std::shared_ptr<Subscription> subscription_;
folly::exception_wrapper error_;
bool completed_{false};
};
auto eagerSubscriber = std::make_shared<EagerSubscriberBridge>();
auto flowable = inner_->handleRequestChannel(
std::move(request),
internal::flowableFromSubscriber<Payload>(
[eagerSubscriber](std::shared_ptr<Subscriber<Payload>> subscriber) {
eagerSubscriber->subscribe(subscriber);
}),
std::move(streamId));
// bridge from the existing eager RequestHandler and old Subscriber type
// to the lazy Flowable and new Subscriber type
flowable->subscribe(std::move(response));
return eagerSubscriber;
}
/// Handles a new Stream requested by the other end.
void RSocketResponderAdapter::handleRequestStream(
Payload request,
StreamId streamId,
std::shared_ptr<Subscriber<Payload>> response) noexcept {
auto flowable =
inner_->handleRequestStream(std::move(request), std::move(streamId));
flowable->subscribe(std::move(response));
}
/// Handles a new inbound RequestResponse requested by the other end.
void RSocketResponderAdapter::handleRequestResponse(
Payload request,
StreamId streamId,
std::shared_ptr<SingleObserver<Payload>> responseObserver) noexcept {
auto single = inner_->handleRequestResponse(std::move(request), streamId);
single->subscribe(std::move(responseObserver));
}
void RSocketResponderAdapter::handleFireAndForget(
Payload request,
StreamId streamId) {
inner_->handleFireAndForget(std::move(request), streamId);
}
void RSocketResponderAdapter::handleMetadataPush(
std::unique_ptr<folly::IOBuf> buf) {
inner_->handleMetadataPush(std::move(buf));
}
} // namespace rsocket