Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Toggle main menu visibility
Loading...
Searching...
No Matches
src
network
websocket
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
16
namespace
net {
17
namespace
websocket {
18
19
using
nlohmann::json
;
20
using
websocketpp::connection_hdl;
21
22
typedef
std::shared_ptr<websocketpp::config::core::message_type> message_t;
23
35
class
SingleClientWSServer
{
36
public
:
43
SingleClientWSServer
(
const
std::string
& serverName,
uint16_t
port);
44
49
~SingleClientWSServer
();
50
51
// delete the assignment operator
52
SingleClientWSServer
& operator=(
const
SingleClientWSServer
&) =
delete
;
53
60
bool
start
();
61
65
void
stop
();
66
76
bool
addProtocol
(
std::unique_ptr<WebSocketProtocol>
protocol);
77
85
void
sendRawString
(
const
std::string
& protocolPath,
const
std::string
& str);
86
94
void
sendJSON
(
const
std::string
& protocolPath,
const
json& obj);
95
96
private
:
97
class
ProtocolData {
98
public
:
99
ProtocolData(
std::unique_ptr<WebSocketProtocol>
protocol);
100
const
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
115
std::map<std::string, ProtocolData>
protocolMap;
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
functional
map
memory
thread
optional
std::string
_GLIBCXX_END_NAMESPACE_CXX11 typedef basic_string< char > string
std::thread
std::mutex
std::map
std::unique_ptr
net::mc::SingleClientWSServer::SingleClientWSServer
SingleClientWSServer(const std::string &serverName, uint16_t port)
Construct a new server object, without starting it.
Definition
WebSocketServer.cpp:16
net::websocket::SingleClientWSServer::sendJSON
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
net::websocket::SingleClientWSServer::sendRawString
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
net::websocket::SingleClientWSServer::addProtocol
bool addProtocol(std::unique_ptr< WebSocketProtocol > protocol)
Register a protocol with this server.
Definition
WebSocketServer.cpp:87
net::websocket::SingleClientWSServer::SingleClientWSServer
SingleClientWSServer(const std::string &serverName, uint16_t port)
Construct a new server object, without starting it.
Definition
WebSocketServer.cpp:16
net::websocket::SingleClientWSServer::stop
void stop()
Stop the server.
Definition
WebSocketServer.cpp:60
net::websocket::SingleClientWSServer::start
bool start()
Start the server.
Definition
WebSocketServer.cpp:38
net::websocket::SingleClientWSServer::~SingleClientWSServer
~SingleClientWSServer()
Destroy the server after calling stop().
Definition
WebSocketServer.cpp:34
util::PeriodicScheduler
Uses a single thread to periodically invoke callbacks at a given frequency.
Definition
scheduler.h:38
util::Watchdog
Implements a thread-safe watchdog.
Definition
scheduler.h:202
uint16_t
::uint16_t uint16_t
nlohmann::json
basic_json<> json
Generated by
1.17.0