Apollo 9.0
自动驾驶开放平台
message_info.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
18
19#include <cstring>
20
21#include "cyber/common/log.h"
22
23namespace apollo {
24namespace cyber {
25namespace transport {
26
27const std::size_t MessageInfo::kSize = 2 * ID_SIZE + sizeof(uint64_t) + \
28 sizeof(uint64_t) + sizeof(int32_t) + \
29 sizeof(uint64_t);
30
31MessageInfo::MessageInfo() : sender_id_(false), spare_id_(false) {}
32
33MessageInfo::MessageInfo(const Identity& sender_id, uint64_t seq_num)
34 : sender_id_(sender_id), seq_num_(seq_num), spare_id_(false) {}
35
36MessageInfo::MessageInfo(const Identity& sender_id, uint64_t seq_num,
37 const Identity& spare_id)
38 : sender_id_(sender_id), seq_num_(seq_num), spare_id_(spare_id) {}
39
41 : sender_id_(another.sender_id_),
42 channel_id_(another.channel_id_),
43 seq_num_(another.seq_num_),
44 spare_id_(another.spare_id_) {}
45
47
49 if (this != &another) {
50 sender_id_ = another.sender_id_;
51 channel_id_ = another.channel_id_;
52 seq_num_ = another.seq_num_;
53 spare_id_ = another.spare_id_;
54 }
55 return *this;
56}
57
58bool MessageInfo::operator==(const MessageInfo& another) const {
59 return sender_id_ == another.sender_id_ &&
60 channel_id_ == another.channel_id_ && seq_num_ == another.seq_num_ &&
61 spare_id_ == another.spare_id_;
62}
63
64bool MessageInfo::operator!=(const MessageInfo& another) const {
65 return !(*this == another);
66}
67
68bool MessageInfo::SerializeTo(std::string* dst) const {
69 RETURN_VAL_IF_NULL(dst, false);
70
71 dst->assign(sender_id_.data(), ID_SIZE);
72 dst->append(
73 reinterpret_cast<const char*>(&channel_id_), sizeof(channel_id_));
74 dst->append(
75 reinterpret_cast<const char*>(&seq_num_), sizeof(seq_num_));
76 dst->append(spare_id_.data(), ID_SIZE);
77 dst->append(reinterpret_cast<const char*>(
78 &msg_seq_num_), sizeof(msg_seq_num_));
79 dst->append(reinterpret_cast<const char*>(
80 &send_time_), sizeof(send_time_));
81 return true;
82}
83
84bool MessageInfo::SerializeTo(char* dst, std::size_t len) const {
85 if (dst == nullptr || len < kSize) {
86 return false;
87 }
88
89 char* ptr = dst;
90 std::memcpy(ptr, sender_id_.data(), ID_SIZE);
91 ptr += ID_SIZE;
92 std::memcpy(ptr,
93 reinterpret_cast<const char*>(&channel_id_), sizeof(channel_id_));
94 ptr += sizeof(channel_id_);
95 std::memcpy(ptr,
96 reinterpret_cast<const char*>(&seq_num_), sizeof(seq_num_));
97 ptr += sizeof(seq_num_);
98 std::memcpy(ptr, spare_id_.data(), ID_SIZE);
99 ptr += ID_SIZE;
100 std::memcpy(ptr,
101 reinterpret_cast<const char*>(&msg_seq_num_), sizeof(msg_seq_num_));
102 ptr += sizeof(msg_seq_num_);
103 std::memcpy(ptr,
104 reinterpret_cast<const char*>(&send_time_), sizeof(send_time_));
105 return true;
106}
107
108bool MessageInfo::DeserializeFrom(const std::string& src) {
109 return DeserializeFrom(src.data(), src.size());
110}
111
112bool MessageInfo::DeserializeFrom(const char* src, std::size_t len) {
113 RETURN_VAL_IF_NULL(src, false);
114 if (len != kSize) {
115 AWARN << "src size mismatch, given[" << len << "] target[" << kSize << "]";
116 return false;
117 }
118
119 char* ptr = const_cast<char*>(src);
120 sender_id_.set_data(ptr);
121 ptr += ID_SIZE;
122 std::memcpy(
123 reinterpret_cast<char*>(&channel_id_), ptr, sizeof(channel_id_));
124 ptr += sizeof(channel_id_);
125 std::memcpy(
126 reinterpret_cast<char*>(&seq_num_), ptr, sizeof(seq_num_));
127 ptr += sizeof(seq_num_);
128 spare_id_.set_data(ptr);
129 ptr += ID_SIZE;
130 std::memcpy(
131 reinterpret_cast<char*>(&msg_seq_num_), ptr, sizeof(msg_seq_num_));
132 ptr += sizeof(msg_seq_num_);
133 std::memcpy(
134 reinterpret_cast<char*>(&send_time_), ptr, sizeof(send_time_));
135 return true;
136}
137
138} // namespace transport
139} // namespace cyber
140} // namespace apollo
const char * data() const
Definition identity.h:44
void set_data(const char *data)
Definition identity.h:45
bool operator!=(const MessageInfo &another) const
bool DeserializeFrom(const std::string &src)
static const std::size_t kSize
bool SerializeTo(std::string *dst) const
bool operator==(const MessageInfo &another) const
MessageInfo & operator=(const MessageInfo &another)
#define RETURN_VAL_IF_NULL(ptr, val)
Definition log.h:98
#define AWARN
Definition log.h:43
constexpr uint8_t ID_SIZE
Definition identity.h:28
class register implement