libvisiontransfer  10.6.0
datachannelservicebase.h
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include <memory>
16 #include <map>
17 #include <set>
18 #include <string>
19 #include <vector>
20 #include <algorithm>
21 
22 #ifdef _WIN32
23 #include <winsock2.h>
24 #else
25 #include <arpa/inet.h>
26 #endif
27 
28 #ifndef VISIONTRANSFER_DATACHANNELSERVICEBASE_H
29 #define VISIONTRANSFER_DATACHANNELSERVICEBASE_H
30 
31 namespace visiontransfer {
32 namespace internal {
33 
34 /*
35  * This is a header file for internal use, please use the wrappers from datachannelservice.h
36  */
37 
38 #pragma pack(push,1)
39 
42 struct DataChannelMessageHeader {
43  uint8_t channelID;
44  uint8_t channelType;
45  uint32_t payloadSize;
46 };
47 struct DataChannelMessage {
48  DataChannelMessageHeader header;
49  unsigned char* payload;
50 };
51 #pragma pack(pop)
52 
53 
54 
55 class DataChannelServiceBase;
59 class DataChannel {
60 public:
64  struct Types {
65  enum DataChannelTypesEnum {
66  CONTROL = 0x00,
67  BNO080 = 0x01,
68  UNDEFINED = 0xff
69  };
70  };
71 
72  typedef unsigned char Type;
73  typedef unsigned char ID;
74 
75  inline DataChannel(): infoString("RESERVED") { }
76  inline virtual ~DataChannel() {}
77  inline ID getChannelID() const { return channelID; }
78  inline std::string getInfoString() const { return infoString; }
79  inline void setService(std::weak_ptr<DataChannelServiceBase> serv) { service = serv; }
80  inline void setChannelID(ID id) { channelID = id; }
81 
82  virtual Type getChannelType() const = 0;
84  virtual int handleMessage(DataChannelMessage& message, sockaddr_in* sender) = 0;
86  virtual bool initialize() = 0;
88  virtual int startService() = 0;
92  virtual bool process() { return true; }
93  virtual int stopService() = 0;
94 protected:
95  std::string infoString;
96  int sendData(unsigned char* data, unsigned int dataSize, sockaddr_in* recipient=nullptr);
97 private:
98  ID channelID;
99  std::weak_ptr<DataChannelServiceBase> service;
100 };
101 
105 class DataChannelInfo {
106 public:
107  inline DataChannelInfo(DataChannel::ID id, DataChannel::Type type, const std::string& info): channelID(id), channelType(type), infoString(info) { }
108  inline DataChannel::ID getChannelID() const { return channelID; }
109  inline DataChannel::Type getChannelType() const { return channelType; }
110  inline std::string getInfoString() const { return infoString; }
111 private:
112  DataChannel::ID channelID;
113  DataChannel::Type channelType;
114  std::string infoString;
115 };
116 
117 
118 
122 class DataChannelServiceBase: public std::enable_shared_from_this<DataChannelServiceBase> {
123 public:
126  void process();
127  DataChannel::ID registerChannel(std::shared_ptr<DataChannel> channel);
128  virtual int sendDataInternal(unsigned char* compiledMessage, unsigned int messageSize, sockaddr_in* recipient);
129  int sendDataIsolatedPacket(DataChannel::ID id, DataChannel::Type type, unsigned char* data, unsigned int dataSize, sockaddr_in* recipient);
130  virtual int handleChannel0Message(DataChannelMessage& message, sockaddr_in* sender) = 0;
131 protected:
132  std::map<DataChannel::ID, std::shared_ptr<DataChannel> > channels;
133 #ifdef _WIN32
134  SOCKET dataChannelSocket;
135 #else
136  int dataChannelSocket;
137 #endif
138  DataChannelMessage message;
139 };
140 
141 
142 }} // namespaces
143 
144 #endif
145 
visiontransfer::internal::DataChannelMessage
Definition: datachannelservicebase.h:71
visiontransfer::internal::DataChannel::handleMessage
virtual int handleMessage(DataChannelMessage &message, sockaddr_in *sender)=0
Channel-dependent message handlers in respective channel implementations.
visiontransfer::internal::DataChannel::startService
virtual int startService()=0
startService() implementations can start devices, launch an IO-blocked worker thread etc.
visiontransfer::internal::DataChannel::process
virtual bool process()
A single processing iteration; should be short and must not block. Actual frequency determined by the...
Definition: datachannelservicebase.h:116
visiontransfer::internal::DataChannel
Base class all data channel services derive from (once on the server side, once on the API side)
Definition: datachannelservicebase.h:83
visiontransfer::internal::DataChannelServiceBase
Base class for the data service (background sending and receiving, dispatching to channels)
Definition: datachannelservicebase.h:146
visiontransfer::internal::DataChannel::initialize
virtual bool initialize()=0
When initialize() implementations return false, the service will be deactivated.
visiontransfer::internal::DataChannelInfo
API-level data channel info for advertisements and subscription accounting.
Definition: datachannelservicebase.h:129
Allied Vision