2021-04-27 16:18:06 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QObject>
|
2021-04-27 21:52:48 +00:00
|
|
|
#include <QThreadPool>
|
|
|
|
#include <QMutex>
|
|
|
|
|
2021-04-27 16:46:00 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2021-04-27 21:52:48 +00:00
|
|
|
#include <websocketpp/config/asio_no_tls.hpp>
|
|
|
|
#include <websocketpp/server.hpp>
|
2021-04-27 16:18:06 +00:00
|
|
|
|
2021-04-27 21:52:48 +00:00
|
|
|
#include "WebSocketSession.h"
|
2021-04-27 16:29:51 +00:00
|
|
|
#include "requesthandler/RequestHandler.h"
|
|
|
|
|
2021-04-27 21:52:48 +00:00
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2021-04-27 16:18:06 +00:00
|
|
|
class WebSocketServer : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2021-04-27 21:52:48 +00:00
|
|
|
WebSocketServer();
|
|
|
|
~WebSocketServer();
|
|
|
|
|
|
|
|
void Start();
|
|
|
|
void Stop();
|
|
|
|
void InvalidateSession(websocketpp::connection_hdl hdl);
|
|
|
|
|
|
|
|
struct WebSocketState {
|
|
|
|
websocketpp::connection_hdl hdl;
|
|
|
|
std::string remoteAddress;
|
|
|
|
uint64_t durationSeconds;
|
|
|
|
uint64_t incomingMessages;
|
|
|
|
uint64_t outgoingMessages;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<WebSocketState> GetWebSocketSessions();
|
|
|
|
|
|
|
|
QThreadPool *GetThreadPool() {
|
|
|
|
return &_threadPool;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Q_SLOTS:
|
|
|
|
void BroadcastEvent(uint64_t requiredIntent, std::string eventType, json eventData = nullptr);
|
2021-04-27 16:46:00 +00:00
|
|
|
|
|
|
|
private:
|
2021-04-27 21:52:48 +00:00
|
|
|
void onOpen(websocketpp::connection_hdl hdl);
|
|
|
|
void onClose(websocketpp::connection_hdl hdl);
|
|
|
|
void onMessage(websocketpp::connection_hdl hdl);
|
|
|
|
|
|
|
|
WebSocketSession *GetWebSocketSession(websocketpp::connection_hdl hdl);
|
|
|
|
|
|
|
|
websocketpp::server<websocketpp::config::asio> _server;
|
|
|
|
QThreadPool _threadPool;
|
|
|
|
|
|
|
|
QMutex _sessionMutex;
|
|
|
|
std::map<websocketpp::connection_hdl, WebSocketSession, std::owner_less<websocketpp::connection_hdl>> _sessions;
|
|
|
|
};
|