When I start adding it to my main dialog for wxwidgets I start getting crazy errors. Any help is greatly appreciated.
Code: Select all
void TestDialog::OnInit(wxInitDialogEvent& event)
{
m_threadWebListener = std::thread(&TestDialog::StartWebListener);
}
// This function produces an HTTP response for the given request. The type of the response object depends on the
// contents of the request, so the interface requires the caller to pass a generic lambda for receiving the response.
template<class Body, class Allocator, class Send>
void TestDialog::handle_request(http::request<Body, http::basic_fields<Allocator>>&& req, Send&& send)
{
auto const bad_request = [&req](boost::beast::string_view why)
{
http::response<http::string_body> res{http::status::bad_request, req.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = why.to_string();
res.prepare_payload();
return res;
};
// Make sure we can handle the method
if(req.method() != http::verb::get && req.method() != http::verb::head)
return send(bad_request("Unknown HTTP-method"));
// Request path must be absolute and not contain "..".
if( req.target().empty() || req.target()[0] != '/' || req.target().find("..") != boost::beast::string_view::npos)
return send(bad_request("Illegal request-target"));
std::string path = req.target().to_string();
http::response<http::string_body> res;
res.version(11); // HTTP/1.1
res.result(http::status::ok);
res.set(http::field::server, "Beast");
res.body() = "Hello, world!";
res.prepare_payload();
// Respond to HEAD request
if(req.method() == http::verb::head)
{
http::response<http::empty_body> res{http::status::ok, req.version()};
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "application/json"); // mime_type(path));
//res.content_length(body.size());
res.keep_alive(req.keep_alive());
return send(std::move(res));
}
res.keep_alive(req.keep_alive());
return send(std::move(res));
}
template<class Stream>
struct send_lambda
{
Stream& stream_;
bool& close_;
boost::system::error_code& ec_;
explicit
send_lambda(
Stream& stream,
bool& close,
boost::system::error_code& ec)
: stream_(stream)
, close_(close)
, ec_(ec)
{
}
// Report a failure
void fail(boost::system::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// This is the C++11 equivalent of a generic lambda.
// The function object is used to send an HTTP message.
template<bool isRequest, class Body, class Fields>
void operator()(http::message<isRequest, Body, Fields>&& msg) const
{
// Determine if we should close the connection after
close_ = msg.need_eof();
// We need the serializer here because the serializer requires
// a non-const file_body, and the message oriented version of
// http::write only works with const messages.
http::serializer<isRequest, Body, Fields> sr{msg};
http::write(stream_, sr, ec_);
}
};
// Handles an HTTP server connection
void TestDialog::do_session(tcp::socket& socket, std::string const& doc_root)
{
bool close = false;
boost::system::error_code ec;
// This buffer is required to persist across reads
boost::beast::flat_buffer buffer;
// This lambda is used to send messages
send_lambda<tcp::socket> lambda{socket, close, ec};
for(;;)
{
// Read a request
http::request<http::string_body> req;
http::read(socket, buffer, req, ec);
if(ec == http::error::end_of_stream)
break;
if(ec)
return fail(ec, "read");
// Send the response
handle_request(std::move(req), lambda);
if(ec)
return fail(ec, "write");
if(close)
{
// This means we should close the connection, usually because
// the response indicated the "Connection: close" semantic.
break;
}
}
// Send a TCP shutdown
socket.shutdown(tcp::socket::shutdown_send, ec);
}
void TestDialog::StartWebListener()
{
try
{
string sListeningIp = "127.0.0.1";
string sListeningPort = "300";
auto const address = boost::asio::ip::make_address(sListeningIp);
auto const port = static_cast<unsigned short>(std::stoi(sListeningPort));
//unsigned short port = 3030;
//std::string const doc_root = "";
// The io_context is required for all I/O
boost::asio::io_context ioc{1};
// The acceptor receives incoming connections
tcp::acceptor acceptor{ioc, {address, port}};
for(;;)
{
tcp::socket socket{ioc}; // receive new connection
acceptor.accept(socket); // Block until we get a connection
// Launch the session, transferring ownership of the socket
std::thread{std::bind(&TestDialog::do_session, std::move(socket), "")}.detach();
}
}
catch (const std::exception& e)
{
std::cerr << "Error: " << e.what() << std::endl;
// return EXIT_FAILURE;
}
}
Code: Select all
||=== Build: Debug(compiler: GNU GCC Compiler) ===|
/usr/include/c++/5/functional||In instantiation of ‘struct std::_Bind_check_arity<void (TestDialog::*)(boost::asio::basic_stream_socket<boost::asio::ip::tcp>&, const std::__cxx11::basic_string<char>&), boost::asio::basic_stream_socket<boost::asio::ip::tcp>, const char (&)[1]>’:|
/usr/include/c++/5/functional|1439|required from ‘struct std::_Bind_helper<false, void (TestDialog::*)(boost::asio::basic_stream_socket<boost::asio::ip::tcp>&, const std::__cxx11::basic_string<char>&), boost::asio::basic_stream_socket<boost::asio::ip::tcp>, const char (&)[1]>’|
/usr/include/c++/5/functional|1462| required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (TestDialog::*)(boost::asio::basic_stream_socket<boost::asio::ip::tcp>&, const std::__cxx11::basic_string<char>&); _BoundArgs = {boost::asio::basic_stream_socket<boost::asio::ip::tcp>, const char (&)[1]}]’|
/path/MyDialogMain.cpp|269|required from here|
/usr/include/c++/5/functional|1426|error: static assertion failed: Wrong number of arguments for pointer-to-member|
/usr/include/c++/5/functional||In instantiation of ‘struct std::_Bind_check_arity<void (TestDialog::*)()>’:|
/usr/include/c++/5/functional|1538|required from ‘struct std::_Bind_simple_helper<void (TestDialog::*)()>’|
/usr/include/c++/5/functional|1552| required by substitution of ‘template<class _Callable, class ... _Args> typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (TestDialog::*)(); _Args = {}]’|
/usr/include/c++/5/thread|137|required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (TestDialog::*)(); _Args = {}]’|
/path/MyDialogMain.cpp|93|required from here|
/usr/include/c++/5/functional|1426|error: static assertion failed: Wrong number of arguments for pointer-to-member|
/usr/include/c++/5/functional||In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<void (TestDialog::*)()>()>’:|
/usr/include/c++/5/thread|137|required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (TestDialog::*)(); _Args = {}]’|
/path/MyDialogMain.cpp|93|required from here|
/usr/include/c++/5/functional|1505|error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (TestDialog::*)()>()>’|
/usr/include/c++/5/functional|1526|error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<void (TestDialog::*)()>()>’|
/usr/include/c++/5/functional||In instantiation of ‘struct std::_Bind_simple<std::_Bind<std::_Mem_fn<void (TestDialog::*)(boost::asio::basic_stream_socket<boost::asio::ip::tcp>&, const std::__cxx11::basic_string<char>&)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>, const char*)>()>’:|
/usr/include/c++/5/thread|137|required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = std::_Bind<std::_Mem_fn<void (TestDialog::*)(boost::asio::basic_stream_socket<boost::asio::ip::tcp>&, const std::__cxx11::basic_string<char>&)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>, const char*)>; _Args = {}]’|
/path/MyDialogMain.cpp|269|required from here|
/usr/include/c++/5/functional|1505|error: no type named ‘type’ in ‘class std::result_of<std::_Bind<std::_Mem_fn<void (TestDialog::*)(boost::asio::basic_stream_socket<boost::asio::ip::tcp>&, const std::__cxx11::basic_string<char>&)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>, const char*)>()>’|
/usr/include/c++/5/functional|1526|error: no type named ‘type’ in ‘class std::result_of<std::_Bind<std::_Mem_fn<void (TestDialog::*)(boost::asio::basic_stream_socket<boost::asio::ip::tcp>&, const std::__cxx11::basic_string<char>&)>(boost::asio::basic_stream_socket<boost::asio::ip::tcp>, const char*)>()>’|
||=== Build failed: 8 error(s), 13 warning(s) (0 minute(s), 3 second(s)) ===|