Files
qt5-legatus-legacy/Lts/Common/LtsWebSocket/lts_web_socket_server.h
T
2026-08-13 18:33:10 +02:00

121 lines
3.4 KiB
C++
Executable File

/**
* @RDE_HEADER@
* The LtsWebSocketServer class processes messages from the client
* $Id$
* $HeadURL$
* $Author$
* Created by Hannes Baeuerle, SLSE, 2016-08-25
*/
#pragma once
#include <QtCore/QObject>
#include <LtsWebSocket/ltswebsocketserver_export.h>
#include "client_message_factory.h"
#include "logger.h"
#include <memory>
#include <QQueue>
class QTimer;
namespace LtsWebSocket
{
class ConnectionHandler;
class ClientConnection;
/**
* @brief The LtsWebSocketServer class processes messages from a client connection and starts the parsing of them
*/
class LTSWEBSOCKETSERVER_EXPORT LtsWebSocketServer
: public QObject
{
Q_OBJECT
public:
/**
* @brief LtsWebSocketServer Constructor
* @param messageFactory Convenience class for creating ClientMessage
* @param parent QObject parent
*/
explicit LtsWebSocketServer(AbstractClientMessageFactory *messageFactory, QObject *parent = nullptr);
virtual ~LtsWebSocketServer();
/**
* @brief init Initialises the LtsWebSocketServer to work properly
* @param serviceName Is Used to name the server (RDE-servicename)
* @param port Port the server is listening, if 0 the port number will be random
* @return True on success, otherwise false
*/
bool init(const QString &serviceName, const unsigned short port = 0);
/**
* @brief init Initialises the LtsWebSocketServer to work properly
* @param serviceName Is Used to name the server (RDE-servicename)
* @param portRange Range where to look at for a port. If invalid (valid range is 1025-65535) a random port is chosen
* @return True on success, otherwise false
*/
bool init(const QString &serviceName, const QPair<quint16, quint16> &portRange);
/**
* @brief sendToAll Sends the given msg to all connected Clients
* @param message Message to be send
*/
void sendToAllClients(ReplyMessage *message);
/**
* @brief webSocketPort returns the listen port
* @return listen port
*/
unsigned short webSocketPort() const;
/**
* @brief setMessageDelay Sets the delay between two messages to be processed
* @param delay Delay in milliseconds
*/
void setMessageDelay(qint32 delay);
signals:
/**
* @brief disconnected Emitted when a client disconnects
* @param client The client
*/
void clientDisconnected(ClientConnection *client);
/**
* @brief clientConnected Emitted when a client connects
* @param client The client
*/
void clientConnected(ClientConnection *client);
private slots:
/**
* @brief onNewTextMessage Slot to react to new messages from clients
* @param client Client to handle a reply
* @param message Message from a client
*/
void onNewTextMessage(ReplyHandler *client, const QString &message);
/**
* @brief onQueueTimerTimeout Timeout for messageprocessing
*/
void onQueueTimerTimeout();
private:
struct MessageCredentials
{
ReplyHandler *client {nullptr};
QString message;
MessageCredentials(ReplyHandler *client, const QString &message)
: client(client)
, message(message)
{}
};
QQueue<MessageCredentials> m_messageQueue;
QTimer *m_queueTimer { nullptr };
AbstractClientMessageFactory *m_messageFactory { nullptr };
std::unique_ptr<ConnectionHandler> m_connectionHandler;
qint32 m_messageDelay { 0 };
LOGGER_T m_logger;
};
} // namespace LtsWebSocket