forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSocketRequester.cpp
More file actions
181 lines (156 loc) · 6.4 KB
/
RSocketRequester.cpp
File metadata and controls
181 lines (156 loc) · 6.4 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
// 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/RSocketRequester.h"
#include <folly/ExceptionWrapper.h>
#include "rsocket/internal/ScheduledSingleObserver.h"
#include "rsocket/internal/ScheduledSubscriber.h"
#include "yarpl/Flowable.h"
#include "yarpl/single/SingleSubscriptions.h"
using namespace folly;
namespace rsocket {
namespace {
template <class Fn>
void runOnCorrectThread(folly::EventBase& evb, Fn fn) {
if (evb.isInEventBaseThread()) {
fn();
} else {
evb.runInEventBaseThread(std::move(fn));
}
}
} // namespace
RSocketRequester::RSocketRequester(
std::shared_ptr<RSocketStateMachine> srs,
EventBase& eventBase)
: stateMachine_{std::move(srs)}, eventBase_{&eventBase} {}
RSocketRequester::~RSocketRequester() {
VLOG(1) << "Destroying RSocketRequester";
}
void RSocketRequester::closeSocket() {
eventBase_->runInEventBaseThread([stateMachine = std::move(stateMachine_)] {
VLOG(2) << "Closing RSocketStateMachine on EventBase";
stateMachine->close({}, StreamCompletionSignal::SOCKET_CLOSED);
});
}
std::shared_ptr<yarpl::flowable::Flowable<rsocket::Payload>>
RSocketRequester::requestChannel(
std::shared_ptr<yarpl::flowable::Flowable<rsocket::Payload>>
requestStream) {
return requestChannel({}, false, std::move(requestStream));
}
std::shared_ptr<yarpl::flowable::Flowable<rsocket::Payload>>
RSocketRequester::requestChannel(
Payload request,
std::shared_ptr<yarpl::flowable::Flowable<rsocket::Payload>>
requestStream) {
return requestChannel(std::move(request), true, std::move(requestStream));
}
std::shared_ptr<yarpl::flowable::Flowable<rsocket::Payload>>
RSocketRequester::requestChannel(
Payload request,
bool hasInitialRequest,
std::shared_ptr<yarpl::flowable::Flowable<rsocket::Payload>>
requestStreamFlowable) {
CHECK(stateMachine_);
return yarpl::flowable::internal::flowableFromSubscriber<Payload>(
[eb = eventBase_,
req = std::move(request),
hasInitialRequest,
requestStream = std::move(requestStreamFlowable),
srs = stateMachine_](
std::shared_ptr<yarpl::flowable::Subscriber<Payload>> subscriber) {
auto lambda = [eb,
r = req.clone(),
hasInitialRequest,
requestStream,
srs,
subs = std::move(subscriber)]() mutable {
auto scheduled =
std::make_shared<ScheduledSubscriptionSubscriber<Payload>>(
std::move(subs), *eb);
auto responseSink = srs->requestChannel(
std::move(r), hasInitialRequest, std::move(scheduled));
// responseSink is wrapped with thread scheduling
// so all emissions happen on the right thread.
// If we don't get a responseSink back, that means that
// the requesting peer wasn't connected (or similar error)
// and the Flowable it gets back will immediately call onError.
if (responseSink) {
auto scheduledResponse =
std::make_shared<ScheduledSubscriber<Payload>>(
std::move(responseSink), *eb);
requestStream->subscribe(std::move(scheduledResponse));
}
};
runOnCorrectThread(*eb, std::move(lambda));
});
}
std::shared_ptr<yarpl::flowable::Flowable<Payload>>
RSocketRequester::requestStream(Payload request) {
CHECK(stateMachine_);
return yarpl::flowable::internal::flowableFromSubscriber<Payload>(
[eb = eventBase_, req = std::move(request), srs = stateMachine_](
std::shared_ptr<yarpl::flowable::Subscriber<Payload>> subscriber) {
auto lambda =
[eb, r = req.clone(), srs, subs = std::move(subscriber)]() mutable {
auto scheduled =
std::make_shared<ScheduledSubscriptionSubscriber<Payload>>(
std::move(subs), *eb);
srs->requestStream(std::move(r), std::move(scheduled));
};
runOnCorrectThread(*eb, std::move(lambda));
});
}
std::shared_ptr<yarpl::single::Single<rsocket::Payload>>
RSocketRequester::requestResponse(Payload request) {
CHECK(stateMachine_);
return yarpl::single::Single<Payload>::create(
[eb = eventBase_, req = std::move(request), srs = stateMachine_](
std::shared_ptr<yarpl::single::SingleObserver<Payload>> observer) {
auto lambda = [eb,
r = req.clone(),
srs,
obs = std::move(observer)]() mutable {
auto scheduled =
std::make_shared<ScheduledSubscriptionSingleObserver<Payload>>(
std::move(obs), *eb);
srs->requestResponse(std::move(r), std::move(scheduled));
};
runOnCorrectThread(*eb, std::move(lambda));
});
}
std::shared_ptr<yarpl::single::Single<void>> RSocketRequester::fireAndForget(
rsocket::Payload request) {
CHECK(stateMachine_);
return yarpl::single::Single<void>::create(
[eb = eventBase_, req = std::move(request), srs = stateMachine_](
std::shared_ptr<yarpl::single::SingleObserverBase<void>> subscriber) {
auto lambda =
[r = req.clone(), srs, subs = std::move(subscriber)]() mutable {
// TODO: Pass in SingleSubscriber for underlying layers to call
// onSuccess/onError once put on network.
srs->fireAndForget(std::move(r));
subs->onSubscribe(yarpl::single::SingleSubscriptions::empty());
subs->onSuccess();
};
runOnCorrectThread(*eb, std::move(lambda));
});
}
void RSocketRequester::metadataPush(std::unique_ptr<folly::IOBuf> metadata) {
CHECK(stateMachine_);
runOnCorrectThread(
*eventBase_, [srs = stateMachine_, meta = std::move(metadata)]() mutable {
srs->metadataPush(std::move(meta));
});
}
} // namespace rsocket