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, k32u 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 SetErrorHandler(GoCtrlChannelErrorHandler callback) override;
96 
103  const kIpAddress& Address() const;
104 
111  const k32u& Port() const;
112 
113 // Private constants
114 private:
115  const k64u WRITE_THREAD_LOOP_SLEEP_USEC = 100000; // 100 msec.
116  const k64u READ_THREAD_LOOP_SLEEP_USEC = 100000; // 100 msec.
117 
118 private:
119  void ProcessRequest(const ByteArray& msgpack) const;
120  void ProcessResponse() const;
121 
122  void OnResponse(const ByteArray& bytes) const;
123  void OnError(GoCtrlChannelErrorType type, k64u id, const std::exception& e);
124 
125  static kStatus kCall WriteThreadFunc(void* context);
126  static kStatus kCall ReadThreadFunc(void* context);
127 
128 private:
129  kIpAddress address = { };
131 
132  Go::Object<kTcpClient> client;
133  bool isConnected = false;
134 
135  Go::Object<kThread> readThread;
136  Go::Object<kThread> writeThread;
137 
138  std::mutex mutex;
139 
140  Go::Object<kSerializer> serializer;
141 
142  std::queue<GoRequest> sendQueue;
143  Go::Object<kSemaphore> sendQueueSema;
144 
145  GoCtrlChannelResponseHandler responseHandler;
146  GoCtrlChannelErrorHandler errorHandler;
147 
148  friend class ::GoCtrlChannelV1Tests;
149 };
150 
151 }
152 
153 #endif
std::function< void(const std::shared_ptr< GoResponse > response)> GoCtrlChannelResponseHandler
Function to receive responses from the server asynchronously.
Definition: GoCtrlChannel.h:32
#define GoPxLSdkClass
Definition: Def.h:35
GoCtrlChannelErrorType
Definition: GoCtrlChannel.h:23
Interface for all control channel implementations.
Definition: GoCtrlChannel.h:43
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.
constexpr k32u GO_PXL_SDK_DEFAULT_CONTROL_PORT
Definition: Def.h:39
std::vector< Byte > ByteArray
Definition: Def.h:49
constexpr k16u MSGPACK_MESSAGE_TYPE
Definition: GoCtrlChannelV1.h:23
Definition: Def.h:46
std::function< void(GoCtrlChannelErrorType type, k64u id, const std::exception &e)> GoCtrlChannelErrorHandler
Function to receive read / write thread errors.
Definition: GoCtrlChannel.h:37