/** * @RDE_HEADER@ * The LtsWebSocketServer class processes messages from the client * $Id$ * $HeadURL$ * $Author$ * Created by Hannes Baeuerle, SLSE, 2016-08-25 */ #pragma once #include #include #include "client_message_factory.h" #include "logger.h" #include #include 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 &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 m_messageQueue; QTimer *m_queueTimer { nullptr }; AbstractClientMessageFactory *m_messageFactory { nullptr }; std::unique_ptr m_connectionHandler; qint32 m_messageDelay { 0 }; LOGGER_T m_logger; }; } // namespace LtsWebSocket