GoPxL SDK

- Home
- Files
- Namespace

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"
13 #include "GoPxLSdk/Internal/GoCtrlChannel.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
26 class GoPxLSdkClass GoCtrlChannelV1 : public GoCtrlChannel
27 {
28 public:
35 GoCtrlChannelV1();
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 = { };
130 k32u port = GO_PXL_SDK_DEFAULT_CONTROL_PORT;
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
GoPxLSdk::GoCtrlChannelResponseHandler
std::function< void(const std::shared_ptr< GoResponse > response)> GoCtrlChannelResponseHandler
Function to receive responses from the server asynchronously.
Definition: GoCtrlChannel.h:32
GoPxLSdkClass
#define GoPxLSdkClass
Definition: Def.h:35
GoPxLSdk::GoCtrlChannelErrorType
GoCtrlChannelErrorType
Definition: GoCtrlChannel.h:23
GoCtrlChannel.h
GoPxLSdk::GoCtrlChannel
Interface for all control channel implementations.
Definition: GoCtrlChannel.h:43
GoPxLSdk::JSON_MESSAGE_TYPE
constexpr k16u JSON_MESSAGE_TYPE
Definition: GoCtrlChannelV1.h:24
GoPxLSdk::GoCtrlChannelV1
Definition: GoCtrlChannelV1.h:26
GO_PXL_SDK_DEFAULT_TCP_TIMEOUT_MILLISECONDS
constexpr k32u GO_PXL_SDK_DEFAULT_TCP_TIMEOUT_MILLISECONDS
Definition: Def.h:38
GoPxLSdk::GoRequest
Definition: GoRequest.h:21
Def.h
Declares the general SDK definitions.
GO_PXL_SDK_DEFAULT_CONTROL_PORT
constexpr k32u GO_PXL_SDK_DEFAULT_CONTROL_PORT
Definition: Def.h:39
GoPxLSdk::ByteArray
std::vector< Byte > ByteArray
Definition: Def.h:49
GoPxLSdk::MSGPACK_MESSAGE_TYPE
constexpr k16u MSGPACK_MESSAGE_TYPE
Definition: GoCtrlChannelV1.h:23
GoPxLSdk
Definition: Def.h:46
GoPxLSdk::GoCtrlChannelErrorHandler
std::function< void(GoCtrlChannelErrorType type, k64u id, const std::exception &e)> GoCtrlChannelErrorHandler
Function to receive read / write thread errors.
Definition: GoCtrlChannel.h:37