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
170 lines (150 loc) · 5.4 KB
/
RSocketRequester.cpp
File metadata and controls
170 lines (150 loc) · 5.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
// Copyright 2004-present Facebook. All Rights Reserved.
#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;
using namespace yarpl;
namespace rsocket {
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_->add([stateMachine = std::move(stateMachine_)] {
VLOG(2) << "Closing RSocketStateMachine on EventBase";
stateMachine->close(
folly::exception_wrapper(), StreamCompletionSignal::SOCKET_CLOSED);
});
}
yarpl::Reference<yarpl::flowable::Flowable<rsocket::Payload>>
RSocketRequester::requestChannel(
yarpl::Reference<yarpl::flowable::Flowable<rsocket::Payload>>
requestStream) {
CHECK(stateMachine_); // verify the socket was not closed
return yarpl::flowable::Flowables::fromPublisher<Payload>([
eb = eventBase_,
requestStream = std::move(requestStream),
srs = stateMachine_
](yarpl::Reference<yarpl::flowable::Subscriber<Payload>> subscriber) mutable {
auto lambda = [
requestStream = std::move(requestStream),
subscriber = std::move(subscriber),
srs = std::move(srs),
eb
]() mutable {
auto responseSink = srs->streamsFactory().createChannelRequester(
yarpl::make_ref<ScheduledSubscriptionSubscriber<Payload>>(
std::move(subscriber), *eb));
// 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) {
requestStream->subscribe(yarpl::make_ref<ScheduledSubscriber<Payload>>(
std::move(responseSink), *eb));
}
};
if (eb->isInEventBaseThread()) {
lambda();
} else {
eb->runInEventBaseThread(std::move(lambda));
}
});
}
yarpl::Reference<yarpl::flowable::Flowable<Payload>>
RSocketRequester::requestStream(Payload request) {
CHECK(stateMachine_); // verify the socket was not closed
return yarpl::flowable::Flowables::fromPublisher<Payload>([
eb = eventBase_,
request = std::move(request),
srs = stateMachine_
](yarpl::Reference<yarpl::flowable::Subscriber<Payload>> subscriber) mutable {
auto lambda = [
request = std::move(request),
subscriber = std::move(subscriber),
srs = std::move(srs),
eb
]() mutable {
srs->streamsFactory().createStreamRequester(
std::move(request),
yarpl::make_ref<ScheduledSubscriptionSubscriber<Payload>>(
std::move(subscriber), *eb));
};
if (eb->isInEventBaseThread()) {
lambda();
} else {
eb->runInEventBaseThread(std::move(lambda));
}
});
}
yarpl::Reference<yarpl::single::Single<rsocket::Payload>>
RSocketRequester::requestResponse(Payload request) {
CHECK(stateMachine_); // verify the socket was not closed
return yarpl::single::Single<Payload>::create([
eb = eventBase_,
request = std::move(request),
srs = stateMachine_
](yarpl::Reference<yarpl::single::SingleObserver<Payload>> observer) mutable {
auto lambda = [
request = std::move(request),
observer = std::move(observer),
eb,
srs = std::move(srs)
]() mutable {
srs->streamsFactory().createRequestResponseRequester(
std::move(request),
yarpl::make_ref<ScheduledSubscriptionSingleObserver<Payload>>(
std::move(observer), *eb));
};
if (eb->isInEventBaseThread()) {
lambda();
} else {
eb->runInEventBaseThread(std::move(lambda));
}
});
}
yarpl::Reference<yarpl::single::Single<void>> RSocketRequester::fireAndForget(
rsocket::Payload request) {
CHECK(stateMachine_); // verify the socket was not closed
return yarpl::single::Single<void>::create([
eb = eventBase_,
request = std::move(request),
srs = stateMachine_
](yarpl::Reference<yarpl::single::SingleObserverBase<void>> subscriber) mutable {
auto lambda = [
request = std::move(request),
subscriber = std::move(subscriber),
srs = std::move(srs)
]() mutable {
// TODO pass in SingleSubscriber for underlying layers to
// call onSuccess/onError once put on network
srs->fireAndForget(std::move(request));
// right now just immediately call onSuccess
subscriber->onSubscribe(yarpl::single::SingleSubscriptions::empty());
subscriber->onSuccess();
};
if (eb->isInEventBaseThread()) {
lambda();
} else {
eb->runInEventBaseThread(std::move(lambda));
}
});
}
void RSocketRequester::metadataPush(std::unique_ptr<folly::IOBuf> metadata) {
CHECK(stateMachine_); // verify the socket was not closed
eventBase_->runInEventBaseThread(
[ srs = stateMachine_, metadata = std::move(metadata) ]() mutable {
srs->metadataPush(std::move(metadata));
});
}
DuplexConnection* RSocketRequester::getConnection() {
return stateMachine_? stateMachine_->getConnection() : nullptr;
}
} // namespace rsocket