Apollo 10.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 &send_time_), sizeof(send_time_));
79 return true;
80}
81
82bool MessageInfo::SerializeTo(char* dst, std::size_t len) const {
83 if (dst == nullptr || len < kSize) {
84 return false;
85 }
86
87 char* ptr = dst;
88 std::memcpy(ptr, sender_id_.data(), ID_SIZE);
89 ptr += ID_SIZE;
90 std::memcpy(ptr,
91 reinterpret_cast<const char*>(&channel_id_), sizeof(channel_id_));
92 ptr += sizeof(channel_id_);
93 std::memcpy(ptr,
94 reinterpret_cast<const char*>(&seq_num_), sizeof(seq_num_));
95 ptr += sizeof(seq_num_);
96 std::memcpy(ptr, spare_id_.data(), ID_SIZE);
97 ptr += ID_SIZE;
98 std::memcpy(ptr,
99 reinterpret_cast<const char*>(&send_time_), sizeof(send_time_));
100 return true;
101}
102
103bool MessageInfo::DeserializeFrom(const std::string& src) {
104 return DeserializeFrom(src.data(), src.size());
105}
106
107bool MessageInfo::DeserializeFrom(const char* src, std::size_t len) {
108 RETURN_VAL_IF_NULL(src, false);
109 if (len != kSize) {
110 AWARN << "src size mismatch, given[" << len << "] target[" << kSize << "]";
111 return false;
112 }
113
114 char* ptr = const_cast<char*>(src);
115 sender_id_.set_data(ptr);
116 ptr += ID_SIZE;
117 std::memcpy(
118 reinterpret_cast<char*>(&channel_id_), ptr, sizeof(channel_id_));
119 ptr += sizeof(channel_id_);
120 std::memcpy(
121 reinterpret_cast<char*>(&seq_num_), ptr, sizeof(seq_num_));
122 ptr += sizeof(seq_num_);
123 spare_id_.set_data(ptr);
124 ptr += ID_SIZE;
125 std::memcpy(
126 reinterpret_cast<char*>(&send_time_), ptr, sizeof(send_time_));
127 return true;
128}
129
130} // namespace transport
131} // namespace cyber
132} // 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
Definition arena_queue.h:37