ApolloAuto / apollo

An open autonomous driving platform
Apache License 2.0
25.25k stars 9.72k forks source link

getMaxCdrSerializedSize/getCdrSerializedSize 函数中的运算是什么含义? #15488

Closed wanggujin closed 3 months ago

wanggujin commented 3 months ago

版本: r8.0.0 平台: x86_64

size_t UnderlayMessage::getMaxCdrSerializedSize(size_t current_alignment) {
  size_t initial_alignment = current_alignment;

  current_alignment +=
      4 + eprosima::fastcdr::Cdr::alignment(current_alignment, 4);

  current_alignment +=
      4 + eprosima::fastcdr::Cdr::alignment(current_alignment, 4);

  current_alignment +=
      4 + eprosima::fastcdr::Cdr::alignment(current_alignment, 4) + 255 + 1;

  current_alignment +=
      4 + eprosima::fastcdr::Cdr::alignment(current_alignment, 4) + 255 + 1;

  return current_alignment - initial_alignment;
}

size_t UnderlayMessage::getCdrSerializedSize(const UnderlayMessage& data,
                                             size_t current_alignment) {
  size_t initial_alignment = current_alignment;

  current_alignment +=
      4 + eprosima::fastcdr::Cdr::alignment(current_alignment, 4);

  current_alignment +=
      4 + eprosima::fastcdr::Cdr::alignment(current_alignment, 4);

  current_alignment += 4 +
                       eprosima::fastcdr::Cdr::alignment(current_alignment, 4) +
                       data.data().size() + 1;

  current_alignment += 4 +
                       eprosima::fastcdr::Cdr::alignment(current_alignment, 4) +
                       data.datatype().size() + 1;

  return current_alignment - initial_alignment;
}

请问,这两个函数中的运算是什么含义?每一次计算,是什么意思?

nagareru commented 3 months ago

这段代码其实是fastrtps(fastdds)的idl生成工具生成的,可以去fastcdr的git里看看它序列化的原理

daohu527 commented 3 months ago

Yes, it is true, but I am not sure whether these methods are automatically generated.

Fastdds uses IDL files to define messages, and then generates C++ files through Fast DDS-Gen

If you have time, we can try to reproduce it.

wanggujin commented 3 months ago

I also don't know if these code are automatically generated and i can't repduce it. Because i don't know which version of the fast-dds-gen to use and there is no idl files in apollo repo. @daohu527

nagareru commented 3 months ago

Apollo 使用的是fastrtps-1.5.0 可以根据underlay_message.h反推得到原始idl的内容:

struct UnderlayMessage { long timestamp; long seq; string data; string datatype; };

我尝试使用配套的fastrtpsgen重新生成了代码,内容基本是相同的。