Here I found a way to convert a data into a boost buffer:
#include <memory>
#include <boost/asio.hpp>
int main()
{
struct { float a, b; } data1;
auto mutable_buffer = boost::asio::buffer(data);
}
How to do the other way? I mean converting recv_buf.data()
into data1
?
socket.receive_from(boost::asio::buffer(recv_buf),
remote_endpoint, 0, error);
data1=recv_buf.data() ???????
You can pack like this:
struct object{ float a, b; } data1[1];
auto mutable_buffer = boost::asio::buffer(data1);
and unpack using memcpy
, but only for POD types.
const char* b = boost::asio::buffer_cast<const char*>(mutable_buffer);
object o;
memcpy(&o, b, boost::asio::buffer_size(mutable_buffer));
Live on Coliru
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With