简体   繁体   English

streambuf with boost :: asio :: async_write

[英]streambuf with boost::asio::async_write

Tell me how to use boost::asio::streambuf with boost::asio::async_write . 告诉我如何使用boost::asio::streambufboost::asio::async_write I have a server application that connects to it one client. 我有一个服务器应用程序连接到它一个客户端。

For each connection I create object tcp_connection . 对于每个连接,我创建对象tcp_connection

How do I properly create buffer for sending data if I need to send to the client several consecutive messages? 如果我需要向客户端发送几条连续的消息,如何正确创建用于发送数据的缓冲区?

Do I need to have to synchronize calls Send() because it is they use a global buffer to be sent? 我是否需要同步调用Send(),因为它们是否使用全局缓冲区发送? Or do I need to create a separate buffer before calling async_write ? 或者在调用async_write之前是否需要创建单独的缓冲区?

For example, in Windows using IOCP I create my own OVERLAPPED structure containing buffer. 例如,在使用IOCP的Windows中,我创建了自己的包含缓冲区的OVERLAPPED结构。 I create a buffer before calling WSASend and delete after the operation is completed, extracting it from the OVERLAPPED Structure. 我在调用WSASend之前创建一个缓冲区,并在操作完成后删除,从OVERLAPPED结构中提取它。 Ie for each WSASend has a own buffer. 即每个WSASend都有自己的缓冲区。

And how to do to boost::asio::async_write ? 怎么做boost::asio::async_write

Here I have a class tcp_connection 这里我有一个类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;
};

Be really careful with async_write , you should not overlap async_write s to the same stream (see the spec http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/async_write/overload1.html ). 要非常小心使用async_write ,不要将async_write与同一个流重叠(请参阅规范http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/reference/async_write/overload1.html ) 。 I am not sure that you really need asynchronous writes as long as you don't need to transfer so many data and doing other things in parallel... If you really need it, then you should ensure the synchronization. 我不确定你是否真的需要异步写入,只要你不需要传输这么多数据并同时做其他事情......如果你真的需要它,那么你应该确保同步。 You may use some locking mechanism, acquire a lock before (async) writing and unlock in the WriteHandler . 您可以使用一些锁定机制,在WriteHandler (异步)写入和解锁之前获取锁定。

If the buffer is local to the connection and you don't access it in other threads, you don't need to lock or copy. 如果缓冲区是连接的本地缓冲区,并且您不在其他线程中访问它,则无需锁定或复制。 That's no different than anywhere without using Asio. 这与没有使用Asio的任何地方没有什么不同。

You do need to synchronize operations on the same socket: Why do I need strand per connection when using boost::asio? 需要在同一插座上进行同步操作: 为什么我需要每个连接搁浅使用boost :: ASIO什么时候?

To send the whole buffer just use boost::asio::async_write . 要发送整个缓冲区,只需使用boost::asio::async_write

Note: you should probably use shared_from_this() instead of this in the bind for the completion handler 注意:您应该在完成处理程序的bind中使用shared_from_this()而不是this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM