forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRSocketClient.cpp
More file actions
109 lines (90 loc) · 3.32 KB
/
Copy pathRSocketClient.cpp
File metadata and controls
109 lines (90 loc) · 3.32 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
// Copyright 2004-present Facebook. All Rights Reserved.
#include "src/RSocketClient.h"
#include "src/RSocketRequester.h"
#include "src/RSocketResponder.h"
#include "src/RSocketStats.h"
#include "src/framing/FrameTransport.h"
#include "src/framing/FramedDuplexConnection.h"
#include "src/internal/FollyKeepaliveTimer.h"
#include "src/internal/RSocketConnectionManager.h"
using namespace folly;
namespace rsocket {
RSocketClient::RSocketClient(
std::unique_ptr<ConnectionFactory> connectionFactory)
: connectionFactory_(std::move(connectionFactory)),
connectionManager_(std::make_unique<RSocketConnectionManager>()) {
VLOG(1) << "Constructing RSocketClient";
}
RSocketClient::~RSocketClient() {
VLOG(1) << "Destroying RSocketClient";
}
folly::Future<std::unique_ptr<RSocketRequester>> RSocketClient::connect(
SetupParameters setupParameters,
std::shared_ptr<RSocketResponder> responder,
std::unique_ptr<KeepaliveTimer> keepaliveTimer,
std::shared_ptr<RSocketStats> stats,
std::shared_ptr<RSocketNetworkStats> networkStats) {
VLOG(2) << "Starting connection";
folly::Promise<std::unique_ptr<RSocketRequester>> promise;
auto future = promise.getFuture();
connectionFactory_->connect([
this,
setupParameters = std::move(setupParameters),
responder = std::move(responder),
keepaliveTimer = std::move(keepaliveTimer),
stats = std::move(stats),
networkStats = std::move(networkStats),
promise = std::move(promise)](
std::unique_ptr<DuplexConnection> connection,
folly::EventBase& eventBase) mutable {
VLOG(3) << "onConnect received DuplexConnection";
auto rsocket = fromConnection(
std::move(connection),
eventBase,
std::move(setupParameters),
std::move(responder),
std::move(keepaliveTimer),
std::move(stats),
std::move(networkStats));
promise.setValue(std::move(rsocket));
});
return future;
}
std::unique_ptr<RSocketRequester> RSocketClient::fromConnection(
std::unique_ptr<DuplexConnection> connection,
folly::EventBase& eventBase,
SetupParameters setupParameters,
std::shared_ptr<RSocketResponder> responder,
std::unique_ptr<KeepaliveTimer> keepaliveTimer,
std::shared_ptr<RSocketStats> stats,
std::shared_ptr<RSocketNetworkStats> networkStats) {
CHECK(eventBase.isInEventBaseThread());
if (!responder) {
responder = std::make_shared<RSocketResponder>();
}
if (!keepaliveTimer) {
keepaliveTimer = std::make_unique<FollyKeepaliveTimer>(
eventBase, std::chrono::milliseconds(5000));
}
if (!stats) {
stats = RSocketStats::noop();
}
auto rs = std::make_shared<RSocketStateMachine>(
eventBase,
std::move(responder),
std::move(keepaliveTimer),
ReactiveSocketMode::CLIENT,
std::move(stats),
std::move(networkStats));
connectionManager_->manageConnection(rs, eventBase);
std::unique_ptr<DuplexConnection> framedConnection;
if (connection->isFramed()) {
framedConnection = std::move(connection);
} else {
framedConnection = std::make_unique<FramedDuplexConnection>(
std::move(connection), setupParameters.protocolVersion);
}
rs->connectClientSendSetup(std::move(framedConnection), std::move(setupParameters));
return std::make_unique<RSocketRequester>(std::move(rs), eventBase);
}
} // namespace rsocket