Apollo  6.0
Open source self driving car software
bridge_proto_serialized_buf.h
Go to the documentation of this file.
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 
26 namespace apollo {
27 namespace bridge {
28 
29 template <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 
56 template <typename T>
58  for (auto frame : frames_) {
59  FREE_ARRY(frame.buf_);
60  }
61 }
62 
63 template <typename T>
64 bool 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 SetTotalFrames(uint32_t total_frames)
Definition: bridge_header.h:73
void SetMsgName(const std::string &msg_name)
Definition: bridge_header.h:63
const char * GetSerializedBuf(size_t index) const
Definition: bridge_proto_serialized_buf.h:38
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void SetMsgSize(bsize msg_size)
Definition: bridge_header.h:98
Definition: bridge_header.h:33
bool Serialize(const std::shared_ptr< T > &proto, const std::string &msg_name)
Definition: bridge_proto_serialized_buf.h:64
void SetFramePos(bsize frame_pos)
Definition: bridge_header.h:83
uint32_t bsize
Definition: bridge_header_item.h:24
Definition: bridge_proto_serialized_buf.h:30
size_t GetSerializedBufCount() const
Definition: bridge_proto_serialized_buf.h:41
#define FREE_ARRY(arry)
Definition: macro.h:24
BridgeProtoSerializedBuf()
Definition: bridge_proto_serialized_buf.h:32
void SetIndex(uint32_t index)
Definition: bridge_header.h:88
~BridgeProtoSerializedBuf()
Definition: bridge_proto_serialized_buf.h:57
size_t GetSerializedBufSize(size_t index) const
Definition: bridge_proto_serialized_buf.h:42
constexpr uint32_t FRAME_SIZE
Definition: macro.h:36
void SetFrameSize(bsize frame_size)
Definition: bridge_header.h:78
void SetMsgID(uint32_t msg_id)
Definition: bridge_header.h:68
void SetHeaderVer(uint32_t header_ver)
Definition: bridge_header.h:58
void SetTimeStamp(double time_stamp)
Definition: bridge_header.h:93
hsize GetHeaderSize() const
Definition: bridge_header.h:44
uint32_t hsize
Definition: bridge_header.h:27
bool Serialize(char *buf, size_t size)