不知何故,我觉得类似的问题有几个答案,但是我找不到最终解决我的问题的方法。所以,我提前道歉: 我有许多传入的消息结构:
struct X_1 //Y_2, Z_x, _...
{
IncomingHeader incoming_header;
//.......
};
或传出:
struct A_1 //B_2, C_x, _...
{
OutgoingHeader outgoing_header;
//.......
};
消息头只有两种类型:
struct IncomingHeader
{
A a;
B b;
};
struct OutgoingHeader
{
A a;
B b;
char c[SIZE};
};
//If it helps, eventually I am only interested in a and b in header structs.
在解码过程中的某个时刻,我需要一个 get_header()
函数来返回 header 成员(incoming_header
或 outgoing_header
)。
有没有办法解决这个问题?
(我使用的是 boost 1.46 而不是 C++11)
最佳答案
好吧,Walter 通过引入通用基类型来解决这个问题。但通常有 2 种方法可以处理编码/编码数据。
- 将网络字节直接映射到数据结构,例如C/C++ POD 类型
- 使用独立于系统的数据表示格式,例如Google Protobuf、XDR、ASN.1 等等(甚至是非二进制的,如 XML、JSON、YAML...)
案例一:POD类处理
C/C++ POD
其实我唯一不同意Walter的想法的地方就是引入了虚基类。特别是,因为类型不再是 POD并且无法将其 1:1 映射到网络字节,需要复制数据。
通常,您示例中的 A
、B
类型被设计为 POD。这允许非常有效地编码/解码它们而无需复制。
假设您有某事。喜欢:
struct incoming_header
{
std::int32_t a;
std::int64_t b;
};
struct outgoing_header
{
std::int32_t a;
std::int64_t b;
char c[SIZE};
};
这里我们使用C++ standard's guaranteed length integers确保我们处理准确长度的字段。不幸的是,标准定义它们是可选的,因此可能无法在您的目标平台上使用(对于完全成熟的 HW 实际上很少,并且在某些嵌入式 HW 上可能是这种情况)。
发送 POD
现在因为这些类型是 POD,我们可以通过简单地将它们的字节推送到网络来简单地发送它们。
所以下面的伪代码是完全OK的:
outgoing_header oh{...};
send(&oh, sizeof(oh));
接收 POD
通常你知道如何(从你的协议(protocol)中你需要多少字节),假设它们都被复制到连续的缓冲区中,你可以获得对该缓冲区的正确 View 。假设我们此时不处理大/小端问题。然后网络代码通常会为您接收字节并说明这些字节有多少。
所以在这一点上,让我们相信我们现在只能接收 outgoing_header
并且我们的缓冲区足够大以包含整个消息长度。
然后代码通常类似于:
constexpr static size_t max_size = ...;
char buf[max_size]{};
size_t got_bytes = receive(&buf, max_size);
// now we need to interpret these bytes as outgoing_header
outgoing_header* pheader = reinterpret_cast<outgoing_header*>(&buf[0]);
// now access the header items
pheader->a;
pheader->b;
不涉及拷贝,只是一个指针转换。
解决您的问题
通常,任何二进制协议(protocol)都有一个通用的 header ,发送方和接收方都可以依赖。有编码,携带的是什么消息,多长时间,可能是协议(protocol)版本等。
您需要做的是引入一个通用 header ,在您的情况下它应该携带字段a
和b
。
struct base_header
{
std::int32_t a;
std::int64_t b;
};
// Note! Using derivation will render the type as non-POD, thus aggregation
struct incoming_header
{
base_header base;
};
struct outgoing_header
{
base_header base;
char c[SIZE};
};
现在 incoming_header 和 outgoing_header 都是 POD。您需要在这里做的是将您的缓冲区转换为指向 base_header 的指针,并获取感兴趣的 a
和 b
:
base_header* pbase_header = reinterpret_cast<base_header*>(&buf[0]);
do_smth(pbase_header->a, pbase_header->b);
案例2:系统无关的数据表示格式
该方法的替代方法是使用 boost::variant
类或者如果你切换到C++17 std::variant
.如果您不能拥有 POD 并且拥有某种带有自定义编码/解码库的自定义序列化格式,例如Google Protobuf 或类似工具...
使用变体,您可以定义您的协议(protocol),即可能到达的消息/ header :
typedef boost::variant<boost::none, IncomingHeader, OutgoingHeader> message_header;
message_header get_header(char* bytes, size_t size)
{
// dispatch bytes and put the message to variant:
// let's say we get OutgoingHeader
OutgoingHeader h{/* init from bytes here */};
return h; // variant has implicit ctor to accept OutgoingHeader object
}
现在您可以使用手工制作visitor键入以获取所需的值:
struct my_header_visitor
{
typedef void result_type;
explicit my_header_visitor(some_context& ctx)
: ctx_{ctx}
{}
template<class T>
result_type operator()(T const&)
{
// throw whatever error, due to unexpected dispatched type
}
result_type operator()(OutgoingHeader const& h)
{
// handle OutgoingHeader
ctx_.do_smth_with_outgoing_header(h);
}
result_type operator()(IncomingHeader const& h)
{
// handle IncomingHeader
ctx_.do_smth_with_incoming_header(h);
}
private:
some_context& ctx_;
};
my_header_visitor v{/* pass context here */};
message_header h {/* some init code here */};
boost::apply_visitor(v, h);
附言如果你有兴趣了解为什么需要 variant 或调度是如何工作的,你可以阅读 Dr. Dobbs 中 Andrei Alexandrescu 关于受歧视 union 的系列文章:
关于c++ - 如果存在则返回一个成员变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47262451/