54 {
56
57 {
60 using Request = LegacyClient::Request;
61 using Response = LegacyClient::Response;
62 using LegacySocketAddress = LegacyClient::SocketAddress;
63
64 const LegacyClient legacyClient(
65 "legacy",
66 [](const std::shared_ptr<MasterRequest>& req) {
67 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
68
69 VLOG(1) << connectionName << ": OnRequestBegin";
70
71 req->set("Sec-WebSocket-Protocol", "subprotocol, echo");
72
73 req->upgrade(
74 "/ws",
75 "websocket",
76 [connectionName](bool success) {
77 VLOG(1) << connectionName << ": HTTP Upgrade (http -> websocket) start " << (success ? "success" : "failed");
78 },
79 [connectionName]([[maybe_unused]] const std::shared_ptr<Request>& req,
80 const std::shared_ptr<Response>& res,
81 [[maybe_unused]] bool success) {
82 VLOG(1) << connectionName << ": Upgrade success:";
83
84 VLOG(1) << connectionName << ": Requested: " << req->header("upgrade");
85 VLOG(1) << connectionName << ": Selected: " << res->get("upgrade");
86 },
87 [connectionName](const std::shared_ptr<Request>&, const std::string& message) {
88 VLOG(1) << connectionName << ": Request parse error: " << message;
89 });
90 },
91 []([[maybe_unused]] const std::shared_ptr<MasterRequest>& req) {
92 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
93
94 VLOG(1) << connectionName << ": OnRequestEnd";
95 });
96
97 legacyClient.connect([instanceName = legacyClient.getConfig().getInstanceName()](const LegacySocketAddress& socketAddress,
99 switch (state) {
101 VLOG(1) << instanceName << " connected to '" << socketAddress.toString() << "'";
102 break;
104 VLOG(1) << instanceName << " disabled";
105 break;
107 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
108 break;
110 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
111 break;
112 }
113 });
114
117 using Request = TlsClient::Request;
118 using Response = TlsClient::Response;
119 using TLSSocketAddress = TlsClient::SocketAddress;
120
121 const TlsClient tlsClient(
122 "tls",
123 [](const std::shared_ptr<MasterRequest>& req) {
124 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
125
126 VLOG(1) << connectionName << ": OnRequestBegin";
127
128 req->set("Sec-WebSocket-Protocol", "subprotocol, echo");
129
130 req->upgrade(
131 "/ws",
132 "websocket",
133 [connectionName](bool success) {
134 VLOG(1) << connectionName << ": HTTP Upgrade (http -> websocket) start " << (success ? "success" : "failed");
135 },
136 [connectionName]([[maybe_unused]] const std::shared_ptr<Request>& req,
137 [[maybe_unused]] const std::shared_ptr<Response>& res,
138 [[maybe_unused]] bool success) {
139 },
140 [connectionName](const std::shared_ptr<Request>&, const std::string& message) {
141 VLOG(1) << connectionName << ": Request parse error: " << message;
142 });
143 },
144 []([[maybe_unused]] const std::shared_ptr<MasterRequest>& req) {
145 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
146
147 VLOG(1) << connectionName << ": OnRequestEnd";
148 });
149
150 tlsClient.connect([instanceName = tlsClient.getConfig().getInstanceName()](const TLSSocketAddress& socketAddress,
152 switch (state) {
154 VLOG(1) << instanceName << " connected to '" << socketAddress.toString() << "'";
155 break;
157 VLOG(1) << instanceName << " disabled";
158 break;
160 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
161 break;
163 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
164 break;
165 }
166 });
167 }
168
170}
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
Client::Response Response
Client::MasterRequest MasterRequest
web::http::client::Client< net::in::stream::legacy::SocketClient > Client
web::http::client::Client< net::in::stream::tls::SocketClient > Client