Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
WebSocketServer.h
1#pragma once
2
3#include "../../utils/scheduler.h"
4#include "WebSocketProtocol.h"
5
6#include <functional>
7#include <map>
8#include <memory>
9#include <optional>
10#include <thread>
11
12#include <nlohmann/json.hpp>
13#include <websocketpp/config/asio_no_tls.hpp>
14#include <websocketpp/server.hpp>
15
16namespace net {
17namespace websocket {
18
19using nlohmann::json;
20using websocketpp::connection_hdl;
21
22typedef std::shared_ptr<websocketpp::config::core::message_type> message_t;
23
36public:
43 SingleClientWSServer(const std::string& serverName, uint16_t port);
44
50
51 // delete the assignment operator
52 SingleClientWSServer& operator=(const SingleClientWSServer&) = delete;
53
60 bool start();
61
65 void stop();
66
77
85 void sendRawString(const std::string& protocolPath, const std::string& str);
86
94 void sendJSON(const std::string& protocolPath, const json& obj);
95
96private:
97 class ProtocolData {
98 public:
99 ProtocolData(std::unique_ptr<WebSocketProtocol> protocol);
101 std::optional<connection_hdl> client;
102 // holds the periodically scheduled ping event and the watchdog
103 std::optional<std::pair<util::PeriodicScheduler<>::eventid_t, util::Watchdog<>>>
104 heartbeatInfo;
105 std::mutex mutex;
106 };
107
108 std::string serverName;
109 uint16_t port;
110 websocketpp::server<websocketpp::config::asio> server;
111 bool isRunning;
112 // protects against race conditions modifying protocolMap
113 std::mutex protocolMapMutex;
114 // maps path prefix to ProtocolData for each protocol
116 std::thread serverThread;
117 util::PeriodicScheduler<> pingScheduler;
118
119 // called when connection is received and being validated.
120 // return true to accept, false to reject
121 bool validate(connection_hdl hdl);
122 // called when connection is opened
123 void onOpen(connection_hdl hdl);
124 // called when connection is closed
125 void onClose(connection_hdl hdl);
126 void onMessage(connection_hdl hdl, message_t message);
127 // called when pong message received from WS client
128 void onPong(connection_hdl hdl, const std::string& payload);
129 void serverTask();
130 // Thread-safe access of the protocolMap
131 std::optional<std::reference_wrapper<SingleClientWSServer::ProtocolData>>
132 getProtocol(const std::string& protocolPath);
133};
134} // namespace websocket
135} // namespace net
_GLIBCXX_END_NAMESPACE_CXX11 typedef basic_string< char > string
SingleClientWSServer(const std::string &serverName, uint16_t port)
Construct a new server object, without starting it.
Definition WebSocketServer.cpp:16
void sendJSON(const std::string &protocolPath, const json &obj)
Serialize the JSON as a string and send it to the client connected to the given protocol path,...
Definition WebSocketServer.cpp:115
void sendRawString(const std::string &protocolPath, const std::string &str)
Send a string to the client connected to the given protocol path, if there is one.
Definition WebSocketServer.cpp:98
bool addProtocol(std::unique_ptr< WebSocketProtocol > protocol)
Register a protocol with this server.
Definition WebSocketServer.cpp:87
SingleClientWSServer(const std::string &serverName, uint16_t port)
Construct a new server object, without starting it.
Definition WebSocketServer.cpp:16
void stop()
Stop the server.
Definition WebSocketServer.cpp:60
bool start()
Start the server.
Definition WebSocketServer.cpp:38
~SingleClientWSServer()
Destroy the server after calling stop()
Definition WebSocketServer.cpp:34
Uses a single thread to periodically invoke callbacks at a given frequency.
Definition scheduler.h:38
Implements a thread-safe watchdog.
Definition scheduler.h:202
::uint16_t uint16_t
basic_json<> json