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 54 of file echoclient.cpp.

54 {
55 core::SNodeC::init(argc, argv);
56
57 {
58 using LegacyClient = web::http::legacy::in::Client;
59 using MasterRequest = LegacyClient::MasterRequest;
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,
98 const core::socket::State& state) {
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 }); // Connection:keep-alive\r\n\r\n"
114
115 using TlsClient = web::http::tls::in::Client;
116 using MasterRequest = TlsClient::MasterRequest;
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,
151 const core::socket::State& state) {
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 }); // Connection:keep-alive\r\n\r\n"
167 }
168
169 return core::SNodeC::start();
170}
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
static constexpr int DISABLED
Definition State.h:56
static constexpr int ERROR
Definition State.h:57
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
Client::Response Response
Definition clients.h:93
Client::Request Request
Definition clients.h:92
Client::MasterRequest MasterRequest
Definition clients.h:91
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(), net::config::ConfigInstance::getInstanceName(), core::socket::stream::SocketContext::getSocketConnection(), web::http::client::Request::getSocketContext(), web::http::client::Request::header(), core::SNodeC::init(), 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: