Sen API
Sen Libraries
Loading...
Searching...
No Matches
include/sen/kernel/transport.h
Go to the documentation of this file.
1// === transport.h =====================================================================================================
2// Sen Infrastructure
3// Released under the Apache License v2.0 (SPDX-License-Identifier Apache-2.0).
4// See the LICENSE.txt file for more information.
5// © Airbus SAS, Airbus Helicopters, and Airbus Defence and Space SAU/GmbH/SAS.
6// =====================================================================================================================
7
8#ifndef SEN_KERNEL_TRANSPORT_H
9#define SEN_KERNEL_TRANSPORT_H
10
11// sen
17#include "sen/core/base/span.h"
21#include "sen/kernel/tracer.h"
22
23// generated code
24#include "stl/sen/kernel/basic_types.stl.h"
25
26// std
27#include <chrono>
28#include <cstddef>
29#include <cstdint>
30#include <functional>
31#include <memory>
32#include <string>
33#include <vector>
34
36SEN_STRONG_TYPE(TimerId, uint64_t)
38
39namespace sen::kernel
40{
43
44SEN_STRONG_TYPE(ProcessId, uint32_t)
45
46struct BusId: public StrongType<uint32_t, BusId>
47{
49 using Base::Base;
50 using Base::ValueType;
51
52 void* userData = nullptr;
53};
54
57{
58 ProcessId proc;
59 ObjectOwnerId id;
60
61 void* userData = nullptr;
62};
63
64[[nodiscard]] bool operator==(const ParticipantAddr& lhs, const ParticipantAddr& rhs) noexcept;
65[[nodiscard]] bool operator!=(const ParticipantAddr& lhs, const ParticipantAddr& rhs) noexcept;
66
68constexpr std::size_t maxBestEffortMessageSize = 65527U;
69
71using BestEffortBlockPtr = std::shared_ptr<FixedMemoryBlock>;
72
74using ReliableBlockPtr = std::shared_ptr<ResizableHeapBlock>;
75
77using BestEffortBufferList = std::vector<BestEffortBlockPtr>;
78
80[[nodiscard]] uint32_t getKernelProtocolVersion() noexcept;
81
82class UniqueByteBufferManagerImpl;
83
85{
86 SEN_MOVE_ONLY(UniqueByteBufferManager)
87 using BufferType = std::vector<uint8_t>;
88
89public:
90 using ByteBufferHandle = std::unique_ptr<BufferType, sen::std_util::move_only_function<void(BufferType*)>>;
91
94
99
100private:
101 std::unique_ptr<UniqueByteBufferManagerImpl> pImpl_;
102};
103
106{
107 SEN_NOCOPY_NOMOVE(TransportListener)
108
109public: // special members
110 TransportListener() = default;
111 virtual ~TransportListener() = default;
112
113public:
116
119
120public:
122 virtual void remoteProcessLost(ProcessId who) = 0;
123
126 const ProcessInfo& processInfo,
127 BusId bus,
128 std::string busName) = 0;
129
131 virtual void remoteParticipantLeftBus(ParticipantAddr addr, BusId bus, std::string busName) = 0;
132
135
137 virtual void remoteMessageReceived(ObjectOwnerId to,
138 BusId busId,
140 ByteBufferHandle buffer,
141 bool ensureNotDropped) = 0;
142};
143
146{
147 std::size_t udpSentBytes = 0;
148 std::size_t udpReceivedBytes = 0;
149 std::size_t tcpSentBytes = 0;
150 std::size_t tcpReceivedBytes = 0;
151};
152
155{
156 SEN_NOCOPY_NOMOVE(Transport)
157
158public:
160 explicit Transport(std::string_view sessionName);
161 virtual ~Transport() = default;
162
163public:
166 virtual void start(TransportListener* listener) = 0;
167
169 virtual void sendTo(ParticipantAddr& to, BusId& busId, TransportMode mode, MemBlockPtr data) = 0;
170
172 virtual void sendTo(ParticipantAddr& to, BusId& busId, TransportMode mode, MemBlockPtr data1, MemBlockPtr data2) = 0;
173
175 virtual void localParticipantJoinedBus(ObjectOwnerId participant, BusId bus, const std::string& busName) = 0;
176
178 virtual void localParticipantLeftBus(ObjectOwnerId participant, BusId bus, const std::string& busName) = 0;
179
181 [[nodiscard]] virtual const ProcessInfo& getOwnInfo() const noexcept = 0;
182
184 virtual void stop() noexcept = 0;
185
186 virtual void stopIO() noexcept {}
187
189 [[nodiscard]] virtual TransportStats fetchStats() const = 0;
190
191public: // timers
193 virtual TimerId startTimer(std::chrono::steady_clock::duration timeout, std::function<void()>&& timeoutCallback) = 0;
194
196 virtual bool cancelTimer(TimerId id) = 0;
197
198private:
199 std::string name_;
200};
201
203using TransportFactory = std::function<std::unique_ptr<Transport>(const std::string&, std::unique_ptr<Tracer> tracer)>;
204
206} // namespace sen::kernel
207
208namespace sen
209{
210template <>
211struct ShouldBePassedByValue<kernel::ProcessId>: std::true_type
212{
213};
214
215template <>
216struct ShouldBePassedByValue<kernel::BusId>: std::true_type
217{
218};
219} // namespace sen
220
222
223SEN_STRONG_TYPE_HASHABLE(sen::kernel::ProcessId)
224
225//----------------------------------------------------------------------------------------------------------------------
226// Inline implementation
227//----------------------------------------------------------------------------------------------------------------------
228
229template <>
230struct std::hash<sen::kernel::ParticipantAddr>
231{
232 template <class T>
233 void hashCombine(std::size_t& seed, const T& v) const noexcept
234 {
235 std::hash<T> hasher;
236 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); // NOLINT
237 }
238
239 std::size_t operator()(const sen::kernel::ParticipantAddr& val) const noexcept
240 {
241 std::size_t seed = 150783; // NOLINT(readability-magic-numbers)
242 hashCombine(seed, val.proc);
243 hashCombine(seed, val.id);
244 return seed;
245 }
246};
247
248#endif // SEN_KERNEL_TRANSPORT_H
Here we define a set of template meta-programming helpers to let the compiler take some decisions bas...
Contiguous view of elements of type T. Inspired by http://www.open-std.org/jtc1/sc22/wg21/docs/papers...
Definition span.h:34
CRTP class that wraps T to make it a strong type.
Definition strong_type.h:72
typename StrongTypeBase< uint32_t >::ValueType ValueType
Definition strong_type.h:75
virtual TransportStats fetchStats() const =0
Fetch the current statistics.
virtual void localParticipantLeftBus(ObjectOwnerId participant, BusId bus, const std::string &busName)=0
Notify other participants that there's a local participant that recently left a bus.
virtual void sendTo(ParticipantAddr &to, BusId &busId, TransportMode mode, MemBlockPtr data1, MemBlockPtr data2)=0
Send a compound message (typically header and data) over a bus to a participant using a given mode.
virtual void stop() noexcept=0
Stop the message exchange.
virtual void sendTo(ParticipantAddr &to, BusId &busId, TransportMode mode, MemBlockPtr data)=0
Send a message over a bus to a participant using a given mode.
virtual ~Transport()=default
virtual const ProcessInfo & getOwnInfo() const noexcept=0
The process info of this process.
virtual bool cancelTimer(TimerId id)=0
Cancels a timer, avoiding its expiration.
Transport(std::string_view sessionName)
A transport instance is created per connected session (which is unique within a kernel).
virtual void stopIO() noexcept
Definition include/sen/kernel/transport.h:186
virtual void start(TransportListener *listener)=0
Start the message exchange, and set the listener that will react to inputs. This method is non-blocki...
virtual void localParticipantJoinedBus(ObjectOwnerId participant, BusId bus, const std::string &busName)=0
Notify other participants that there's a local participant that recently joined a bus.
virtual TimerId startTimer(std::chrono::steady_clock::duration timeout, std::function< void()> &&timeoutCallback)=0
Starts an asio asynchronous timer that executes a callback when the steady_clock timeout is reached.
Interface for the internal Sen element that reacts to transport events.
Definition include/sen/kernel/transport.h:106
virtual void remoteParticipantLeftBus(ParticipantAddr addr, BusId bus, std::string busName)=0
Some participant left a bus.
virtual ByteBufferManager & getBufferManager()=0
Returns access to the buffer manager for easy access to memory buffers.
virtual void remoteMessageReceived(ObjectOwnerId to, BusId busId, Span< const uint8_t > msg, ByteBufferHandle buffer, bool ensureNotDropped)=0
Direct message received on a bus (independently of the communication channel).
virtual ~TransportListener()=default
ByteBufferManager::ByteBufferHandle ByteBufferHandle
Definition include/sen/kernel/transport.h:115
virtual void remoteParticipantJoinedBus(ParticipantAddr addr, const ProcessInfo &processInfo, BusId bus, std::string busName)=0
Some participant is part of, or recently joined a bus.
UniqueByteBufferManager ByteBufferManager
Definition include/sen/kernel/transport.h:114
virtual void remoteProcessLost(ProcessId who)=0
A process is not reachable anymore.
virtual void remoteBroadcastMessageReceived(BusId busId, Span< const uint8_t > msg, ByteBufferHandle buffer)=0
Message received on a bus via broadcast.
Definition include/sen/kernel/transport.h:85
ByteBufferHandle getBuffer(size_t size)
Returns a handle to a buffer that fix size elements.
std::unique_ptr< BufferType, sen::std_util::move_only_function< void(BufferType *)> > ByteBufferHandle
Definition include/sen/kernel/transport.h:90
void * userData
Allows transport implementations to inject information.
Definition include/sen/kernel/transport.h:61
ObjectOwnerId id
Definition include/sen/kernel/transport.h:59
std::size_t udpSentBytes
Definition include/sen/kernel/transport.h:147
std::size_t tcpReceivedBytes
Definition include/sen/kernel/transport.h:150
ProcessId proc
Definition include/sen/kernel/transport.h:58
std::size_t tcpSentBytes
Definition include/sen/kernel/transport.h:149
std::size_t udpReceivedBytes
Definition include/sen/kernel/transport.h:148
std::shared_ptr< FixedMemoryBlock > BestEffortBlockPtr
Pointer to a memory block of fixed size, used for best effort communication.
Definition include/sen/kernel/transport.h:71
bool operator!=(const ParticipantAddr &lhs, const ParticipantAddr &rhs) noexcept
std::vector< BestEffortBlockPtr > BestEffortBufferList
A list (vector) of fixed-size memory blocks.
Definition include/sen/kernel/transport.h:77
uint32_t getKernelProtocolVersion() noexcept
The protocol version of kernel messaging.
bool operator==(const ParticipantAddr &lhs, const ParticipantAddr &rhs) noexcept
constexpr std::size_t maxBestEffortMessageSize
The maximum UDP payload size.
Definition include/sen/kernel/transport.h:68
std::function< std::unique_ptr< Transport >(const std::string &, std::unique_ptr< Tracer > tracer)> TransportFactory
A function that creates a transport given a session name.
Definition include/sen/kernel/transport.h:203
std::shared_ptr< ResizableHeapBlock > ReliableBlockPtr
Pointer to a resizable memory block, used for reliable communication.
Definition include/sen/kernel/transport.h:74
To identify remote participants.
Definition include/sen/kernel/transport.h:57
Global transport statistics.
Definition include/sen/kernel/transport.h:146
TransportMode
How to transport information.
Definition type.h:56
Strong type for the unique identifier for a transport timer.
Definition native_object_impl.h:39
detail::MoveOnlyFunctionImpl< FwdArgs... > move_only_function
Definition move_only_function.h:256
Definition assert.h:17
#define SEN_STRONG_TYPE_HASHABLE(name)
Makes your strong type std::hashable. NOLINTNEXTLINE.
Definition strong_type.h:188
#define SEN_STRONG_TYPE(name, native_type)
Helper macro to define strong types. NOLINTNEXTLINE.
Definition strong_type.h:179
Utility to indicate that your class wants to be passed by value in some of the library calls.
Definition class_helpers.h:34
Definition include/sen/kernel/transport.h:47
void * userData
Allows transport implementations to inject information.
Definition include/sen/kernel/transport.h:52
StrongType< uint32_t, BusId > Base
Definition include/sen/kernel/transport.h:48
std::size_t operator()(const sen::kernel::ParticipantAddr &val) const noexcept
Definition include/sen/kernel/transport.h:239
void hashCombine(std::size_t &seed, const T &v) const noexcept
Definition include/sen/kernel/transport.h:233