GoPxL SDK
GoCtrlChannelV1.h
Go to the documentation of this file.
1 
9 #ifndef GO_PXL_SDK_GOCTRLCHANNELV1_H
10 #define GO_PXL_SDK_GOCTRLCHANNELV1_H
11 
12 #include "GoPxLSdk/Def.h"
14 
15 #include "GoApi/Object.h"
16 
17 #include "kApi/Threads/kSemaphore.h"
18 
19 class GoCtrlChannelV1Tests;
20 
21 namespace GoPxLSdk
22 {
23 constexpr k16u MSGPACK_MESSAGE_TYPE = 0xB000;
24 constexpr k16u JSON_MESSAGE_TYPE = 0xB001;
25 
27 {
28 public:
36 
37  ~GoCtrlChannelV1();
38 
49  void Connect(const kIpAddress& address, k16u port,
50  k64u connectionTimeout = GO_PXL_SDK_DEFAULT_TCP_TIMEOUT_MILLISECONDS) override;
51 
58  void Disconnect() override;
59 
67  bool IsConnected() const override;
68 
77  void SendRequest(const GoRequest& request) override;
78 
86  void SetResponseHandler(GoCtrlChannelResponseHandler callback) override;
87 
95  void SetWriteErrorHandler(GoCtrlChannelWriteErrorHandler callback) override;
96 
105  void SetReadErrorHandler(GoCtrlChannelReadErrorHandler callback) override;
106 
113  const kIpAddress& Address() const;
114 
121  const k16u Port() const;
122 
123 // Private constants
124 private:
125  const k64u WRITE_THREAD_LOOP_SLEEP_USEC = 100000; // 100 msec.
126  const k64u READ_THREAD_LOOP_SLEEP_USEC = 100000; // 100 msec.
127 
128 private:
129  void ProcessRequest(const ByteArray& msgpack) const;
130  void ProcessResponse() const;
131 
132  void OnResponse(const ByteArray& bytes) const;
133  void OnWriteError(k64u id, const std::exception& e) const;
134  void OnReadError(ReadErrorType readErrorType, const std::exception& e) const;
135 
136  static kStatus kCall WriteThreadFunc(void* context);
137  static kStatus kCall ReadThreadFunc(void* context);
138 
139 private:
140  kIpAddress address = { };
142 
143  Go::Object<kTcpClient> client;
144  bool isConnected = false;
145 
146  Go::Object<kThread> readThread;
147  Go::Object<kThread> writeThread;
148 
149  std::mutex mutex;
150 
151  Go::Object<kSerializer> serializer;
152 
153  std::queue<GoRequest> sendQueue;
154  Go::Object<kSemaphore> sendQueueSema;
155 
156  GoCtrlChannelResponseHandler responseHandler;
157 
158  GoCtrlChannelWriteErrorHandler writeErrorHandler;
159  GoCtrlChannelReadErrorHandler readErrorHandler;
160 
161  friend class ::GoCtrlChannelV1Tests;
162 };
163 
164 }
165 
166 #endif
std::function< void(const std::shared_ptr< GoResponse > response)> GoCtrlChannelResponseHandler
Function to receive responses from the server asynchronously.
Definition: GoCtrlChannel.h:34
constexpr k16u GO_PXL_SDK_DEFAULT_CONTROL_PORT
Definition: Def.h:39
#define GoPxLSdkClass
Definition: Def.h:35
Interface for all control channel implementations.
Definition: GoCtrlChannel.h:50
constexpr k16u JSON_MESSAGE_TYPE
Definition: GoCtrlChannelV1.h:24
Definition: GoCtrlChannelV1.h:26
constexpr k32u GO_PXL_SDK_DEFAULT_TCP_TIMEOUT_MILLISECONDS
Definition: Def.h:38
Definition: GoRequest.h:21
Declares the general SDK definitions.
std::vector< Byte > ByteArray
Definition: Def.h:49
ReadErrorType
Definition: GoCtrlChannel.h:23
std::function< void(k64u id, const std::exception &e)> GoCtrlChannelWriteErrorHandler
Function to receive write thread errors.
Definition: GoCtrlChannel.h:39
constexpr k16u MSGPACK_MESSAGE_TYPE
Definition: GoCtrlChannelV1.h:23
Definition: Def.h:46
std::function< void(ReadErrorType readErrorType, const std::exception &e)> GoCtrlChannelReadErrorHandler
Function to receive read thread errors.
Definition: GoCtrlChannel.h:44