streambuf with boost::asio::async_write

streambuf with boost::asio::async_write

本文关键字:write async asio boost streambuf with      更新时间:2023-10-16

告诉我如何将boost::asio::streambufboost::asio::async_write一起使用。我有一个服务器应用程序,它通过一个客户端连接到它。

对于每个连接,我创建对象tcp_connection

如果我需要向客户端发送几个连续的消息,我如何正确地创建用于发送数据的缓冲区?

我是否需要同步调用Send(),因为它们使用全局缓冲区来发送?还是在调用async_write之前需要创建一个单独的缓冲区?

例如,在使用IOCP的Windows中,我创建了自己的包含缓冲区的OVERLAPPED结构。我在调用WSASend之前创建了一个缓冲区,并在操作完成后删除,从OVERLAPPED结构中提取它。Ie对于每个WSASend都有自己的缓冲区。

boost::asio::async_write怎么办?

这里有一个tcp_connection

#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/bind.hpp>
#include <iostream>
class tcp_connection
    // Using shared_ptr and enable_shared_from_this Because we want to keep the
    // tcp_connection object alive As long as there is an operation that refers to
    // it.
    : public boost::enable_shared_from_this<tcp_connection> {
    tcp_connection(boost::asio::io_service& io) : m_socket(io) {}
    void send(std::string data) {
        {
            std::ostream stream(&send_buffer);
            stream << data;
        }
        std::cout << "Send Data   =" << data                     << std::endl;
        std::cout << "Send Buffer =" << make_string(send_buffer) << std::endl;
        boost::asio::async_write(m_socket, send_buffer,
                                 boost::bind(&tcp_connection::handle_send, this, 
                                     boost::asio::placeholders::error,
                                     boost::asio::placeholders::bytes_transferred));
    }
    void handle_send(const boost::system::error_code &error, size_t);
  private:
    static std::string make_string(boost::asio::streambuf const&) { return "implemented elsewhere"; }
    boost::asio::ip::tcp::socket m_socket;
    boost::asio::streambuf send_buffer;
};

async_write要特别小心,不应该将async_write重叠到同一个流(请参阅规范http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/async_write/overload1.html)。我不确定你是否真的需要异步写入,只要你不需要传输这么多数据和并行地做其他事情。。。如果您确实需要它,那么您应该确保同步。您可以使用一些锁定机制,在(异步)写入之前获取锁定,并在WriteHandler中解锁。

如果缓冲区是连接的本地缓冲区,并且您没有在其他线程中访问它,则不需要锁定或复制。这和不使用阿西奥的任何地方都没什么不同。

确实需要同步同一套接字上的操作:为什么在使用boost::asio时,我需要每个连接都有strand?

要发送整个缓冲区,只需使用boost::asio::async_write

注意:您可能应该在完成处理程序的绑定中使用shared_from_this()而不是this