streambuf with boost::asio::async_write
streambuf with boost::asio::async_write
告诉我如何将boost::asio::streambuf
与boost::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
- 为什么std::async使用同一个线程运行函数
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 为什么可以将左值传递给"std::async",即使它引用了右值
- 使用用户定义的参数调用future/async并调用类方法
- 使用 std::async 时死锁,将来作为成员
- 为什么我不能将引用作为 std::async 的函数参数传递
- std::async from std::async in windows xp
- Google Cloud Pubsub Async Streaming API in C++
- 当客户端在 write() 期间终止连接时,由对等套接字错误重置连接
- Qt 写入 xml: QIODevice::write (QFile, "D:/logs.xml" ): 设备未打开
- write() 和 read() 中几乎没有混淆
- OpenCV-contrib/Python/Windows:Tracker.write() 在 matrix_wrap.
- -fno-unwind-tables 和 -fno-async-unwind-tables 不起作用 NDK clang
- std::async 如何工作:为什么它会调用这么多次复制/移动?
- asio::read() 需要很长时间,使用 asio::write 没有问题
- std::async 不会立即调用
- async中没有匹配的函数(模板中未解析的类型)
- 封送指向结构异常"cannot Read or Write protected memory"的指针数组的指针数组
- BLE gattServer.write()重载函数
- std::async 如果线程是从 DLL 创建的,则会阻止进程退出?