VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
subscriber.h File Reference

Type-safe event-model subscriber for VLink topics. More...

#include <functional>
#include <memory>
#include <string>
#include "./impl/subscriber_impl.h"
#include "./node.h"
#include "./internal/subscriber-inl.h"
Include dependency graph for subscriber.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

 Type-safe subscriber for the VLink event communication model. More...
 Convenience alias for Subscriber with message security enabled. More...

Namespaces

Detailed Description

Type-safe event-model subscriber for VLink topics.

Subscriber<MsgT, SecT> is the read side of the VLink event model. It registers a callback that is invoked with each deserialized MsgT message delivered from any matching Publisher on the same URL.

Event Model Data Flow
Publisher<T> Transport Back-end Subscriber<T>
|-- publish(msg) --------> | |
| serialize(msg) |-- bytes delivery ---------> |
| | |--> callback(msg)
| | | deserialize(bytes)
Supported Message Types
Category Type Serializer
Raw bytes Bytes kBytesType
Protobuf MyProto (MessageLite) kProtoType
Protobuf ptr MyProto* (Arena-managed) kProtoPtrType
FlatBuffers MyTableT (NativeTable) kFlatTableType
FlatBuffers MyTable* (Table ptr) kFlatPtrType
CDR (DDS only) MyCdrType kCdrType
Standard layout POD struct / trivial type kStandardType
String std::string kStringType
Custom T (has operator>>/<<) kCustomType
Basic Usage
Subscriber<MyMsg> sub("dds://vehicle/speed");
sub.listen([](const MyMsg& msg) {
std::cout << "speed: " << msg.value << std::endl;
});
Zero-copy Intra Transport
When MsgT is a shared pointer type whose element_type derives from IntraDataType (generated via VLINK_INTRA_DATA_DECLARE) and the URL uses intra://, the shared pointer is forwarded zero-copy (no serialization):
VLINK_INTRA_DATA_DECLARE(MyProtoMsg, MyIntra)
Subscriber<MyIntra> sub("intra://my_topic");
sub.listen([](const MyIntra& data) { ... });
#define VLINK_INTRA_DATA_DECLARE(target_type, declare_name)
Generates a typed in-process message container pair.
Definition intra_data.h:123
Latency and Sample-loss Tracking
Subscriber<MyMsg> sub("dds://my_topic");
sub.set_latency_and_lost_enabled(true);
// ... after receiving messages:
int64_t latency_us = sub.get_latency();
SampleLostInfo lost = sub.get_lost();
std::cout << "lost: " << lost.lost << " / " << lost.total << std::endl;
Note
Calling listen() more than once is a fatal error. The subscriber must be initialised before listen() is called.
Template Parameters
MsgTMessage type. Must satisfy Serializer::is_supported().
SecTSecurity mode; defaults to SecurityType::kWithoutSecurity.