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