TTK
Loading...
Searching...
No Matches
WebSocketIO.h
Go to the documentation of this file.
1
22
23#pragma once
24
25#include <Debug.h>
26#include <list>
27#include <set>
28
29#include <iostream>
30
31#include <functional>
32
33#ifdef TTK_ENABLE_WEBSOCKETPP
34
35// fix undefined reference to boost::system link error
36#define BOOST_ERROR_CODE_HEADER_ONLY
37
38#include <websocketpp/config/asio_no_tls.hpp>
39#include <websocketpp/server.hpp>
40
41using websocketpp::connection_hdl;
42using websocketpp::lib::thread;
43
44using con_list = std::set<connection_hdl, std::owner_less<connection_hdl>>;
45using WSServer = websocketpp::server<websocketpp::config::asio>;
46
47#endif
48
49namespace ttk {
50
51 class WebSocketIO : virtual public Debug {
52 public:
53 struct Message {
54 size_t binaryPayloadSize{0}; // Used for Binary Data
55 const void *binaryPayload{nullptr}; // Used for Binary Data
56 std::string stringPayload; // Used for String Data
57 Message(const std::string &msg) : stringPayload(msg) {
58 }
59
60 Message(const size_t &sizeInBytes, const void *data)
61 : binaryPayloadSize(sizeInBytes), binaryPayload(data) {
62 }
68 };
69
71 ~WebSocketIO() override;
72
73 int isListening();
74
75 int virtual processEvent(const std::string &eventName,
76 const std::string &eventData = "");
77
78 int startServer(int PortNumber);
79 int stopServer();
80
81 int getPortNumber();
82
83 int sendString(const std::string &msg) const;
84 int sendBinary(const size_t &sizeInBytes, const void *data) const;
85 int sendMessage(const Message &msg) const;
86
87 int queueMessage(const std::string &msg);
88 int queueMessage(const size_t &sizeInBytes, const void *data);
89 int queueMessage(const Message &msg);
93
94 private:
95#ifdef TTK_ENABLE_WEBSOCKETPP
96 mutable WSServer server;
97
98 ttk::Timer msgTimer;
99 size_t nMessages;
100
101 std::thread serverThread{};
102 con_list connections;
103 std::mutex mutex;
104 websocketpp::lib::error_code ec;
105
106 // keep the state of the object sending process
107 bool serverThreadRunning = false;
108 int portNumber = 0;
109
110 int on_open(const websocketpp::connection_hdl &hdl);
111 int on_close(const websocketpp::connection_hdl &hdl);
112 int on_message(const websocketpp::connection_hdl &hdl,
113 const WSServer::message_ptr &msg);
114
115 std::list<Message> messageQueue;
116#endif
117 };
118} // namespace ttk
Minimalist debugging class.
Definition Debug.h:88
~WebSocketIO() override
int sendString(const std::string &msg) const
int sendMessage(const Message &msg) const
int queueMessage(const std::string &msg)
int sendBinary(const size_t &sizeInBytes, const void *data) const
virtual int processEvent(const std::string &eventName, const std::string &eventData="")
int startServer(int PortNumber)
The Topology ToolKit.
Message(const Message &msg)
Definition WebSocketIO.h:63
Message(const size_t &sizeInBytes, const void *data)
Definition WebSocketIO.h:60
Message(const std::string &msg)
Definition WebSocketIO.h:57