File indexing completed on 2024-05-12 05:39:52

0001 #ifndef RSERVER_H
0002 #define RSERVER_H
0003 
0004 #include <QDebug>
0005 #include <QList>
0006 #include <QObject>
0007 #include <QPair>
0008 #include <QTcpServer>
0009 #include <QThread>
0010 
0011 #include "connectionaccepter.h"
0012 #include "network_global.h"
0013 #include "serverconnection.h"
0014 #include "serverconnectionmanager.h"
0015 
0016 #include "updater/controller/servermanagerupdater.h"
0017 
0018 struct NETWORK_EXPORT ThreadInfo
0019 {
0020     QPointer<QThread> m_thread;
0021     int m_connectionCount;
0022 };
0023 
0024 class NETWORK_EXPORT RServer : public QTcpServer
0025 {
0026     Q_OBJECT
0027     Q_PROPERTY(qint64 port READ port WRITE setPort NOTIFY portChanged)
0028     Q_PROPERTY(ServerState state READ state NOTIFY stateChanged)
0029     Q_PROPERTY(int threadCount READ threadCount CONSTANT)
0030     Q_PROPERTY(bool internal READ internal CONSTANT)
0031 public:
0032     enum ServerState
0033     {
0034         Stopped,
0035         Listening,
0036         Error
0037     };
0038     Q_ENUM(ServerState)
0039     explicit RServer(const QMap<QString, QVariant>& parameter, bool internal, QObject* parent= nullptr);
0040     virtual ~RServer() override;
0041 
0042     virtual bool listen();
0043     virtual void close();
0044     virtual qint64 port();
0045     bool internal() const;
0046 
0047     int threadCount() const;
0048 
0049     RServer::ServerState state() const;
0050 
0051 public slots:
0052     void setPort(int p);
0053 
0054 protected:
0055     virtual void incomingConnection(qintptr descriptor) override; // qint64, qHandle, qintptr, uint
0056     virtual void accept(qintptr descriptor, ServerConnection* connection);
0057 
0058 protected slots:
0059     void setState(const RServer::ServerState& state);
0060     void complete();
0061     void runUpnpNat();
0062 
0063 signals:
0064     void stateChanged(RServer::ServerState);
0065     void accepting(qintptr handle, ServerConnection* connection);
0066     void finished();
0067     void completed();
0068     void portChanged();
0069     void eventOccured(QString message, LogController::LogLevel type);
0070 
0071 protected:
0072     int m_threadCount{1};
0073     std::unique_ptr<QThread> m_connectionThread;
0074     QList<ThreadInfo> m_threadPool;
0075     std::unique_ptr<ServerConnectionManager> m_connectionsManager;
0076     std::unique_ptr<ConnectionAccepter> m_corConnection;
0077     std::unique_ptr<ServerManagerUpdater> m_updater;
0078     const QMap<QString, QVariant>& m_data;
0079     ServerState m_state= Stopped;
0080     quint16 m_port{6660};
0081     bool m_internal{true};
0082 };
0083 #endif // RSERVER_H