The server accept new connections :
void do_accept()
{
acceptor_.async_accept(socket_,
[this](boost::system::error_code ec)
{
if (!ec)
{
connection_count++;
all_session.push_back(std::make_shared<session>(std::move(socket_), io_service_));
all_session.back()->start();
}
do_accept();
});
}
and the session->start works like below:
void start()
{
std::cout << "connection from " << socket_.remote_endpoint().address() << std::endl;
do_read_header();
}
the do_read_header tackle the messages then recursively call itself. When tackling the message, it would call write to send some information to client. The write function is below:
void write(const chat_message& msg)
{
write_msgs_ = msg;
do_write(msg.length());
}
void do_write(std::uint32_t length)
{
auto self(shared_from_this());
boost::asio::async_write(socket_, boost::asio::buffer(write_msgs_.data(), length),
[this, self](boost::system::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
do_read_header();
}
});
}
Then here is the question: how does the server actively send information to client outside the do_read_header() recursion? I need the server dispatch some work which is decided by future user input to all the clients. The boost asio c++11 examples don't metion how to do it. And I can't find some relevant resources on it.
Aucun commentaire:
Enregistrer un commentaire