Apollo
10.0
自动驾驶开放平台
bridge_header.cc
浏览该文件的文档.
1
/******************************************************************************
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
#include "
modules/bridge/common/bridge_header.h
"
18
19
#include <cstring>
20
#include <limits>
21
22
namespace
apollo
{
23
namespace
bridge {
24
25
bool
BridgeHeader::Serialize
(
char
*buf,
size_t
size) {
26
if
(!buf || size == 0) {
27
return
false
;
28
}
29
char
*cursor = buf;
30
char
*p_header_size =
nullptr
;
31
cursor = SerializeHeaderFlag(cursor, size);
32
p_header_size = cursor;
33
cursor +=
sizeof
(
hsize
) + 1;
34
for
(
int
i = 0; i <
Header_Tail
; i++) {
35
cursor = header_item[i]->
SerializeItem
(cursor, size);
36
}
37
38
if
(!SerializeHeaderSize(p_header_size, size)) {
39
return
false
;
40
}
41
return
true
;
42
}
43
44
bool
BridgeHeader::Diserialize
(
const
char
*buf,
size_t
buf_size) {
45
const
char
*cursor = buf;
46
47
int
i =
static_cast<
int
>
(buf_size);
48
while
(i > 0) {
49
HType
type = *(
reinterpret_cast<
const
HType
*
>
(cursor));
50
if
(type >=
Header_Tail
|| type < 0) {
51
cursor +=
sizeof
(
HType
) + 1;
52
bsize
size = *(
reinterpret_cast<
const
bsize
*
>
(cursor));
53
cursor +=
sizeof
(
bsize
) + size + 2;
54
if
(size > (std::numeric_limits<int>::max() -
sizeof
(
HType
) -
55
sizeof
(
bsize
) - 3)) {
56
return
false
;
57
}
58
i -=
static_cast<
int
>
(
sizeof
(
HType
) +
sizeof
(
bsize
) + size + 3);
59
continue
;
60
}
61
62
for
(
int
j = 0; j <
Header_Tail
; j++) {
63
if
(type == header_item[j]->GetType()) {
64
size_t
value_size = 0;
65
cursor = header_item[j]->
DiserializeItem
(cursor,
static_cast<
size_t
>
(i),
66
&value_size);
67
i -=
static_cast<
int
>
(value_size);
68
if
(cursor ==
nullptr
) {
69
return
false
;
70
}
71
break
;
72
}
73
}
74
}
75
return
true
;
76
}
77
78
bool
BridgeHeader::IsAvailable
(
const
char
*buf) {
79
if
(!buf) {
80
return
false
;
81
}
82
if
(memcmp(
BRIDGE_HEADER_FLAG
, buf,
sizeof
(
BRIDGE_HEADER_FLAG
) - 1) != 0) {
83
return
false
;
84
}
85
return
true
;
86
}
87
88
char
*BridgeHeader::SerializeHeaderFlag(
char
*buf,
size_t
size) {
89
if
(!buf || size == 0) {
90
return
nullptr
;
91
}
92
return
SerializeBasicType<char, sizeof(BRIDGE_HEADER_FLAG)>(
93
BRIDGE_HEADER_FLAG
, buf, size);
94
}
95
96
char
*BridgeHeader::SerializeHeaderSize(
char
*buf,
size_t
size) {
97
hsize
header_size =
GetHeaderSize
();
98
return
SerializeBasicType<hsize, sizeof(hsize)>(&header_size, buf, size);
99
}
100
101
}
// namespace bridge
102
}
// namespace apollo
bridge_header.h
apollo::bridge::BridgeHeader::Serialize
bool Serialize(char *buf, size_t size)
Definition
bridge_header.cc:25
apollo::bridge::BridgeHeader::Diserialize
bool Diserialize(const char *buf, size_t buf_size)
Definition
bridge_header.cc:44
apollo::bridge::BridgeHeader::GetHeaderSize
hsize GetHeaderSize() const
Definition
bridge_header.h:44
apollo::bridge::BridgeHeader::IsAvailable
bool IsAvailable(const char *buf)
Definition
bridge_header.cc:78
apollo::bridge::HeaderItemBase::SerializeItem
virtual char * SerializeItem(char *buf, size_t buf_size)=0
apollo::bridge::HeaderItemBase::DiserializeItem
virtual const char * DiserializeItem(const char *buf, const size_t buf_size, size_t *diserialized_size)=0
apollo::bridge::hsize
uint32_t hsize
Definition
bridge_header.h:27
apollo::bridge::HType
HType
Definition
bridge_header_item.h:26
apollo::bridge::Header_Tail
@ Header_Tail
Definition
bridge_header_item.h:37
apollo::bridge::BRIDGE_HEADER_FLAG
constexpr char BRIDGE_HEADER_FLAG[]
Definition
bridge_header.h:29
apollo::bridge::bsize
uint32_t bsize
Definition
bridge_header_item.h:24
apollo
class register implement
Definition
arena_queue.h:37
modules
bridge
common
bridge_header.cc