Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ build --cxxopt="-std=c++17"
build --jobs=4
build --verbose_failures
build --keep_going

build --registry=https://bcr.wheelos.cn/
build --registry=https://raw.githubusercontent.com/wheelos/bazel-central-registry/fastdds-2.14.x

# Enable Bzlmod for every Bazel command
Expand Down
4 changes: 4 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ bazel_dep(name = "ncurses", version = "6.4.20221231")
bazel_dep(name = "libuuid", version = "2.39.3.bcr.1")
bazel_dep(name = "fastcdr", version = "2.2.2")
bazel_dep(name = "fastdds", version = "2.14.3")

# FlatBuffers system-installed runtime (header-only, provided via local repo).
flatbuffers_ext = use_extension("//bazel:flatbuffers.bzl", "flatbuffers_extension")
use_repo(flatbuffers_ext, "flatbuffers")
1 change: 1 addition & 0 deletions _codeql_detected_source_root
53 changes: 53 additions & 0 deletions bazel/flatbuffers.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2018 The Apollo Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module extension that exposes the system-installed FlatBuffers headers
as a Bazel repository named @flatbuffers.

The FlatBuffers C++ runtime is header-only, so no library linking is required.
The repository rule simply symlinks the system include directory and writes a
minimal BUILD file that declares a cc_library for the headers.
"""

def _system_flatbuffers_impl(ctx):
include_dir = ctx.os.environ.get(
"FLATBUFFERS_INCLUDE_DIR",
"/usr/include",
)
ctx.symlink(include_dir + "/flatbuffers", "flatbuffers")
ctx.file("BUILD", """
load("@rules_cc//cc:defs.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

# Header-only FlatBuffers runtime library.
cc_library(
name = "flatbuffers",
hdrs = glob(["flatbuffers/**/*.h"]),
includes = ["."],
)
""")

_system_flatbuffers = repository_rule(
implementation = _system_flatbuffers_impl,
local = True,
environ = ["FLATBUFFERS_INCLUDE_DIR"],
)

def _flatbuffers_extension_impl(module_ctx):
_system_flatbuffers(name = "flatbuffers")

flatbuffers_extension = module_extension(
implementation = _flatbuffers_extension_impl,
)
21 changes: 21 additions & 0 deletions cyber/examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,24 @@ cc_binary(
"//cyber/proto:record_cc_proto",
],
)

# FlatBuffers zero-copy talker/listener pair.
cc_binary(
name = "talker_fb",
srcs = ["talker_fb.cc"],
deps = [
"//cyber",
"//cyber/examples/proto:examples_fbs",
"//cyber/message:flatbuffers_message",
],
)

cc_binary(
name = "listener_fb",
srcs = ["listener_fb.cc"],
deps = [
"//cyber",
"//cyber/examples/proto:examples_fbs",
"//cyber/message:flatbuffers_message",
],
)
55 changes: 55 additions & 0 deletions cyber/examples/listener_fb.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/******************************************************************************
* Copyright 2018 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/

/**
* @file listener_fb.cc
* @brief FlatBuffers-based listener demonstrating zero-copy message access.
*
* This example mirrors listener.cc but subscribes to FlatBufferMessage
* payloads. On receipt the callback calls GetRoot<Chatter>() to access the
* FlatBuffers root object directly from the buffer — no copy, no allocation.
*/

#include "cyber/cyber.h"
#include "cyber/examples/proto/examples_generated.h"
#include "cyber/message/flatbuffers_message.h"

using apollo::cyber::message::FlatBufferMessage;
using apollo::cyber::examples::proto::Chatter;

void MessageCallback(const std::shared_ptr<FlatBufferMessage>& msg) {
// Zero-copy: access the FlatBuffers root directly from the buffer.
const Chatter* chatter = msg->GetRoot<Chatter>();
if (chatter == nullptr) {
AERROR << "listener_fb: received an empty FlatBufferMessage";
return;
}
AINFO << "listener_fb received seq->" << chatter->seq();
if (chatter->content()) {
AINFO << "listener_fb content->" << chatter->content()->str();
}
}

int main(int argc, char* argv[]) {
apollo::cyber::Init(argv[0]);

auto listener_node = apollo::cyber::CreateNode("listener_fb");
auto listener = listener_node->CreateReader<FlatBufferMessage>(
"channel/chatter_fb", MessageCallback);

apollo::cyber::WaitForShutdown();
return 0;
}
20 changes: 14 additions & 6 deletions cyber/examples/proto/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@rules_cc//cc:defs.bzl", "cc_proto_library")

## Auto generated by `proto_build_generator.py`
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//:bazel/python_rules.bzl", "py_proto_library")
## Auto generated by `proto_build_generator.sh`
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
load("@com_google_protobuf//bazel:cc_proto_library.bzl", "cc_proto_library")
load("@com_google_protobuf//bazel:py_proto_library.bzl", "py_proto_library")
load("@rules_cc//cc:defs.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

Expand All @@ -20,7 +20,15 @@ proto_library(

py_proto_library(
name = "examples_py_pb2",
deps = [":examples_proto"],
)

# FlatBuffers pre-generated header for the Chatter schema.
# Generated from examples.fbs with: flatc --cpp examples.fbs
cc_library(
name = "examples_fbs",
hdrs = ["examples_generated.h"],
deps = [
":examples_proto",
"@flatbuffers//:flatbuffers",
],
)
27 changes: 27 additions & 0 deletions cyber/examples/proto/examples.fbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 The Apollo Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// FlatBuffers schema equivalent to examples.proto
// Enables zero-copy message passing via cyber shared-memory transport.

namespace apollo.cyber.examples.proto;

table Chatter {
timestamp:uint64;
lidar_timestamp:uint64;
seq:uint64;
content:string;
}

root_type Chatter;
145 changes: 145 additions & 0 deletions cyber/examples/proto/examples_generated.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// automatically generated by the FlatBuffers compiler, do not modify

// NOLINTBEGIN

#ifndef FLATBUFFERS_GENERATED_EXAMPLES_APOLLO_CYBER_EXAMPLES_PROTO_H_
#define FLATBUFFERS_GENERATED_EXAMPLES_APOLLO_CYBER_EXAMPLES_PROTO_H_

#include "flatbuffers/flatbuffers.h"

namespace apollo {
namespace cyber {
namespace examples {
namespace proto {

struct Chatter;
struct ChatterBuilder;

struct Chatter FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef ChatterBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_TIMESTAMP = 4,
VT_LIDAR_TIMESTAMP = 6,
VT_SEQ = 8,
VT_CONTENT = 10
};
uint64_t timestamp() const {
return GetField<uint64_t>(VT_TIMESTAMP, 0);
}
uint64_t lidar_timestamp() const {
return GetField<uint64_t>(VT_LIDAR_TIMESTAMP, 0);
}
uint64_t seq() const {
return GetField<uint64_t>(VT_SEQ, 0);
}
const flatbuffers::String *content() const {
return GetPointer<const flatbuffers::String *>(VT_CONTENT);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<uint64_t>(verifier, VT_TIMESTAMP, 8) &&
VerifyField<uint64_t>(verifier, VT_LIDAR_TIMESTAMP, 8) &&
VerifyField<uint64_t>(verifier, VT_SEQ, 8) &&
VerifyOffset(verifier, VT_CONTENT) &&
verifier.VerifyString(content()) &&
verifier.EndTable();
}
};

struct ChatterBuilder {
typedef Chatter Table;
flatbuffers::FlatBufferBuilder &fbb_;
flatbuffers::uoffset_t start_;
void add_timestamp(uint64_t timestamp) {
fbb_.AddElement<uint64_t>(Chatter::VT_TIMESTAMP, timestamp, 0);
}
void add_lidar_timestamp(uint64_t lidar_timestamp) {
fbb_.AddElement<uint64_t>(Chatter::VT_LIDAR_TIMESTAMP, lidar_timestamp, 0);
}
void add_seq(uint64_t seq) {
fbb_.AddElement<uint64_t>(Chatter::VT_SEQ, seq, 0);
}
void add_content(flatbuffers::Offset<flatbuffers::String> content) {
fbb_.AddOffset(Chatter::VT_CONTENT, content);
}
explicit ChatterBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
flatbuffers::Offset<Chatter> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = flatbuffers::Offset<Chatter>(end);
return o;
}
};

inline flatbuffers::Offset<Chatter> CreateChatter(
flatbuffers::FlatBufferBuilder &_fbb,
uint64_t timestamp = 0,
uint64_t lidar_timestamp = 0,
uint64_t seq = 0,
flatbuffers::Offset<flatbuffers::String> content = 0) {
ChatterBuilder builder_(_fbb);
builder_.add_seq(seq);
builder_.add_lidar_timestamp(lidar_timestamp);
builder_.add_timestamp(timestamp);
builder_.add_content(content);
return builder_.Finish();
}

inline flatbuffers::Offset<Chatter> CreateChatterDirect(
flatbuffers::FlatBufferBuilder &_fbb,
uint64_t timestamp = 0,
uint64_t lidar_timestamp = 0,
uint64_t seq = 0,
const char *content = nullptr) {
auto content__ = content ? _fbb.CreateString(content) : 0;
return apollo::cyber::examples::proto::CreateChatter(
_fbb,
timestamp,
lidar_timestamp,
seq,
content__);
}

inline const apollo::cyber::examples::proto::Chatter *GetChatter(
const void *buf) {
return flatbuffers::GetRoot<apollo::cyber::examples::proto::Chatter>(buf);
}

inline const apollo::cyber::examples::proto::Chatter *GetSizePrefixedChatter(
const void *buf) {
return flatbuffers::GetSizePrefixedRoot<
apollo::cyber::examples::proto::Chatter>(buf);
}

inline bool VerifyChatterBuffer(flatbuffers::Verifier &verifier) {
return verifier.VerifyBuffer<apollo::cyber::examples::proto::Chatter>(
nullptr);
}

inline bool VerifySizePrefixedChatterBuffer(flatbuffers::Verifier &verifier) {
return verifier.VerifySizePrefixedBuffer<
apollo::cyber::examples::proto::Chatter>(nullptr);
}

inline void FinishChatterBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<apollo::cyber::examples::proto::Chatter> root) {
fbb.Finish(root);
}

inline void FinishSizePrefixedChatterBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<apollo::cyber::examples::proto::Chatter> root) {
fbb.FinishSizePrefixed(root);
}

} // namespace proto
} // namespace examples
} // namespace cyber
} // namespace apollo

// NOLINTEND

#endif // FLATBUFFERS_GENERATED_EXAMPLES_APOLLO_CYBER_EXAMPLES_PROTO_H_
Loading