如何从 Boost mutable_buffers_1 中获取数据?
How to get data out of Boost mutable_buffers_1?
我正在为我们的应用程序开发一个系统,以从外部设备获取数据。只要我向它发送一条特定消息,它就会以 10 条/秒的速度向我们发送回短消息(因此大约每 100 毫秒发送一条消息)。我正在使用 Boost 进行此通信。
过程相当简单:我创建套接字,发送消息,为消息接收提供处理程序:
// Header file:
...
std::unique_ptr<boost::asio::io_service> _theIOService;
std::unique_ptr<boost::asio::ip::tcp::socket> _theSocket;
int size_of_the_data = 100;
std::vector<char> _raw_buffer = std::vector<char>(size_of_the_data);
boost::asio::mutable_buffers_1 _data_buffer = boost::asio::buffer(_raw_buffer, size_of_the_data);
...
// Implementation file:
...
void DeviceDataListener::initiateTransfer() {
// create and connect the socket up here
...
// send the message
boost::system::error_code error;
boost::asio::write(*_theSocket,
boost::asio::buffer(beginMessage),
boost::asio::transfer_all(), error);
// start the receive
auto handler = boost::bind(&SCUDataListener::dataHandler, this, _1, _2);
_theSocket->async_receive( _data_buffer, handler );
std::thread run_thread([&]{ _theIOService->run(); });
...
}
void DeviceDataListener::dataHandler (
const boost::system::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes received.
) {
int foo = bytes_transferred;
// this line crashes application
char* pData = static_cast<char*>(_data_buffer.data());
}
有效,我的处理程序会立即被调用,这是应该的。问题是,我无法从 _data_buffer
中获取数据。这个:
auto it = _data_buffer.begin();
导致崩溃,即使 _data_buffer
有效。这个:
const char* pData = static_cast<char*>(_data_buffer.data());
不会编译。错误是“无法解析方法 'data'”。 mutable_buffer_1
API 表示 data() 是一个完全有效的方法,returns 内存范围的开始。
通过调试器检查,我可以看到没有错误,我可以看到 data
作为 _data_buffer
的成员并且它包含的内存地址确实包含我们期望的数据.问题是,我无法通过代码获取它。有谁知道如何在 Boost mutable_buffers_1
中获取数据?
我们在 Linux.
上使用 Eclipse CDT、C++11 和 gcc 运行
“Method 'data' could not be resolved”.
这个错误可能是正确的,但这取决于您使用的 Boost 版本。 data()
是 mutable_buffer
的成员,因为 >= 1.66 版本。因为 mutable_buffer
是 mutable_buffers_1
的基础 class 如果您至少使用 1.66 版本的 Boost,您的代码应该可以编译。
如果您的版本低于 1.66,您应该使用
char* p1 = boost::asio::buffer_cast<char*>(_data_buffer);
获取指向缓冲区中数据的指针。
_data_buffer.begin();
你不应该使用 begin()
方法,它 returns 指向 mutable_buffer_1
本身的指针。此方法由 asio-boost 库的内部函数使用,例如复制缓冲区序列,然后 begin()
指向要复制的特定缓冲区。
我正在为我们的应用程序开发一个系统,以从外部设备获取数据。只要我向它发送一条特定消息,它就会以 10 条/秒的速度向我们发送回短消息(因此大约每 100 毫秒发送一条消息)。我正在使用 Boost 进行此通信。
过程相当简单:我创建套接字,发送消息,为消息接收提供处理程序:
// Header file:
...
std::unique_ptr<boost::asio::io_service> _theIOService;
std::unique_ptr<boost::asio::ip::tcp::socket> _theSocket;
int size_of_the_data = 100;
std::vector<char> _raw_buffer = std::vector<char>(size_of_the_data);
boost::asio::mutable_buffers_1 _data_buffer = boost::asio::buffer(_raw_buffer, size_of_the_data);
...
// Implementation file:
...
void DeviceDataListener::initiateTransfer() {
// create and connect the socket up here
...
// send the message
boost::system::error_code error;
boost::asio::write(*_theSocket,
boost::asio::buffer(beginMessage),
boost::asio::transfer_all(), error);
// start the receive
auto handler = boost::bind(&SCUDataListener::dataHandler, this, _1, _2);
_theSocket->async_receive( _data_buffer, handler );
std::thread run_thread([&]{ _theIOService->run(); });
...
}
void DeviceDataListener::dataHandler (
const boost::system::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes received.
) {
int foo = bytes_transferred;
// this line crashes application
char* pData = static_cast<char*>(_data_buffer.data());
}
有效,我的处理程序会立即被调用,这是应该的。问题是,我无法从 _data_buffer
中获取数据。这个:
auto it = _data_buffer.begin();
导致崩溃,即使 _data_buffer
有效。这个:
const char* pData = static_cast<char*>(_data_buffer.data());
不会编译。错误是“无法解析方法 'data'”。 mutable_buffer_1
API 表示 data() 是一个完全有效的方法,returns 内存范围的开始。
通过调试器检查,我可以看到没有错误,我可以看到 data
作为 _data_buffer
的成员并且它包含的内存地址确实包含我们期望的数据.问题是,我无法通过代码获取它。有谁知道如何在 Boost mutable_buffers_1
中获取数据?
我们在 Linux.
上使用 Eclipse CDT、C++11 和 gcc 运行“Method 'data' could not be resolved”.
这个错误可能是正确的,但这取决于您使用的 Boost 版本。 data()
是 mutable_buffer
的成员,因为 >= 1.66 版本。因为 mutable_buffer
是 mutable_buffers_1
的基础 class 如果您至少使用 1.66 版本的 Boost,您的代码应该可以编译。
如果您的版本低于 1.66,您应该使用
char* p1 = boost::asio::buffer_cast<char*>(_data_buffer);
获取指向缓冲区中数据的指针。
_data_buffer.begin();
你不应该使用 begin()
方法,它 returns 指向 mutable_buffer_1
本身的指针。此方法由 asio-boost 库的内部函数使用,例如复制缓冲区序列,然后 begin()
指向要复制的特定缓冲区。