BAOS SDK v2  1.0.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
IOEvent.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_EVENT_H__
14 #define __BAOSLIB_EVENT_H__
15 
16 #include "baos/BaosLib_export.h"
17 #include "baos/config.h"
18 #include "baos/ip/IpGeneral.h"
19 #include "baos/protocol/Response.h"
20 #include "wzcpp/features/concurrent/concurrent_queue.h"
21 
23 #include <future>
24 #include <memory>
25 #include <variant>
27 
33 namespace wz::baos
34 {
35 
41 class BAOSLIB_EXPORT IOEvent
42 {
43 public:
44  using UPtr = std::unique_ptr<IOEvent>;
45  using EventDataType = std::variant<ip::Ip4SocketAddress, std::unique_ptr<Buffer>>;
47 
49  enum class Type
50  {
51  UNKNOWN,
52  OPEN,
53  CLOSE,
54  SOCKETERROR,
55  RESPONSETIMEOUT,
56  HEARTBEAT,
57  RX_PACKET,
58  TX_PACKET
59  };
60  IOEvent() = default;
61 
70  IOEvent(Type eventT, EventDataType&& pkt, protocol::ResponsePromise&& prom, std::uint64_t mID = 0)
71  : eventType{eventT},
72  msgID{mID},
73  responsePromise{std::move(prom)}
74  {
75  data_ = std::move(pkt);
76  }
77 
78 public:
79  Type eventType{Type::UNKNOWN};
80  std::uint64_t msgID{0};
82  bool promiseMoved{false};
84 };
85 
92 inline std::string toString(IOEvent::Type typ)
93 {
94  switch (typ)
95  {
97  return "UNKNOWN";
99  return "OPEN";
101  return "CLOSE";
103  return "SOCKETERROR";
105  return "RESPONSETIMEOUT";
107  return "HEARTBEAT";
109  return "RX_PACKET";
111  return "TX_PACKET";
112  default:
113  return "Unhandled IOEvent type";
114  }
115 }
116 
117 using BaosMsgQueue = wzcpp::ConcurrentDeque<IOEvent>;
118 
119 } // namespace wz::baos
120 
121 #endif //__BAOSLIB_TCP_IO_H__
Provides the structures for devices with IP capabilities.
Classes regarding response telegrams.
Represents an IOEvent.
Definition: IOEvent.h:42
std::unique_ptr< IOEvent > UPtr
An alias for an unique pointer to an IOEvent.
Definition: IOEvent.h:44
Type
The different possible event types.
Definition: IOEvent.h:50
@ HEARTBEAT
A Heartbeat is requested.
@ RESPONSETIMEOUT
The response was not received in the through timeout given period.
@ TX_PACKET
An outgoing (TX) packet should be handled.
@ UNKNOWN
If event is not specified.
@ CLOSE
An close event is requested.
@ SOCKETERROR
Errors regarding the underlying socket.
@ OPEN
An open event is requested.
@ RX_PACKET
An incoming (RX) packet was received and should be handled.
EventDataType data_
The payload/data for this event.
Definition: IOEvent.h:83
IOEvent(Type eventT, EventDataType &&pkt, protocol::ResponsePromise &&prom, std::uint64_t mID=0)
Construct a new IOEvent object.
Definition: IOEvent.h:70
IOEvent()=default
Default constructor, generating an empty IOEvent.
std::variant< ip::Ip4SocketAddress, std::unique_ptr< Buffer > > EventDataType
Definition: IOEvent.h:46
protocol::ResponsePromise responsePromise
Stores the response promise to resolve later.
Definition: IOEvent.h:81
Global types and configuration for the whole SDK.
std::promise< BaseResponse::SPtr > ResponsePromise
Definition: Response.h:95
Global BAOS sdk namespace.
Definition: config.h:62
std::string toString(IOEvent::Type typ)
Provides a human readable string for an IOEvent type.
Definition: IOEvent.h:92
wzcpp::ConcurrentDeque< IOEvent > BaosMsgQueue
Alias for a concurrent queue with IOEvents.
Definition: IOEvent.h:117