I'm a newbie in using BOOST library. I'm currently trying to send packets to Web-socket using boost library and struggling.
I've referred to the template from this boost website (https://www.boost.org/doc/libs/1_68_0/libs/beast/example/websocket/server/async/websocket_server_async.cpp).
Firstly, I call listener constructor by running the thread function below.
DWORD WINAPI RunWebServerThread(LPVOID lpParameter)
{
int threads(1);
net::io_context ioc{ threads };
queue<PQN_DATA_PACKET *> *pconnectionQ = (queue<PQN_DATA_PACKET *> *)lpParameter;
auto const address = net::ip::make_address(LOCAL_IP);
// Create and launch a listening port
std::make_shared<listener>(ioc, tcp::endpoint{ address, SERVER_PORT_NUMBER }, pconnectionQ)->run();
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for (auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ioc]
{
ioc.run();
});
ioc.run();
}
When the make_shared function is run, the following functions run from the boost library.
// Start accepting incoming connections
void listener::run()
{
do_accept();
}
void listener::do_accept()
{
cout << "DO_ACCEPT LISTENER\n";
// The new connection gets its own strand
acceptor_.async_accept(
net::make_strand(ioc_),
beast::bind_front_handler(
&listener::on_accept,
shared_from_this()));
cout << "DO_ACCEPT LISTENER END\n";
}
void listener::on_accept(beast::error_code ec, tcp::socket socket)
{
cout << "ON_ACCEPT LISTENER\n";
if (ec)
{
fail(ec, "accept");
}
else
{
// Create the session and run it
std::make_shared<session>(std::move(socket), pconnectionQ, ioc_)->run();
}
// Accept another connection
do_accept();
}
The problem is that on_accept function doesn't seem to be called upon bind_front_handler function from do_accept function.
The console is supposed to show the following.
DO_ACCEPT LISTENER
DO_ACCEPT LISTENER END
ON_ACCEPT LISTENER
However the console is just showing the following.
DO_ACCEPT LISTENER
DO_ACCEPT LISTENER END
As you can see on_accept() function is never called, and it is very hard for me to start debug this since boost library is too complicated.
Can anyone suggest me what to do here?
Thank you in advance!