I've wrote an async websocket thru boost.beast. But when I try to run it, I cannot connect it.
The server code like below. When I try to connect my websocket server, my chrome shows status connecting. When I debug thru VS2017, it never run into the lambda expression in run().
iListener::iListener( boost::asio::io_context& ioc,boost::asio::ip::tcp::endpoint endpoint) : acceptor_(ioc), socket_(ioc) {
boost::system::error_code ec;
std::cout<<"iListener"<<std::endl;
// Open the acceptor
acceptor_.open(endpoint.protocol(), ec);
if (ec) {
// fail(ec, "open");
return;
}
// Allow address reuse
acceptor_.set_option(boost::asio::socket_base::reuse_address(true), ec);
if (ec) {
// fail(ec, "set_option");
return;
}
// Bind to the server address
acceptor_.bind(endpoint, ec);
if (ec) {
// fail(ec, "bind");
return;
}
// Start listening for connections
acceptor_.listen(
boost::asio::socket_base::max_listen_connections, ec);
if (ec) {
std::cout << ec.message() << " listen" << std::endl;
// fail(ec, "listen");
return;
}
}
iListener::~iListener() {
}
void iListener::run() {
if (!acceptor_.is_open()) {
return;
}
std::cout<<"iListener run"<<std::endl;
while (true) {
acceptor_.async_accept(socket_, [&](boost::system::error_code ec1) {
std::cout << "now run listener" << std::endl;
if (ec1) {
std::cout<<ec1.message()<<" accept"<<std::endl;
// fail(ec, "accept");
} else {
// Create the session and run it
std::make_shared<NormalSession>(std::move(socket_))->run();
}
});
}
}
void iListener::initListener(const std::string &addressStr, unsigned short port, int threads){
auto const address = boost::asio::ip::make_address(addressStr);
boost::asio::io_context ioc{threads};
std::make_shared<iListener>(ioc, boost::asio::ip::tcp::endpoint{address, port})->run();
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();
}
And when I try to connect it on Console of Chrome. It taks a long time to connect, and then shows failed.
So I change back, as the example of boost.It works.
void iListener::run() {
if (!acceptor_.is_open()) {
return;
}
// std::cout<<"iListener run"<<std::endl;
// while (true) {
// acceptor_.async_accept(socket_, [&](boost::system::error_code ec1) {
//std::cout << "now run listener" << std::endl;
// if (ec1) {
// std::cout<<ec1.message()<<" accept"<<std::endl;
// // fail(ec, "accept");
// } else {
// // Create the session and run it
// std::make_shared<NormalSession>(std::move(socket_))->run();
// }
// });
// }
do_accept();
}
void iListener::do_accept() {
acceptor_.async_accept(
socket_,
std::bind(
&iListener::on_accept,
shared_from_this(),
std::placeholders::_1));
}
void iListener::on_accept(boost::system::error_code ec) {
if (ec)
{
std::cout << ec.message() << " on_accept" << std::endl;
}
else
{
// Create the session and run it
std::make_shared<NormalSession>(std::move(socket_))->run();
}
// Accept another connection
do_accept();
}
I have two questions:
1.Why I use lambda, it will SOF , but example won't. 2.When I use while(), it not work, why? Is there any different between lambda expression and std::bind()??
So another question, what's different between two code blocks below?
void iListener::do_accept() {
acceptor_.async_accept(
socket_,
[&](boost::system::error_code ec1) mutable {
on_accept(ec1);
}
}
void iListener::do_accept() {
acceptor_.async_accept(
socket_,
std::bind(
&iListener::on_accept,
shared_from_this(),
std::placeholders::_1));
}
When I use the top one, it return error_code 995.