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 Request = LegacyClient::Request;
60 using Response = LegacyClient::Response;
61 using LegacySocketAddress = LegacyClient::SocketAddress;
62
63 const LegacyClient legacyClient(
64 "legacy",
65 [](const std::shared_ptr<Request>& req) {
66 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
67
68 VLOG(1) << connectionName << ": OnRequestBegin";
69
70 req->set("Sec-WebSocket-Protocol", "subprotocol, echo");
71
72 req->upgrade(
73 "/ws",
74 "websocket",
75 [connectionName](const std::shared_ptr<Request>& req, bool success) {
76 VLOG(1) << connectionName << ": HTTP Upgrade (http -> " << req->header("upgrade") << "|"
77 << req->header("Sec-WebSocket-Protocol") << ") start " << (success ? "success" : "failed");
78 },
79 [connectionName]([[maybe_unused]] const std::shared_ptr<Request>& req,
80 [[maybe_unused]] 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 },
88 []([[maybe_unused]] const std::shared_ptr<Request>& req) {
89 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
90
91 VLOG(1) << connectionName << ": OnRequestEnd";
92 });
93
94 legacyClient.connect([instanceName = legacyClient.getConfig().getInstanceName()](const LegacySocketAddress& socketAddress,
95 const core::socket::State& state) {
96 switch (state) {
98 VLOG(1) << instanceName << " connected to '" << socketAddress.toString() << "'";
99 break;
101 VLOG(1) << instanceName << " disabled";
102 break;
104 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
105 break;
107 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
108 break;
109 }
110 }); // Connection:keep-alive\r\n\r\n"
111
112 using TlsClient = web::http::tls::in::Client;
113 using Request = TlsClient::Request;
114 using Response = TlsClient::Response;
115 using TLSSocketAddress = TlsClient::SocketAddress;
116
117 const TlsClient tlsClient(
118 "tls",
119 [](const std::shared_ptr<Request>& req) {
120 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
121
122 VLOG(1) << connectionName << ": OnRequestBegin";
123
124 req->set("Sec-WebSocket-Protocol", "subprotocol, echo");
125
126 req->upgrade(
127 "/ws",
128 "websocket",
129 [connectionName](const std::shared_ptr<Request>& req, bool success) {
130 VLOG(1) << connectionName << ": HTTP Upgrade (http -> " << req->header("upgrade") << "|"
131 << req->header("Sec-WebSocket-Protocol") << ") start " << (success ? "success" : "failed");
132 },
133 [connectionName]([[maybe_unused]] const std::shared_ptr<Request>& req,
134 [[maybe_unused]] const std::shared_ptr<Response>& res,
135 [[maybe_unused]] bool success) {
136 });
137 },
138 []([[maybe_unused]] const std::shared_ptr<Request>& req) {
139 const std::string connectionName = req->getSocketContext()->getSocketConnection()->getConnectionName();
140
141 VLOG(1) << connectionName << ": OnRequestEnd";
142 });
143
144 tlsClient.connect([instanceName = tlsClient.getConfig().getInstanceName()](const TLSSocketAddress& socketAddress,
145 const core::socket::State& state) {
146 switch (state) {
148 VLOG(1) << instanceName << " connected to '" << socketAddress.toString() << "'";
149 break;
151 VLOG(1) << instanceName << " disabled";
152 break;
154 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
155 break;
157 VLOG(1) << instanceName << " " << socketAddress.toString() << ": " << state.what();
158 break;
159 }
160 }); // Connection:keep-alive\r\n\r\n"
161 }
162
163 return core::SNodeC::start();
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
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:87
Client::Request Request
Definition clients.h:86
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::Request::upgrade(), and core::socket::State::what().

Here is the call graph for this function: