forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSocket.cpp
More file actions
140 lines (131 loc) · 5.43 KB
/
RSocket.cpp
File metadata and controls
140 lines (131 loc) · 5.43 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
// 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/RSocket.h"
namespace rsocket {
folly::Future<std::unique_ptr<RSocketClient>> RSocket::createConnectedClient(
std::shared_ptr<ConnectionFactory> connectionFactory,
SetupParameters setupParameters,
std::shared_ptr<RSocketResponder> responder,
std::chrono::milliseconds keepaliveInterval,
std::shared_ptr<RSocketStats> stats,
std::shared_ptr<RSocketConnectionEvents> connectionEvents,
std::shared_ptr<ResumeManager> resumeManager,
std::shared_ptr<ColdResumeHandler> coldResumeHandler,
folly::EventBase* stateMachineEvb) {
CHECK(resumeManager)
<< "provide ResumeManager::makeEmpty() instead of nullptr";
auto protocolVersion = setupParameters.protocolVersion;
auto createRSC =
[connectionFactory,
setupParameters = std::move(setupParameters),
responder = std::move(responder),
keepaliveInterval,
stats = std::move(stats),
connectionEvents = std::move(connectionEvents),
resumeManager = std::move(resumeManager),
coldResumeHandler = std::move(coldResumeHandler),
stateMachineEvb](
ConnectionFactory::ConnectedDuplexConnection connection) mutable {
VLOG(3) << "createConnectedClient received DuplexConnection";
return RSocket::createClientFromConnection(
std::move(connection.connection),
connection.eventBase,
std::move(setupParameters),
std::move(connectionFactory),
std::move(responder),
keepaliveInterval,
std::move(stats),
std::move(connectionEvents),
std::move(resumeManager),
std::move(coldResumeHandler),
stateMachineEvb);
};
return connectionFactory->connect(protocolVersion, ResumeStatus::NEW_SESSION)
.thenValue(
[createRSC = std::move(createRSC)](
ConnectionFactory::ConnectedDuplexConnection connection) mutable {
// fromConnection method must be called from the transport eventBase
// and since there is no guarantee that the Future returned from the
// connectionFactory::connect method is executed on the event base,
// we have to ensure it by using folly::via
auto transportEvb = &connection.eventBase;
return folly::via(
transportEvb,
[connection = std::move(connection),
createRSC = std::move(createRSC)]() mutable {
return createRSC(std::move(connection));
});
});
}
folly::Future<std::unique_ptr<RSocketClient>> RSocket::createResumedClient(
std::shared_ptr<ConnectionFactory> connectionFactory,
ResumeIdentificationToken token,
std::shared_ptr<ResumeManager> resumeManager,
std::shared_ptr<ColdResumeHandler> coldResumeHandler,
std::shared_ptr<RSocketResponder> responder,
std::chrono::milliseconds keepaliveInterval,
std::shared_ptr<RSocketStats> stats,
std::shared_ptr<RSocketConnectionEvents> connectionEvents,
ProtocolVersion protocolVersion,
folly::EventBase* stateMachineEvb) {
auto* c = new RSocketClient(
std::move(connectionFactory),
std::move(protocolVersion),
std::move(token),
std::move(responder),
keepaliveInterval,
std::move(stats),
std::move(connectionEvents),
std::move(resumeManager),
std::move(coldResumeHandler),
stateMachineEvb);
return c->resume().thenValue(
[client = std::unique_ptr<RSocketClient>(c)](auto&&) mutable {
return std::move(client);
});
}
std::unique_ptr<RSocketClient> RSocket::createClientFromConnection(
std::unique_ptr<DuplexConnection> connection,
folly::EventBase& transportEvb,
SetupParameters params,
std::shared_ptr<ConnectionFactory> connectionFactory,
std::shared_ptr<RSocketResponder> responder,
std::chrono::milliseconds keepaliveInterval,
std::shared_ptr<RSocketStats> stats,
std::shared_ptr<RSocketConnectionEvents> connectionEvents,
std::shared_ptr<ResumeManager> resumeManager,
std::shared_ptr<ColdResumeHandler> coldResumeHandler,
folly::EventBase* stateMachineEvb) {
auto client = std::unique_ptr<RSocketClient>(new RSocketClient(
std::move(connectionFactory),
params.protocolVersion,
params.token,
std::move(responder),
keepaliveInterval,
std::move(stats),
std::move(connectionEvents),
std::move(resumeManager),
std::move(coldResumeHandler),
stateMachineEvb));
client->fromConnection(
std::move(connection), transportEvb, std::move(params));
return client;
}
std::unique_ptr<RSocketServer> RSocket::createServer(
std::unique_ptr<ConnectionAcceptor> connectionAcceptor,
std::shared_ptr<RSocketStats> stats) {
return std::make_unique<RSocketServer>(
std::move(connectionAcceptor), std::move(stats));
}
} // namespace rsocket