Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
268 lines (237 loc) · 9.45 KB

File metadata and controls

268 lines (237 loc) · 9.45 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// 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/RSocketServer.h"
#include <folly/io/async/EventBaseManager.h>
#include <rsocket/internal/ScheduledRSocketResponder.h>
#include "rsocket/RSocketErrors.h"
#include "rsocket/RSocketStats.h"
#include "rsocket/framing/FramedDuplexConnection.h"
#include "rsocket/framing/ScheduledFrameTransport.h"
#include "rsocket/internal/ConnectionSet.h"
#include "rsocket/internal/WarmResumeManager.h"
namespace rsocket {
RSocketServer::RSocketServer(
std::unique_ptr<ConnectionAcceptor> connectionAcceptor,
std::shared_ptr<RSocketStats> stats)
: duplexConnectionAcceptor_(std::move(connectionAcceptor)),
setupResumeAcceptors_([] {
return new rsocket::SetupResumeAcceptor{
folly::EventBaseManager::get()->getExistingEventBase()};
}),
connectionSet_(std::make_unique<ConnectionSet>()),
stats_(std::move(stats)) {}
RSocketServer::~RSocketServer() {
VLOG(3) << "~RSocketServer ..";
shutdownAndWait();
}
void RSocketServer::shutdownAndWait() {
if (isShutdown_) {
return;
}
// Will stop forwarding connections from duplexConnectionAcceptor_ to
// setupResumeAcceptors_
isShutdown_ = true;
// Stop accepting new connections.
if (duplexConnectionAcceptor_) {
duplexConnectionAcceptor_->stop();
}
std::vector<folly::Future<folly::Unit>> closingFutures;
for (auto& acceptor : setupResumeAcceptors_.accessAllThreads()) {
// This call will queue up the cleanup on the eventBase.
closingFutures.push_back(acceptor.close());
}
folly::collectAll(closingFutures).get();
// Close off all outstanding connections.
connectionSet_->shutdownAndWait();
}
void RSocketServer::start(
std::shared_ptr<RSocketServiceHandler> serviceHandler) {
CHECK(duplexConnectionAcceptor_); // RSocketServer has to be initialized with
// the acceptor
if (started) {
throw std::runtime_error("RSocketServer::start() already called.");
}
started = true;
duplexConnectionAcceptor_->start(
[this, serviceHandler](
std::unique_ptr<DuplexConnection> connection,
folly::EventBase& eventBase) {
acceptConnection(std::move(connection), eventBase, serviceHandler);
});
}
void RSocketServer::start(OnNewSetupFn onNewSetupFn) {
start(RSocketServiceHandler::create(std::move(onNewSetupFn)));
}
void RSocketServer::startAndPark(OnNewSetupFn onNewSetupFn) {
startAndPark(RSocketServiceHandler::create(std::move(onNewSetupFn)));
}
void RSocketServer::setSingleThreadedResponder() {
useScheduledResponder_ = false;
}
void RSocketServer::acceptConnection(
std::unique_ptr<DuplexConnection> connection,
folly::EventBase&,
std::shared_ptr<RSocketServiceHandler> serviceHandler) {
stats_->serverConnectionAccepted();
if (isShutdown_) {
// connection is getting out of scope and terminated
return;
}
std::unique_ptr<DuplexConnection> framedConnection;
if (connection->isFramed()) {
framedConnection = std::move(connection);
} else {
framedConnection = std::make_unique<FramedDuplexConnection>(
std::move(connection), ProtocolVersion::Unknown);
}
auto* acceptor = setupResumeAcceptors_.get();
VLOG(2) << "Going to accept duplex connection";
acceptor->accept(
std::move(framedConnection),
[serviceHandler,
weakConSet = std::weak_ptr<ConnectionSet>(connectionSet_),
scheduledResponder = useScheduledResponder_](
std::unique_ptr<DuplexConnection> conn,
SetupParameters params) mutable {
if (auto connectionSet = weakConSet.lock()) {
RSocketServer::onRSocketSetup(
serviceHandler,
std::move(connectionSet),
scheduledResponder,
std::move(conn),
std::move(params));
}
},
std::bind(
&RSocketServer::onRSocketResume,
this,
serviceHandler,
std::placeholders::_1,
std::placeholders::_2));
}
void RSocketServer::onRSocketSetup(
std::shared_ptr<RSocketServiceHandler> serviceHandler,
std::shared_ptr<ConnectionSet> connectionSet,
bool scheduledResponder,
std::unique_ptr<DuplexConnection> connection,
SetupParameters setupParams) {
const auto eventBase = folly::EventBaseManager::get()->getExistingEventBase();
VLOG(2) << "Received new setup payload on " << eventBase->getName();
CHECK(eventBase);
auto result = serviceHandler->onNewSetup(setupParams);
if (result.hasError()) {
VLOG(3) << "Terminating SETUP attempt from client. "
<< result.error().what();
connection->send(
FrameSerializer::createFrameSerializer(setupParams.protocolVersion)
->serializeOut(Frame_ERROR::rejectedSetup(result.error().what())));
return;
}
auto connectionParams = std::move(result.value());
if (!connectionParams.responder) {
LOG(ERROR) << "Received invalid Responder. Dropping connection";
connection->send(
FrameSerializer::createFrameSerializer(setupParams.protocolVersion)
->serializeOut(Frame_ERROR::rejectedSetup(
"Received invalid Responder from server")));
return;
}
const auto rs = std::make_shared<RSocketStateMachine>(
scheduledResponder
? std::make_shared<ScheduledRSocketResponder>(
std::move(connectionParams.responder), *eventBase)
: std::move(connectionParams.responder),
nullptr,
RSocketMode::SERVER,
connectionParams.stats,
std::move(connectionParams.connectionEvents),
setupParams.resumable
? std::make_shared<WarmResumeManager>(connectionParams.stats)
: ResumeManager::makeEmpty(),
nullptr /* coldResumeHandler */);
if (!connectionSet->insert(rs, eventBase)) {
VLOG(1) << "Server is closed, so ignore the connection";
connection->send(
FrameSerializer::createFrameSerializer(setupParams.protocolVersion)
->serializeOut(Frame_ERROR::rejectedSetup(
"Server ignores the connection attempt")));
return;
}
rs->registerCloseCallback(connectionSet.get());
auto requester = std::make_shared<RSocketRequester>(rs, *eventBase);
auto serverState = std::shared_ptr<RSocketServerState>(
new RSocketServerState(*eventBase, rs, std::move(requester)));
serviceHandler->onNewRSocketState(std::move(serverState), setupParams.token);
rs->connectServer(
std::make_shared<FrameTransportImpl>(std::move(connection)),
std::move(setupParams));
}
void RSocketServer::onRSocketResume(
std::shared_ptr<RSocketServiceHandler> serviceHandler,
std::unique_ptr<DuplexConnection> connection,
ResumeParameters resumeParams) {
auto result = serviceHandler->onResume(resumeParams.token);
if (result.hasError()) {
stats_->resumeFailedNoState();
VLOG(3) << "Terminating RESUME attempt from client. No ServerState found";
connection->send(
FrameSerializer::createFrameSerializer(resumeParams.protocolVersion)
->serializeOut(Frame_ERROR::rejectedSetup(result.error().what())));
return;
}
const auto serverState = std::move(result.value());
CHECK(serverState);
const auto eventBase = folly::EventBaseManager::get()->getExistingEventBase();
VLOG(2) << "Resuming client on " << eventBase->getName();
if (!serverState->eventBase_.isInEventBaseThread()) {
// If the resumed connection is on a different EventBase, then use
// ScheduledFrameTransport and ScheduledFrameProcessor to ensure the
// RSocketStateMachine continues to live on the same EventBase and the
// IO happens in the new EventBase
auto scheduledFT = std::make_shared<ScheduledFrameTransport>(
std::make_shared<FrameTransportImpl>(std::move(connection)),
eventBase, /* Transport EventBase */
&serverState->eventBase_); /* StateMachine EventBase */
serverState->eventBase_.runInEventBaseThread(
[serverState,
scheduledFT = std::move(scheduledFT),
resumeParams = std::move(resumeParams)]() mutable {
serverState->rSocketStateMachine_->resumeServer(
std::move(scheduledFT), resumeParams);
});
} else {
// If the resumed connection is on the same EventBase, then the
// RSocketStateMachine and Transport can continue living in the same
// EventBase without any thread hopping between them.
serverState->rSocketStateMachine_->resumeServer(
std::make_shared<FrameTransportImpl>(std::move(connection)),
resumeParams);
}
}
void RSocketServer::startAndPark(
std::shared_ptr<RSocketServiceHandler> serviceHandler) {
start(std::move(serviceHandler));
waiting_.wait();
}
void RSocketServer::unpark() {
waiting_.post();
}
folly::Optional<uint16_t> RSocketServer::listeningPort() const {
return duplexConnectionAcceptor_ ? duplexConnectionAcceptor_->listeningPort()
: folly::none;
}
size_t RSocketServer::getNumConnections() {
return connectionSet_ ? connectionSet_->size() : 0;
}
} // namespace rsocket
Morty Proxy This is a proxified and sanitized view of the page, visit original site.