WebCFace 2.5.2
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
client_internal.h
Go to the documentation of this file.
1#pragma once
2#include <mutex>
3#include <condition_variable>
4#include <vector>
5#include <string>
6#include <memory>
7#include <chrono>
8#include <atomic>
9#include <unordered_map>
10#include <cstdlib>
11#include <spdlog/logger.h>
14#include "webcface/field.h"
15#include "webcface/log.h"
16#include "queue.h"
18#include "data_store1.h"
19#include "data_store2.h"
20#include "func_internal.h"
22
24
25class Log;
26class Variant;
27
28namespace internal {
29
30void wsThreadMain(const std::shared_ptr<ClientData> &data);
31// void syncThreadMain(const std::shared_ptr<ClientData> &data);
32
33extern std::atomic<int> log_keep_lines;
34
35struct ClientData : std::enable_shared_from_this<ClientData> {
36 explicit ClientData(const SharedString &name,
37 const SharedString &host = nullptr, int port = -1);
38
39 void close();
40
46 std::optional<unsigned int> self_member_id;
47 bool isSelf(const FieldBase &base) const {
48 return base.member_ == self_member_name;
49 }
50
51 // std::mutex curl_m;
53 int port;
54 bool current_curl_connected = false;
55 void *current_curl_handle = nullptr;
56 std::string current_curl_path;
57 std::string current_ws_buf = "";
58 std::shared_ptr<void> curl_initializer;
59
69 std::thread ws_thread;
70 // /*!
71 // * \brief recv_queueのメッセージを処理するスレッド
72 // *
73 // * auto_recv がtrueの場合のみ。
74 // *
75 // */
76 // std::thread sync_thread;
77
78 struct SyncDataSnapshot {
79 std::chrono::system_clock::time_point time;
84 StrMap1<std::shared_ptr<message::Canvas3DData>> canvas3d_prev, canvas3d_data;
85 StrMap1<std::shared_ptr<message::Canvas2DData>> canvas2d_prev, canvas2d_data;
86 StrMap1<ImageData> image_data;
89 };
90
91 struct SyncDataFirst {
92 StrMap2<unsigned int> value_req, text_req, robot_model_req, view_req,
93 canvas3d_req, canvas2d_req, image_req, log_req;
94 StrMap2<message::ImageReq> image_req_info;
95 bool ping_status_req;
96 SyncDataSnapshot sync_data;
97 };
98
102 std::atomic<bool> closing = false;
103
109 bool connected = false;
114 bool sync_init_end = false;
121 bool do_ws_init = false;
131 bool do_ws_recv = false;
136 bool recv_ready = false;
137
138 std::queue<std::vector<std::pair<int, std::shared_ptr<void>>>>
153 std::queue<std::variant<std::string, SyncDataSnapshot>> sync_queue;
154 };
155
156 private:
157 WsMutexedData ws_data;
158 std::mutex ws_m;
159
160 public:
170 std::condition_variable ws_cond;
171
175 class ScopedWsLock : public std::unique_lock<std::mutex> {
176 ClientData *data;
177
178 public:
179 explicit ScopedWsLock(ClientData *data)
180 : std::unique_lock<std::mutex>(data->ws_m), data(data) {}
181 explicit ScopedWsLock(const std::shared_ptr<ClientData> &data)
182 : ScopedWsLock(data.get()) {}
183 auto &getData() {
184 assert(this->owns_lock());
185 return data->ws_data;
186 }
187 };
188
192 std::atomic<bool> auto_reconnect = true;
193
208 std::optional<SyncDataFirst> sync_first;
217 SyncDataFirst syncDataFirst(internal::ClientData *this_);
228 SyncDataSnapshot syncData(internal::ClientData *this_, bool is_first);
229 };
230
231 private:
232 SyncMutexedData sync_data;
240 std::mutex sync_m;
241
242 public:
246 class ScopedSyncLock : public std::unique_lock<std::mutex> {
247 ClientData *data;
248
249 public:
251 : std::unique_lock<std::mutex>(data->sync_m), data(data) {}
252 explicit ScopedSyncLock(const std::shared_ptr<ClientData> &data)
253 : ScopedSyncLock(data.get()) {}
254 auto &getData() {
255 assert(this->owns_lock());
256 return data->sync_data;
257 }
258 };
259
266 bool messagePushOnline(std::string &&msg) {
267 ScopedWsLock lock_ws(this);
268 if (lock_ws.getData().connected) {
269 lock_ws.getData().sync_queue.push(std::move(msg));
270 this->ws_cond.notify_all();
271 return true;
272 } else {
273 return false;
274 }
275 }
282 bool messagePushReq(std::string &&msg) {
283 bool has_sync_first;
284 {
285 ScopedSyncLock lock_s(this);
286 has_sync_first = (lock_s.getData().sync_first != std::nullopt);
287 }
288 if (has_sync_first) {
289 ScopedWsLock lock_ws(this);
290 lock_ws.getData().sync_queue.push(std::move(msg));
291 this->ws_cond.notify_all();
292 return true;
293 } else {
294 return false;
295 }
296 }
302 void messagePushAlways(std::string &&msg) {
303 ScopedWsLock lock_ws(this);
304 lock_ws.getData().sync_queue.push(std::move(msg));
305 this->ws_cond.notify_all();
306 }
307 void messagePushAlways(SyncDataSnapshot &&msg) {
308 ScopedWsLock lock_ws(this);
309 lock_ws.getData().sync_queue.push(std::move(msg));
310 this->ws_cond.notify_all();
311 }
312
313 std::string packSyncDataFirst(const SyncDataFirst &data);
314 std::string packSyncData(std::stringstream &buffer, int &len,
315 const SyncDataSnapshot &data);
316
321 void start();
326 void join();
327
336 void syncImpl(bool sync, bool forever,
337 std::optional<std::chrono::microseconds> timeout);
338
343 void
344 onRecv(const std::vector<std::pair<int, std::shared_ptr<void>>> &messages);
345
346 std::mutex entry_m;
359
365
367 std::unordered_map<unsigned int, std::string> member_lib_name,
368 member_lib_ver, member_addr;
369 const SharedString &getMemberNameFromId(unsigned int id) const {
370 if (self_member_id && *self_member_id == id) {
371 return self_member_name;
372 }
373 for (const auto &it : member_ids) {
374 if (it.second == id) {
375 return it.first;
376 }
377 }
378 static SharedString empty;
379 return empty;
380 }
381 unsigned int getMemberIdFromName(const SharedString &name) const {
382 if (name == self_member_name && self_member_id) {
383 return *self_member_id;
384 }
385 auto it = member_ids.find(name);
386 if (it != member_ids.end()) {
387 return it->second;
388 }
389 return 0;
390 }
391
396 std::mutex event_m;
403 std::shared_ptr<std::function<void(Member)>> member_entry_event;
404
405 StrMap2<std::shared_ptr<std::function<void(Value)>>> value_change_event;
406 StrMap2<std::shared_ptr<std::function<void(Variant)>>> text_change_event;
407 StrMap2<std::shared_ptr<std::function<void(Image)>>> image_change_event;
408 StrMap2<std::shared_ptr<std::function<void(RobotModel)>>>
410 StrMap2<std::shared_ptr<std::function<void(View)>>> view_change_event;
411 StrMap2<std::shared_ptr<std::function<void(Canvas3D)>>>
413 StrMap2<std::shared_ptr<std::function<void(Canvas2D)>>>
415 StrMap2<std::shared_ptr<std::function<void(Log)>>> log_append_event;
416 StrMap1<std::shared_ptr<std::function<void(Member)>>> sync_event,
418 StrMap1<std::shared_ptr<std::function<void(Value)>>> value_entry_event;
419 StrMap1<std::shared_ptr<std::function<void(Text)>>> text_entry_event;
420 StrMap1<std::shared_ptr<std::function<void(Func)>>> func_entry_event;
421 StrMap1<std::shared_ptr<std::function<void(View)>>> view_entry_event;
422 StrMap1<std::shared_ptr<std::function<void(Image)>>> image_entry_event;
423 StrMap1<std::shared_ptr<std::function<void(RobotModel)>>>
425 StrMap1<std::shared_ptr<std::function<void(Canvas3D)>>>
427 StrMap1<std::shared_ptr<std::function<void(Canvas2D)>>>
429 StrMap1<std::shared_ptr<std::function<void(Log)>>>
431
432 std::shared_ptr<spdlog::logger> logger_internal;
433 std::mutex logger_m;
438
439 std::string svr_name, svr_version, svr_hostname;
440
441 std::shared_ptr<std::unordered_map<unsigned int, int>> ping_status =
442 nullptr;
443 bool ping_status_req = false;
448 void pingStatusReq();
449};
450} // namespace internal
Canvas2Dの送受信データを表すクラス
Definition canvas2d.h:24
Canvas3Dの送受信データを表すクラス
Definition canvas3d.h:28
関数1つを表すクラス
Definition func.h:95
(ver1.3から追加) 画像の送受信データを表すクラス
Definition image.h:20
ログの送受信データを表すクラス
Definition log.h:65
Memberを指すクラス
Definition member.h:22
RobotModelの送受信データを表すクラス
Definition robot_model.h:26
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:69
文字列の送受信データを表すクラス
Definition text.h:183
実数値またはその配列の送受信データを表すクラス
Definition value.h:23
文字列、数値などの型を送受信するクラス
Definition text.h:25
Viewの送受信データを表すクラス
Definition view.h:26
Definition client_internal.h:246
ScopedSyncLock(const std::shared_ptr< ClientData > &data)
Definition client_internal.h:252
auto & getData()
Definition client_internal.h:254
ScopedSyncLock(ClientData *data)
Definition client_internal.h:250
Definition client_internal.h:175
auto & getData()
Definition client_internal.h:183
ScopedWsLock(ClientData *data)
Definition client_internal.h:179
ScopedWsLock(const std::shared_ptr< ClientData > &data)
Definition client_internal.h:181
PromiseDataのリストを保持する。
Definition func_internal.h:88
Definition data_store1.h:10
送受信するデータを保持するクラス
Definition data_store2.h:21
std::atomic< int > log_keep_lines
Definition log.cc:9
void wsThreadMain(const std::shared_ptr< ClientData > &data)
Definition client_threading.cc:78
std::map< SharedString, T > StrMap1
Definition encoding.h:110
StrMap1< StrMap1< T > > StrMap2
Definition encoding.h:112
std::set< SharedString > StrSet1
Definition encoding.h:113
メンバ名とデータ名を持つクラス
Definition field.h:37
SharedString member_
メンバー名
Definition field.h:42
Definition client_internal.h:194
std::optional< SyncDataFirst > sync_first
Definition client_internal.h:208
Definition client_internal.h:104
std::queue< std::vector< std::pair< int, std::shared_ptr< void > > > > recv_queue
Definition client_internal.h:139
std::queue< std::variant< std::string, SyncDataSnapshot > > sync_queue
送信したいメッセージを入れるキュー
Definition client_internal.h:153
Definition client_internal.h:35
FuncResultStore func_result_store
Definition client_internal.h:358
std::unordered_map< unsigned int, std::string > member_lib_name
Definition client_internal.h:367
unsigned int getMemberIdFromName(const SharedString &name) const
Definition client_internal.h:381
bool messagePushOnline(std::string &&msg)
Definition client_internal.h:266
SyncDataStore2< std::shared_ptr< message::Canvas3DData > > canvas3d_store
Definition client_internal.h:354
std::thread ws_thread
websocket接続、通信するスレッド
Definition client_internal.h:69
StrMap1< std::shared_ptr< std::function< void(Member)> > > sync_event
Definition client_internal.h:416
StrMap2< std::shared_ptr< std::function< void(Variant)> > > text_change_event
Definition client_internal.h:406
std::optional< unsigned int > self_member_id
Definition client_internal.h:46
SyncDataStore2< std::shared_ptr< TextData > > text_store
Definition client_internal.h:349
StrMap1< std::shared_ptr< std::function< void(Value)> > > value_entry_event
Definition client_internal.h:418
StrMap1< std::unique_ptr< std::streambuf > > logger_buf
Definition client_internal.h:434
bool messagePushReq(std::string &&msg)
Definition client_internal.h:282
StrMap2< std::shared_ptr< std::function< void(RobotModel)> > > robot_model_change_event
Definition client_internal.h:409
std::shared_ptr< std::function< void(Member)> > member_entry_event
Definition client_internal.h:403
bool isSelf(const FieldBase &base) const
Definition client_internal.h:47
StrMap2< std::shared_ptr< std::function< void(View)> > > view_change_event
Definition client_internal.h:410
std::string current_curl_path
Definition client_internal.h:56
StrSet1 member_entry
Definition client_internal.h:347
int port
Definition client_internal.h:53
std::string svr_hostname
Definition client_internal.h:439
SyncDataStore2< std::shared_ptr< LogData > > log_store
Definition client_internal.h:356
StrMap1< unsigned int > member_ids
Definition client_internal.h:366
SyncDataStore2< std::shared_ptr< ValueData > > value_store
Definition client_internal.h:348
StrMap1< std::shared_ptr< std::function< void(Log)> > > log_entry_event
Definition client_internal.h:430
StrMap2< std::shared_ptr< std::function< void(Value)> > > value_change_event
Definition client_internal.h:405
StrMap1< std::shared_ptr< std::function< void(Text)> > > text_entry_event
Definition client_internal.h:419
SyncDataStore2< ImageData, message::ImageReq > image_store
Definition client_internal.h:352
StrMap1< std::unique_ptr< std::wostream > > logger_os_w
Definition client_internal.h:437
SharedString host
Definition client_internal.h:52
StrMap1< std::shared_ptr< std::function< void(Member)> > > ping_event
Definition client_internal.h:417
std::unordered_map< unsigned int, std::string > member_addr
Definition client_internal.h:368
void messagePushAlways(std::string &&msg)
Definition client_internal.h:302
StrMap1< std::unique_ptr< std::ostream > > logger_os
Definition client_internal.h:435
StrMap1< std::shared_ptr< std::function< void(Canvas2D)> > > canvas2d_entry_event
Definition client_internal.h:428
SharedString self_member_name
Client自身の名前
Definition client_internal.h:45
StrMap1< std::shared_ptr< std::function< void(Func)> > > func_entry_event
Definition client_internal.h:420
std::mutex entry_m
Definition client_internal.h:346
StrMap2< std::shared_ptr< std::function< void(Canvas3D)> > > canvas3d_change_event
Definition client_internal.h:412
SyncDataStore1< std::chrono::system_clock::time_point > sync_time_store
Definition client_internal.h:357
StrMap2< std::shared_ptr< std::function< void(Image)> > > image_change_event
Definition client_internal.h:407
SyncDataStore2< std::shared_ptr< FuncData > > func_store
Definition client_internal.h:350
const SharedString & getMemberNameFromId(unsigned int id) const
Definition client_internal.h:369
std::mutex logger_m
Definition client_internal.h:433
std::mutex event_m
コールバックリスト(のmapなど)にアクセスするときにロック
Definition client_internal.h:396
SyncDataStore2< std::shared_ptr< message::Canvas2DData > > canvas2d_store
Definition client_internal.h:355
StrMap1< std::shared_ptr< std::function< void(RobotModel)> > > robot_model_entry_event
Definition client_internal.h:424
StrMap1< std::shared_ptr< std::function< void(Canvas3D)> > > canvas3d_entry_event
Definition client_internal.h:426
std::shared_ptr< void > curl_initializer
Definition client_internal.h:58
SyncDataStore2< std::shared_ptr< RobotModelData > > robot_model_store
Definition client_internal.h:353
SyncDataStore2< std::shared_ptr< message::ViewData > > view_store
Definition client_internal.h:351
StrMap2< std::shared_ptr< std::function< void(Canvas2D)> > > canvas2d_change_event
Definition client_internal.h:414
StrMap1< std::shared_ptr< std::function< void(View)> > > view_entry_event
Definition client_internal.h:421
std::condition_variable ws_cond
ws_thread, recv_thread, queue 間の同期
Definition client_internal.h:170
std::shared_ptr< spdlog::logger > logger_internal
Definition client_internal.h:432
StrMap2< std::shared_ptr< std::function< void(Log)> > > log_append_event
Definition client_internal.h:415
StrMap1< std::unique_ptr< std::wstreambuf > > logger_buf_w
Definition client_internal.h:436
StrMap1< std::shared_ptr< std::function< void(Image)> > > image_entry_event
Definition client_internal.h:422
StrMap1< Queue< FuncCallHandle > > func_listener_handlers
listenerがfetchするの待ちの関数呼び出しをためておく
Definition client_internal.h:364
void messagePushAlways(SyncDataSnapshot &&msg)
Definition client_internal.h:307
#define WEBCFACE_NS_END
Definition webcface-config.h:104
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:103