SNode.C
Loading...
Searching...
No Matches
echoclient.cpp File Reference
Include dependency graph for echoclient.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 52 of file echoclient.cpp.

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

References web::http::client::Client< SocketClientT >::Client(), core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::connect(), core::socket::State::DISABLED, core::socket::State::ERROR, core::socket::State::FATAL, web::http::client::Response::get(), core::socket::Socket< ConfigT >::getConfig(), core::socket::stream::SocketConnection::getConnectionName(), web::http::client::Request::getConnectionName(), net::config::ConfigInstance::getInstanceName(), core::socket::stream::SocketContext::getSocketConnection(), web::http::client::MasterRequest::getSocketContext(), web::http::client::Request::header(), core::SNodeC::init(), logger::LogMessage::LogMessage(), core::socket::State::OK, web::http::client::Request::set(), core::SNodeC::start(), net::in::SocketAddress::toString(), web::http::client::MasterRequest::upgrade(), and core::socket::State::what().

Here is the call graph for this function: