BAOS SDK v2  1.2.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
Loading...
Searching...
No Matches
TransportIO.h
Go to the documentation of this file.
1//
2// Copyright (c) 2002-2026 WEINZIERL ENGINEERING GmbH
3// All rights reserved.
4//
5// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
8// SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
9// WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
10// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
11//
12
13#ifndef __BAOSLIB_TRANSPORT_IO_H__
14#define __BAOSLIB_TRANSPORT_IO_H__
15
16#include "baos/BaosLib_export.h"
17#include "baos/config.h"
19
20#include "transport/ProtocolStack.h"
21
22#include "wzcpp/features/concurrent/executor.h"
23#include "wzcpp/features/timer/timer.h"
24
25
27#include <array>
28#include <atomic>
29#include <chrono>
30#include <memory>
31#include <string>
33
34
40namespace wz::baos::transport
41{
42
43class ProtocolLayer;
44class ProtocolStack;
45
46/******************************************************************
47 * TransportIO
48 ******************************************************************/
49
55class BAOSLIB_EXPORT TransportIO
56{
57public:
58 using Clock = std::chrono::system_clock;
59 using UPtr = std::unique_ptr<TransportIO>;
60 using UniqueLock = std::unique_lock<std::mutex>;
61 enum class PromiseState
62 {
63 UNUSED,
64 SET,
65 RESOLVED
66 };
68 {
69 protocol::ResponsePromise prom; // Promise from the BAOS connection to resolve later
70 PromiseState state{PromiseState::UNUSED};
71 };
72
81 wzcpp::ExecutorBase& executor,
82 wzcpp::TimerManager<Clock>& timerManager);
83
84 virtual ~TransportIO();
85
89 TransportIO(const TransportIO&) = delete;
90
95
100
105
106 template <typename LayerType>
107 void addLayer(std::uint32_t layerId)
108 {
109 transportStack_->addLayer(std::make_unique<LayerType>(layerId, *transportStack_, timerManager_));
110 }
111
112 ProtocolLayer& getLayer(unsigned int layerId);
113
117 void start();
118
122 virtual void stop() = 0;
123
129 virtual void send(const Buffer& frame) = 0;
130
131
135 virtual void close() = 0;
136
140 virtual void waitForFinished() = 0;
141
147 virtual bool isOpen();
148
154 bool isIdle() const;
155
156 void onTimersElapsed(wzcpp::TimerManager<Clock>::TimerIdList& timerIds);
157
158protected:
162 virtual void eventHandler() = 0;
163
164 virtual void sendToIo(const Buffer& frame) = 0;
165
166protected:
167 static std::atomic<std::uint64_t> msgId;
168 std::unique_ptr<ProtocolStack> transportStack_;
169 std::atomic<bool> shouldStop_{false};
170 std::condition_variable cvFinished_;
171 mutable std::mutex cvFinished_mut_;
172 bool stopped_{false};
174
175 std::atomic<bool> isOpen_{false};
178 wzcpp::ExecutorBase& executor_;
179 wzcpp::TimerManager<Clock>& timerManager_;
180 wzcpp::TimerManager<Clock>::AllTimersCallbackId timerCallbackId_{0};
182};
183
184} // namespace wz::baos::transport
185
186#endif //__BAOSLIB_TRANSPORT_IO_H__
Code regarding Input/Output Events.
Base class for a Transport input output connection.
Definition TransportIO.h:56
std::unique_ptr< ProtocolStack > transportStack_
The transport stack to use for actual sending and receiving.
Definition TransportIO.h:168
virtual void send(const Buffer &frame)=0
Send a baos frame.
virtual void eventHandler()=0
Handles the incoming IOEvents.
virtual void waitForFinished()=0
Wait till the worker of this class has finished.
TransportIO & operator=(const TransportIO &)=delete
TransportMsgQueue msgQueue_
Reference to the message queue in which events will be posted by this class.
Definition TransportIO.h:176
std::chrono::system_clock Clock
An alias for the clock internaly used.
Definition TransportIO.h:58
std::unique_ptr< TransportIO > UPtr
Alias for a std::unique_ptr to this class.
Definition TransportIO.h:59
std::unique_lock< std::mutex > UniqueLock
Alias for unique lock used by this class.
Definition TransportIO.h:60
wzcpp::TimerManager< Clock > & timerManager_
Reference to the timer manager used by this transport IO.
Definition TransportIO.h:179
BaosPromise connectionPromise_
A promise to be resolved when the connection is established, or disconnected.
Definition TransportIO.h:181
bool isIdle() const
Get the idle state of the Transport connection.
virtual bool isOpen()
Get the open state of the Transport connection.
TransportIO(BaosMsgQueue &msgQueue, wzcpp::ExecutorBase &executor, wzcpp::TimerManager< Clock > &timerManager)
Construct a new TransportIO object with a given message queue and executor.
BaosMsgQueue & bMsgQ_
Reference to the message queue in which baos events will be posted by this class.
Definition TransportIO.h:177
std::condition_variable cvFinished_
Condition variable which is set when the receiver worker has stopped.
Definition TransportIO.h:170
TransportIO(const TransportIO &)=delete
std::mutex cvFinished_mut_
The correponding mutex for the cvFinished_ condition variable.
Definition TransportIO.h:171
wzcpp::ExecutorBase & executor_
Reference to the executor to use for running the receive(RX) worker.
Definition TransportIO.h:178
TransportIO(TransportIO &&)=delete
TransportIO & operator=(TransportIO &&)=delete
virtual void close()=0
Close the Transport connection.
static std::atomic< std::uint64_t > msgId
Used to generate a continuous unique message id.
Definition TransportIO.h:167
Global types and configuration for the whole SDK.
std::promise< BaseResponse::SPtr > ResponsePromise
Definition Response.h:95
std::vector< std::uint8_t > Buffer
An alias type for a byte buffer.
Definition config.h:71
wzcpp::ConcurrentDeque< IOEvent > BaosMsgQueue
Alias for a concurrent queue with IOEvents.
Definition IOEvent.h:96
wzcpp::ConcurrentDeque< IOEvent > TransportMsgQueue
Alias for a concurrent queue with IOEvents used by the transport layer.
Definition IOEvent.h:97