BAOS SDK v2  1.0.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
TcpIO.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2002-2023 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_TCP_IO_H__
14 #define __BAOSLIB_TCP_IO_H__
15 
16 #include "baos/BaosLib_export.h"
17 #include "baos/config.h"
19 
20 #include "wzcpp/features/concurrent/executor.h"
21 
23 #include <array>
24 #include <atomic>
25 #include <memory>
26 #include <string>
28 
29 
35 namespace wz::baos::ip
36 {
37 
38 class Ip4Driver;
39 
45 class BAOSLIB_EXPORT PacketizerBase
46 {
47 public:
48  using UPtr = std::unique_ptr<PacketizerBase>;
49  PacketizerBase() = delete;
50 
58  PacketizerBase(const Buffer& startPattern, std::uint16_t sizePosition, std::uint16_t sizeBytes)
59  : startPattern_{startPattern},
60  sizePosition_{sizePosition},
61  sizeBytes_{sizeBytes}
62  {
63  }
64  virtual ~PacketizerBase() = default;
65 
73  virtual bool packetize(const Buffer& buf,
74  std::uint16_t bufferSize,
75  std::int32_t& offset,
76  std::uint16_t& length) = 0;
77 
78 protected:
80  const std::int32_t sizePosition_{-1};
81  const std::uint16_t sizeBytes_{0};
82 };
83 
87 class BAOSLIB_EXPORT KnxNetIPPacketizer : public PacketizerBase
88 {
89 public:
90  using UPtr = std::unique_ptr<KnxNetIPPacketizer>;
91 
96  KnxNetIPPacketizer() : PacketizerBase({0x06, 0x20, 0xF0, 0x80}, 4, 2)
97  {
98  }
99 
100  bool packetize(const Buffer& buf, std::uint16_t bufferSize, std::int32_t& offset, std::uint16_t& length) override;
101 };
102 
103 
104 /******************************************************************
105  * TcpIO
106  ******************************************************************/
107 
113 class BAOSLIB_EXPORT TcpIO
114 {
115 public:
116  using UPtr = std::unique_ptr<TcpIO>;
117  using UniqueLock = std::unique_lock<std::mutex>;
118 
125  TcpIO(BaosMsgQueue& msgQueue, wzcpp::ExecutorBase& executor);
126 
128 
135  TcpIO(BaosMsgQueue& msgQueue, std::unique_ptr<Ip4Driver>& driver, wzcpp::ExecutorBase& executor);
136 
142  Ip4Driver& getDriver()
143  {
144  return *driver_;
145  }
147 
148  ~TcpIO();
149 
155  void send(const Buffer& buf);
156 
163  void open(const std::string& ip4Address, std::uint16_t port);
164 
168  void close();
169 
174 
180  bool isOpen()
181  {
182  return isOpen_;
183  };
184 
185 
186 private:
187  void rxHandler();
188 
189 protected:
190  static std::atomic<std::uint64_t> msgId;
191  std::unique_ptr<Ip4Driver> driver_;
192  std::atomic<bool> rxShouldStop_{false};
193  std::condition_variable cvFinished_;
194  mutable std::mutex cvFinished_mut_;
195  std::atomic<bool> stopped_{false};
197 
198  std::atomic<bool> isOpen_{false};
201  wzcpp::ExecutorBase& executor_;
202 };
203 
204 } // namespace wz::baos::ip
205 
206 #endif //__BAOSLIB_TCP_IO_H__
Code regarding Input/Output Events.
Specializaton for a Packetizer which detects KnxNet/Ip packets.
Definition: TcpIO.h:88
bool packetize(const Buffer &buf, std::uint16_t bufferSize, std::int32_t &offset, std::uint16_t &length) override
KnxNetIPPacketizer()
Construct a new Knx Net IP Packetizer object with given pattern. size position and length.
Definition: TcpIO.h:96
To find packets in the TCP byte stream a pattern and the following toatl packet length are used.
Definition: TcpIO.h:46
const Buffer startPattern_
The pattern which marks the beginning of a packet in the byte stream.
Definition: TcpIO.h:79
PacketizerBase(const Buffer &startPattern, std::uint16_t sizePosition, std::uint16_t sizeBytes)
Construct a new Packetizer Base object from a given pattern.
Definition: TcpIO.h:58
virtual bool packetize(const Buffer &buf, std::uint16_t bufferSize, std::int32_t &offset, std::uint16_t &length)=0
std::unique_ptr< PacketizerBase > UPtr
Alias for a std::unique_ptr to this class.
Definition: TcpIO.h:48
Base class for a TCP input output connection.
Definition: TcpIO.h:114
bool isOpen()
Get the open state of the TCP connection.
Definition: TcpIO.h:180
void close()
Close the TCP connection.
std::unique_lock< std::mutex > UniqueLock
Alias for unique lock used by this class.
Definition: TcpIO.h:117
BaosMsgQueue & msgQueue_
Reference to the message queue in which events will be posted by this class.
Definition: TcpIO.h:200
void open(const std::string &ip4Address, std::uint16_t port)
Open the TCP connection to the remote device.
TcpIO(BaosMsgQueue &msgQueue, wzcpp::ExecutorBase &executor)
Construct a new TcpIO object with a given message queue and executor.
std::condition_variable cvFinished_
Condition variable which is set when the receiver worker has stopped.
Definition: TcpIO.h:193
void waitForFinished()
Wait till the worker of this class has finished.
std::unique_ptr< TcpIO > UPtr
Alias for a std::unique_ptr to this class.
Definition: TcpIO.h:116
void send(const Buffer &buf)
Send a byte list out.
std::mutex cvFinished_mut_
The correponding mutex for the cvFinished_ condition variable.
Definition: TcpIO.h:194
std::unique_ptr< Ip4Driver > driver_
The driver to use for actual sending and receiving.
Definition: TcpIO.h:191
wzcpp::ExecutorBase & executor_
Reference to the executor to use for running the receive(RX) worker.
Definition: TcpIO.h:201
static std::atomic< std::uint64_t > msgId
Used to generate a continuous unique message id.
Definition: TcpIO.h:190
PacketizerBase::UPtr packetizer_
TCP stream packetizer used by this class.
Definition: TcpIO.h:199
Global types and configuration for the whole SDK.
std::vector< std::uint8_t > Buffer
An alias type for a byte buffer.
Definition: config.h:64
wzcpp::ConcurrentDeque< IOEvent > BaosMsgQueue
Alias for a concurrent queue with IOEvents.
Definition: IOEvent.h:117