C++ 通过boost::asio发送原始数据
我试图通过boost::asio发送原始数据,因为boost::序列化对于我的需要来说太慢了。以下是各种示例和boost文档,我有一个客户机: 模拟客户端:C++ 通过boost::asio发送原始数据,c++,sockets,networking,boost,boost-asio,C++,Sockets,Networking,Boost,Boost Asio,我试图通过boost::asio发送原始数据,因为boost::序列化对于我的需要来说太慢了。以下是各种示例和boost文档,我有一个客户机: 模拟客户端: void SimulationClient::sendData(std::vector<WaveformDefinition>waveformPackets) { socket.async_send_to(boost::asio::buffer(waveformPackets),
void SimulationClient::sendData(std::vector<WaveformDefinition>waveformPackets) {
socket.async_send_to(boost::asio::buffer(waveformPackets),
receiver_endpoint,
boost::bind(&ClientEnvironmentEngine::sendComplete, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
Tanner或其他人,你能告诉我我正在做的事情是否也应该起作用,或者我只是幸运地发现它正在起作用吗?首先,使用
pragma pack(1)
是不安全的。不同编译器/arch的打包方式可能不同。此外,在协议更改方面也会遇到问题。我建议改用
第二。您正在发送std::vector
,但此向量的实际数据不在结构波形\u数据\u结构
(向量将其数据保存在堆中)。因此,您将vector及其指向堆的指针发送到另一台机器,其中该指针显然无效。你需要以某种方式序列化你的向量
另外,boost::asio与此无关,这个问题是关于正确的序列化/反序列化。问题的基本部分是关于序列化和反序列化集合 如果不控制服务器和客户端的编译器和体系结构,发送原始结构通常是不安全的,因为系统之间的字节表示可能不同。虽然编译器和体系结构在此特定情况下是相同的,
#pragma pack(1)
是不相关的,因为波形数据结构
不是作为原始内存写入套接字的。相反,为收集写入操作提供了多个内存缓冲区
boost::数组缓冲区={{
boost::asio::buffer(&waveormpacket->numWaveforms,…)/&numWaveforms
boost::asio::buffer(波形MPacket->waveforms)/&波形[0]
}};
有各种工具可以帮助序列化数据结构,例如
下面的代码将演示为网络通信序列化数据结构的基础知识。为了简化代码和解释,我选择将重点放在序列化和反序列化上,而不是从套接字编写和读取。本节下面的另一个示例将展示更多的原始方法,该方法假定相同的编译器和体系结构
从基本的foo
类型开始:
structfoo
{
字符a;
字符b;
boost::uint16\u t c;
};
可以确定数据可以打包为总共4个字节。以下是一种可能的导线更换:
08162432
|--------+--------+--------+--------|
|a | b | c|
'--------+--------+--------+--------'
确定连线表示后,两个函数可用于将foo
对象序列化(保存)到缓冲区,另一个函数可用于从缓冲区反序列化(加载)foo
。由于foo.c
大于一个字节,因此函数也需要考虑。我选择使用Boost.Asio详细名称空间中的endian字节交换函数来实现一些平台中立性
//@brief将foo序列化到网络字节顺序缓冲区中。
void序列化(常量foo&foo,无符号字符*缓冲区)
{
缓冲区[0]=foo.a;
缓冲区[1]=foo.b;
//手柄端部。
using::boost::asio::detail::socket\u ops::host\u to\u network\u short;
boost::uint16\u t c=主机到网络短(foo.c);
std::memcpy(&buffer[2],&c,sizeof c);
}
///@brief从网络字节顺序缓冲区反序列化foo。
void反序列化(foo&foo,const unsigned char*buffer)
{
foo.a=缓冲区[0];
foo.b=缓冲区[1];
//手柄端部。
使用::boost::asio::detail::socket_ops::network_to_host_short;
boost::uint16\u t c;
std::memcpy(&c,&buffer[2],sizeof c);
foo.c=网络到主机(c);
}
对foo
进行序列化和反序列化后,下一步是处理foo
对象的集合。在编写代码之前,需要确定导线表示形式。在本例中,我决定使用32位计数字段作为foo
元素序列的前缀
08162432
|--------+--------+--------+--------|
|foo元素的计数[n]|
|--------+--------+--------+--------|
|序列化的foo[0]|
|--------+--------+--------+--------|
|序列化的foo[1]|
|--------+--------+--------+--------|
| ... |
|--------+--------+--------+--------|
|序列化的foo[n-1]|
'--------+--------+--------+--------'
同样,可以引入两个helper函数来序列化和反序列化foo
对象的集合,并且还需要考虑count字段的字节顺序
//@brief将一组foo序列化到网络字节顺序缓冲区中。
模板
std::vector序列化(const-Foos&Foos)
{
boost::uint32_t count=foos.size();
//分配一个足够大的缓冲区来存储:
//-foo元素的计数。
//-每个序列化的foo对象。
向量缓冲区(
sizeof count+//计数
foo_packed_size*count);//序列化foo对象
//手柄端部尺寸。
使用::boost::asio::detail::socket_ops::host_to_network_long;
计数=主机到网络的长度(计数);
//将大小打包到缓冲区中。
无符号字符*当前=&缓冲区[0];
std::memcpy(当前、计数和计数大小);
当前+=计数的大小;//调整位置。
//将每个foo打包到缓冲区中。
BOOST_FOREACH(const foo&foo,foos)
{
序列化(foo,当前);
当前+=foo\u packed\u size;//调整位置。
}
返回缓冲区;
};
///@brief将缓冲区反序列化为foo对象集合。
std::vector反序列化(const std::vector和buffer)
{
常量无符号字符*当前=&缓冲区[0];
//从缓冲区中提取元素的计数。
boost::uint32_t计数;
std::memcpy(&count,current,sizeof count);
电流+=sizeo
class WaveformReceiver {
WaveformDefinition *buffer;
WaveformReceiver(){
buffer = new WaveformDefinition[MAX_WAVEFORMS];
startReceive();
}
void startReceive() {
socket_.async_receive_from(boost::asio::null_buffers(), remote_endpoint_,
boost::bind(&WaveformReceiver::handleReceive, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
void handleReceive(const boost::system::error_code& error,
std::size_t size/*bytes_transferred*/)
{
if (!error)
{
int available = socket_.available();
int numWaveforms = available / sizeof(WaveformDefinition_c);
socket_.receive(boost::asio::buffer(buffer, available));
//copy buffer into another buffer so we can re-use the original buffer for the next read
WaveformDefinition_c* tempBuffer = new WaveformDefinition_c[numWaveforms];
std::memcpy ( tempBuffer, buffer, available );
//schedule a thread to handle the array of waveforms that we copied
threadPool.schedule( boost::bind( handleWaveforms, tempBuffer, numWaveforms));
//start listening for more waveforms
startReceive();
}
}
}
#include <iostream>
#include <vector>
#include <boost/asio.hpp>
#include <boost/asio/detail/socket_ops.hpp> // endian functions
#include <boost/cstdint.hpp>
#include <boost/foreach.hpp>
#include <boost/tuple/tuple.hpp> // boost::tie
#include <boost/tuple/tuple_comparison.hpp> // operator== for boost::tuple
/// @brief Mockup type.
struct foo
{
char a;
char b;
boost::uint16_t c;
};
/// @brief Equality check for foo objects.
bool operator==(const foo& lhs, const foo& rhs)
{
return boost::tie(lhs.a, lhs.b, lhs.c) ==
boost::tie(rhs.a, rhs.b, rhs.c);
}
/// @brief Calculated byte packed size for foo.
///
/// @note char + char + uint16 = 1 + 1 + 2 = 4
static const std::size_t foo_packed_size = 4;
/// @brief Serialize foo into a network-byte-order buffer.
///
/// @detail Data is packed as follows:
///
/// 0 8 16 24 32
/// |--------+--------+--------+--------|
/// | a | b | c |
/// '--------+--------+--------+--------'
void serialize(const foo& foo, unsigned char* buffer)
{
buffer[0] = foo.a;
buffer[1] = foo.b;
// Handle endianness.
using ::boost::asio::detail::socket_ops::host_to_network_short;
boost::uint16_t c = host_to_network_short(foo.c);
std::memcpy(&buffer[2], &c, sizeof c);
}
/// @brief Deserialize foo from a network-byte-order buffer.
void deserialize(foo& foo, const unsigned char* buffer)
{
foo.a = buffer[0];
foo.b = buffer[1];
// Handle endianness.
using ::boost::asio::detail::socket_ops::network_to_host_short;
boost::uint16_t c;
std::memcpy(&c, &buffer[2], sizeof c);
foo.c = network_to_host_short(c);
}
/// @brief Serialize a collection of foos into a network-byte-order buffer.
///
/// @detail Data is packed as follows:
///
/// 0 8 16 24 32
/// |--------+--------+--------+--------|
/// | count of foo elements [n] |
/// |--------+--------+--------+--------|
/// | serialized foo [0] |
/// |--------+--------+--------+--------|
/// | serialized foo [1] |
/// |--------+--------+--------+--------|
/// | ... |
/// |--------+--------+--------+--------|
/// | serialized foo [n-1] |
/// '--------+--------+--------+--------'
template <typename Foos>
std::vector<unsigned char> serialize(const Foos& foos)
{
boost::uint32_t count = foos.size();
// Allocate a buffer large enough to store:
// - Count of foo elements.
// - Each serialized foo object.
std::vector<unsigned char> buffer(
sizeof count + // count
foo_packed_size * count); // serialize foo objects
// Handle endianness for size.
using ::boost::asio::detail::socket_ops::host_to_network_long;
count = host_to_network_long(count);
// Pack size into buffer.
unsigned char* current = &buffer[0];
std::memcpy(current, &count, sizeof count);
current += sizeof count; // Adjust position.
// Pack each foo into the buffer.
BOOST_FOREACH(const foo& foo, foos)
{
serialize(foo, current);
current += foo_packed_size; // Adjust position.
}
return buffer;
};
/// @brief Deserialize a buffer into a collection of foo objects.
std::vector<foo> deserialize(const std::vector<unsigned char>& buffer)
{
const unsigned char* current = &buffer[0];
// Extract the count of elements from the buffer.
boost::uint32_t count;
std::memcpy(&count, current, sizeof count);
current += sizeof count;
// Handle endianness.
using ::boost::asio::detail::socket_ops::network_to_host_long;
count = network_to_host_long(count);
// With the count extracted, create the appropriate sized collection.
std::vector<foo> foos(count);
// Deserialize each foo from the buffer.
BOOST_FOREACH(foo& foo, foos)
{
deserialize(foo, current);
current += foo_packed_size;
}
return foos;
};
int main()
{
// Create a collection of foo objects with pre populated data.
std::vector<foo> foos_expected(5);
char a = 'a',
b = 'A';
boost::uint16_t c = 100;
// Populate each element.
BOOST_FOREACH(foo& foo, foos_expected)
{
foo.a = a++;
foo.b = b++;
foo.c = c++;
}
// Serialize the collection into a buffer.
std::vector<unsigned char> buffer = serialize(foos_expected);
// Deserialize the buffer back into a collection.
std::vector<foo> foos_actual = deserialize(buffer);
// Compare the two.
std::cout << (foos_expected == foos_actual) << std::endl; // expect 1
// Negative test.
foos_expected[0].c = 0;
std::cout << (foos_expected == foos_actual) << std::endl; // expect 0
}
// Create and populate a contiguous sequence of foo objects.
std::vector<foo> foo1;
populate(foo1);
// Get a handle to the contiguous memory block.
const char* buffer = reinterpret_cast<const char*>(&foo1[0]);
// Populate a new vector via iterator constructor.
const foo* begin = reinterpret_cast<const foo*>(buffer);
std::vector<foo> foos2(begin, begin + foos1.size());
#include <iostream>
#include <vector>
#include <boost/cstdint.hpp>
#include <boost/foreach.hpp>
#include <boost/tuple/tuple.hpp> // boost::tie
#include <boost/tuple/tuple_comparison.hpp> // operator== for boost::tuple
/// @brief Mockup type.
struct foo
{
char a;
char b;
boost::uint16_t c;
};
/// @brief Equality check for foo objects.
bool operator==(const foo& lhs, const foo& rhs)
{
return boost::tie(lhs.a, lhs.b, lhs.c) ==
boost::tie(rhs.a, rhs.b, rhs.c);
}
int main()
{
// Create a collection of foo objects with pre populated data.
std::vector<foo> foos_expected(5);
char a = 'a',
b = 'A';
boost::uint16_t c = 100;
// Populate each element.
BOOST_FOREACH(foo& foo, foos_expected)
{
foo.a = a++;
foo.b = b++;
foo.c = c++;
}
// Treat the collection as a raw buffer.
const char* buffer =
reinterpret_cast<const char*>(&foos_expected[0]);
// Populate a new vector.
const foo* begin = reinterpret_cast<const foo*>(buffer);
std::vector<foo> foos_actual(begin, begin + foos_expected.size());
// Compare the two.
std::cout << (foos_expected == foos_actual) << std::endl;
// Negative test.
foos_expected[0].c = 0;
std::cout << (foos_expected == foos_actual) << std::endl;
}