Apollo 10.0
自动驾驶开放平台
bridge_proto_serialized_buf.h
浏览该文件的文档.
1/******************************************************************************der
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#pragma once
18
19#include <memory>
20#include <string>
21#include <vector>
22
25
26namespace apollo {
27namespace bridge {
28
29template <typename T>
31 public:
34
35 char *GetFrame(size_t index);
36 bool Serialize(const std::shared_ptr<T> &proto, const std::string &msg_name);
37
38 const char *GetSerializedBuf(size_t index) const {
39 return frames_[index].buf_;
40 }
41 size_t GetSerializedBufCount() const { return frames_.size(); }
42 size_t GetSerializedBufSize(size_t index) const {
43 return frames_[index].buf_len_;
44 }
45
46 private:
47 struct Buf {
48 char *buf_;
49 size_t buf_len_;
50 };
51
52 private:
53 std::vector<Buf> frames_;
54};
55
56template <typename T>
58 for (auto frame : frames_) {
59 FREE_ARRY(frame.buf_);
60 }
61}
62
63template <typename T>
64bool BridgeProtoSerializedBuf<T>::Serialize(const std::shared_ptr<T> &proto,
65 const std::string &msg_name) {
66 bsize msg_len = static_cast<bsize>(proto->ByteSizeLong());
67 char *tmp = new char[msg_len]();
68 if (!proto->SerializeToArray(tmp, static_cast<int>(msg_len))) {
69 FREE_ARRY(tmp);
70 return false;
71 }
72 bsize offset = 0;
73 bsize frame_index = 0;
74 uint32_t total_frames = static_cast<uint32_t>(msg_len / FRAME_SIZE +
75 (msg_len % FRAME_SIZE ? 1 : 0));
76
77 while (offset < msg_len) {
78 bsize left = msg_len - frame_index * FRAME_SIZE;
79 bsize cpy_size = (left > FRAME_SIZE) ? FRAME_SIZE : left;
80
81 BridgeHeader header;
82 header.SetHeaderVer(0);
83 header.SetMsgName(msg_name);
84 header.SetMsgID(proto->header().sequence_num());
85 header.SetTimeStamp(proto->header().timestamp_sec());
86 header.SetMsgSize(msg_len);
87 header.SetTotalFrames(total_frames);
88 header.SetFrameSize(cpy_size);
89 header.SetIndex(frame_index);
90 header.SetFramePos(frame_index * FRAME_SIZE);
91 hsize header_size = header.GetHeaderSize();
92 Buf buf;
93 buf.buf_ = new char[cpy_size + header_size];
94 buf.buf_len_ = cpy_size + header_size;
95 header.Serialize(buf.buf_, buf.buf_len_);
96 memcpy(buf.buf_ + header_size, tmp + frame_index * FRAME_SIZE, cpy_size);
97 frames_.push_back(buf);
98 frame_index++;
99 offset += cpy_size;
100 }
101 FREE_ARRY(tmp);
102 return true;
103}
104
105} // namespace bridge
106} // namespace apollo
void SetMsgSize(bsize msg_size)
void SetTotalFrames(uint32_t total_frames)
void SetHeaderVer(uint32_t header_ver)
void SetMsgID(uint32_t msg_id)
void SetFrameSize(bsize frame_size)
bool Serialize(char *buf, size_t size)
void SetFramePos(bsize frame_pos)
void SetMsgName(const std::string &msg_name)
void SetTimeStamp(double time_stamp)
void SetIndex(uint32_t index)
const char * GetSerializedBuf(size_t index) const
bool Serialize(const std::shared_ptr< T > &proto, const std::string &msg_name)
#define FREE_ARRY(arry)
Definition macro.h:24
constexpr uint32_t FRAME_SIZE
Definition macro.h:36
class register implement
Definition arena_queue.h:37