#include <boost/asio.hpp> #include <boost/asio/ssl.hpp> #include <boost/thread.hpp> #include <chrono> #include <list> #include <iostream> #include <boost/beast/core.hpp> #include <boost/beast/http.hpp> #include <boost/beast/ssl.hpp> #include <boost/beast/version.hpp> using namespace std::chrono_literals; using boost::asio::ip::tcp; namespace ssl = boost::asio::ssl; namespace beast = boost::beast; // from <boost/beast.hpp> namespace http = boost::beast::http; // from <boost/beast/http.hpp> // Connection establisher class class CSSLConn : public std::enable_shared_from_this<CSSLConn> { ssl::stream<tcp::socket> _stream; std::string _host, _port; beast::flat_buffer _buffer; // (Must persist between reads) http::request<http::string_body> _req; http::response<http::string_body> _res; public: template <typename Executor> CSSLConn(Executor ex, ssl::context& ctx, std::string sHost, std::string sPort) : _stream(make_strand(ex), ctx), _host(sHost), _port(sPort) {} void connect(std::string host, std::string port) { tcp::resolver r{ _stream.get_executor() }; boost::asio::connect(_stream.lowest_layer(), r.resolve({ host, port })); _stream.handshake(ssl::stream_base::handshake_type::client); // TODO: post some async work using shared_from_this() } void senddata(http::request<http::string_body> req) { boost::asio::post( beast::bind_front_handler( &CSSLConn::AsyncWrite, shared_from_this())); } void AsyncWrite() { // Send the HTTP request to the remote host http::async_write(_stream, _req, beast::bind_front_handler( &CSSLConn::on_write, shared_from_this())); } void on_write(beast::error_code ec, std::size_t bytes_transferred) { http::async_read(_stream, _buffer, _res, std::bind( &CSSLConn::on_read, shared_from_this(), std::placeholders::_1, std::placeholders::_2)); } void on_read(beast::error_code ec, std::size_t bytes_transferred) { boost::ignore_unused(bytes_transferred); std::cout << _res; } }; // Class for maintaining the thread pool class CHttpClient { public: CHttpClient(int nThreads) : ioc(nThreads) { while (nThreads--) { m_sslConnObj.push_back(std::make_shared<CSSLConn>( ioc.get_executor(), m_ctx, sHost, sPort)); } for (auto& conn : m_sslConnObj) conn->connect(sHost, sPort); std::this_thread::sleep_for(200ms); } ~CHttpClient() { ioc.join(); // stop() if you don't want to wait for work to complete } void SendRequest(http::request<http::string_body> req) { // post the request to thread_pool m_sslConnObj.front()->senddata(req); // what is the correct way of posting to thread pool } private: std::string sHost = "localhost"; std::string sPort = "5443"; std::list<std::shared_ptr<CSSLConn> > m_sslConnObj; boost::asio::thread_pool ioc; ssl::context m_ctx{ ssl::context::tlsv12_client }; }; int main() { CHttpClient clients(10); std::string target = "/api/v1/Alarm"; int version = 11; http::request<http::string_body> req; // Set up an HTTP GET request message req.version(version); req.method(http::verb::get); req.target(target); req.set(http::field::host, "localhost"); req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING); while (true) { clients.SendRequest(req); std::this_thread::sleep_for(std::chrono::milliseconds(200)); } }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}