Chatterino
PubSubClient.hpp
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include <QString>
8 #include <pajlada/signals/signal.hpp>
9 
10 #include <atomic>
11 #include <vector>
12 
13 namespace chatterino {
14 
15 struct TopicData {
16  QString topic;
17  bool authed{false};
18  bool persistent{false};
19 };
20 
21 struct Listener : TopicData {
22  bool confirmed{false};
23 };
24 
25 class PubSubClient : public std::enable_shared_from_this<PubSubClient>
26 {
27 public:
29  std::vector<QString> topics;
30  QString nonce;
31  };
32 
33  // The max amount of topics we may listen to with a single connection
34  static constexpr std::vector<QString>::size_type MAX_LISTENS = 50;
35 
36  PubSubClient(WebsocketClient &_websocketClient, WebsocketHandle _handle,
37  const PubSubClientOptions &clientOptions);
38 
39  void start();
40  void stop();
41 
42  void close(const std::string &reason,
43  websocketpp::close::status::value code =
44  websocketpp::close::status::normal);
45 
46  bool listen(PubSubListenMessage msg);
47  UnlistenPrefixResponse unlistenPrefix(const QString &prefix);
48 
49  void handleListenResponse(const PubSubMessage &message);
50  void handleUnlistenResponse(const PubSubMessage &message);
51 
52  void handlePong();
53 
54  bool isListeningToTopic(const QString &topic);
55 
56  std::vector<Listener> getListeners() const;
57 
58 private:
59  void ping();
60  bool send(const char *payload);
61 
62  WebsocketClient &websocketClient_;
63  WebsocketHandle handle_;
64  uint16_t numListens_ = 0;
65 
66  std::vector<Listener> listeners_;
67 
68  std::atomic<bool> awaitingPong_{false};
69  std::atomic<bool> started_{false};
70 
71  const PubSubClientOptions &clientOptions_;
72 };
73 
74 } // namespace chatterino
websocketpp::client< chatterinoconfig > WebsocketClient
Definition: PubSubWebsocket.hpp:28
websocketpp::connection_hdl WebsocketHandle
Definition: PubSubWebsocket.hpp:29
Definition: PubSubClient.hpp:25
Definition: Application.cpp:48
bool authed
Definition: PubSubClient.hpp:17
Definition: PubSubClient.hpp:15
QString topic
Definition: PubSubClient.hpp:16
Options to change the behaviour of the underlying websocket clients.
Definition: PubSubClientOptions.hpp:10
QString nonce
Definition: PubSubClient.hpp:30
Definition: Listen.hpp:10
std::vector< QString > topics
Definition: PubSubClient.hpp:29
bool persistent
Definition: PubSubClient.hpp:18
Definition: Base.hpp:13
Definition: PubSubClient.hpp:21