WebCFace 2.9.0
Web-based Communication Framework & Dashboard-like UI
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1#pragma once
2#include <string>
3#include <memory>
4#include <vector>
5#include "member.h"
6#ifdef WEBCFACE_MESON
7#include "webcface-config.h"
8#else
9#include "webcface/common/webcface-config.h"
10#endif
11
13
18class WEBCFACE_DLL Client : public Member {
19 std::shared_ptr<internal::ClientData> data;
20
21 public:
22 Client(const Client &) = delete;
23 const Client &operator=(const Client &) = delete;
24
31 Client() : Client("") {}
42 explicit Client(const std::string &name,
43 const std::string &host = "127.0.0.1",
44 int port = WEBCFACE_DEFAULT_PORT)
45 : Client(SharedString::encode(name), SharedString::encode(host), port) {
46 }
58 explicit Client(const std::wstring &name,
59 const std::wstring &host = L"127.0.0.1",
60 int port = WEBCFACE_DEFAULT_PORT)
61 : Client(SharedString::encode(name), SharedString::encode(host), port) {
62 }
63
64 explicit Client(const SharedString &name, const SharedString &host,
65 int port);
66 explicit Client(const SharedString &name,
67 const std::shared_ptr<internal::ClientData> &data);
68
74 bool connected() const;
75
83 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
84 std::nullptr_t> = nullptr>
85 const Client &onDisconnect(F callback) const {
87 [callback = std::move(callback)](const auto &) { callback(); });
88 return *this;
89 }
99 template <typename F, typename std::enable_if_t<std::is_invocable_v<F>,
100 std::nullptr_t> = nullptr>
101 const Client &onConnect(F callback) const {
102 this->Member::onConnect(
103 [callback = std::move(callback)](const auto &) { callback(); });
104 return *this;
105 }
106
111 ~Client();
116 const Client &close() const;
117
126 const Client &autoReconnect(bool enabled) const;
131 bool autoReconnect() const;
132
138 const Client &start() const;
151 const Client &waitConnection() const;
152
153 private:
154 const Client &
155 syncImpl(std::optional<std::chrono::microseconds> timeout) const;
156
157 public:
172 const Client &sync() const {
173 return syncImpl(std::chrono::microseconds(0));
174 }
188 const Client &loopSyncFor(std::chrono::microseconds timeout) const {
189 return syncImpl(timeout);
190 }
200 template <typename Clock, typename Duration>
201 const Client &
202 loopSyncUntil(std::chrono::time_point<Clock, Duration> timeout) const {
203 return syncImpl(std::chrono::duration_cast<std::chrono::microseconds>(
204 timeout - Clock::now()));
205 }
217 const Client &loopSync() const { return syncImpl(std::nullopt); }
218
219 // /*!
220 // * \brief 別スレッドでsync()を自動的に呼び出す間隔を設定する。
221 // * \since ver2.0
222 // *
223 // * * start() や waitConnection() より前に設定する必要がある。
224 // * *
225 // autoSyncが有効の場合、別スレッドで一定間隔(100μs)ごとにsync()が呼び出され、
226 // * 各種コールバック (onEntry, onChange, Func::run()など)
227 // * も別のスレッドで呼ばれることになる
228 // * (そのためmutexなどを適切に設定すること)
229 // * * デフォルトでは無効なので、手動でsync()などを呼び出す必要がある
230 // *
231 // * \param enabled trueにすると自動でsync()が呼び出されるようになる
232 // * \sa sync(), loopSyncFor(), loopSyncUntil(), loopSync()
233 // */
234 // void autoSync(bool enabled);
235
236 private:
237 Member member(const SharedString &name) const {
238 if (name.empty()) {
239 return *this;
240 } else {
241 return Member{data, name};
242 }
243 }
244
245 public:
253 Member member(std::string_view name) const {
254 return member(SharedString::encode(name));
255 }
263 Member member(std::wstring_view name) const {
264 return member(SharedString::encode(name));
265 }
272 std::vector<Member> members();
280 std::vector<Member> members() const;
288 const Client &
289 onMemberEntry(std::function<void WEBCFACE_CALL_FP(Member)> callback) const;
290
306 std::streambuf *loggerStreamBuf() const;
314 std::streambuf *loggerStreamBuf(std::string_view name) const;
327 std::ostream &loggerOStream() const;
335 std::ostream &loggerOStream(std::string_view name) const;
341 std::wstreambuf *loggerWStreamBuf() const;
349 std::wstreambuf *loggerWStreamBuf(std::wstring_view name) const;
355 std::wostream &loggerWOStream() const;
363 std::wostream &loggerWOStream(std::wstring_view name) const;
364
369 const std::string &serverVersion() const;
377 const std::string &serverName() const;
382 const std::string &serverHostName() const;
383};
384
サーバーに接続するクライアント。
Definition client.h:18
const Client & loopSyncFor(std::chrono::microseconds timeout) const
送信用にセットしたデータをすべて送信キューに入れ、受信したデータを処理する。
Definition client.h:188
const Client & onConnect(F callback) const
サーバーに接続したときに呼び出されるコールバックを設定
Definition client.h:101
const Client & loopSync() const
送信用にセットしたデータをすべて送信キューに入れ、受信したデータを処理する。
Definition client.h:217
const Client & loopSyncUntil(std::chrono::time_point< Clock, Duration > timeout) const
送信用にセットしたデータをすべて送信キューに入れ、受信したデータを処理する。
Definition client.h:202
const Client & onDisconnect(F callback) const
切断したときに呼び出されるコールバックを設定
Definition client.h:85
Client()
名前を指定せずサーバーに接続する
Definition client.h:31
Client(const Client &)=delete
Client(const std::wstring &name, const std::wstring &host=L"127.0.0.1", int port=7530)
名前を指定しサーバーに接続する (wstring)
Definition client.h:58
const Client & sync() const
送信用にセットしたデータをすべて送信キューに入れ、受信したデータを処理する。
Definition client.h:172
Member member(std::string_view name) const
他のmemberにアクセスする
Definition client.h:253
Client(const std::string &name, const std::string &host="127.0.0.1", int port=7530)
名前を指定しサーバーに接続する
Definition client.h:42
Member member(std::wstring_view name) const
他のmemberにアクセスする (wstring)
Definition client.h:263
const Client & operator=(const Client &)=delete
Memberを指すクラス
Definition member.h:23
const Member & onConnect(std::function< void(Member)> callback) const
Memberがサーバーに接続したときに呼び出されるコールバックを設定
Definition member.cc:47
const Member & onDisconnect(std::function< void(Member)> callback) const
Memberが切断したときに呼び出されるコールバックを設定
Definition member.cc:40
bool connected() const
Memberがサーバーに接続できているときtrueを返す
Definition member.cc:30
u8stringとstringとwstringをshared_ptrで持ち共有する
Definition encoding.h:67
bool empty() const
Definition encoding.cc:118
Member_ member() const
Memberを返す
Definition field.h:107
#define WEBCFACE_DLL
Definition webcface-config.h:69
#define WEBCFACE_NS_END
Definition webcface-config.h:118
#define WEBCFACE_NS_BEGIN
Definition webcface-config.h:117
#define WEBCFACE_DEFAULT_PORT
Definition webcface-config.h:4
#define WEBCFACE_CALL_FP
Definition webcface-config.h:107