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

0001 #ifndef SERVERMANAGER_H
0002 #define SERVERMANAGER_H
0003 
0004 #include <QHash>
0005 #include <QObject>
0006 #include <QReadWriteLock>
0007 #include <QTcpSocket>
0008 #include <QThread>
0009 
0010 //#include "networkmessage.h"
0011 #include <common/logcontroller.h>
0012 
0013 #include "network_global.h"
0014 /**
0015  * @brief The ServerManager class
0016  *
0017  *
0018  */
0019 class ChannelModel;
0020 class Channel;
0021 class RServer;
0022 class ConnectionAccepter;
0023 class MessageDispatcher;
0024 class ServerConnection;
0025 class NetworkMessageReader;
0026 class NETWORK_EXPORT ServerConnectionManager : public QObject
0027 {
0028     Q_OBJECT
0029     Q_PROPERTY(ChannelModel* channelModel READ channelModel CONSTANT)
0030 public:
0031     enum Channels
0032     {
0033         Unique,
0034         Several
0035     };
0036 
0037     explicit ServerConnectionManager(const QMap<QString, QVariant>& parameters, QObject* parent= nullptr);
0038     ~ServerConnectionManager();
0039 
0040     int countConnection() const;
0041     ChannelModel* channelModel() const;
0042     const QHash<QTcpSocket*, ServerConnection*> connections() const;
0043 
0044 signals:
0045 
0046     void eventOccured(QString, LogController::LogLevel);
0047     void messageMustBeDispatched(QByteArray array, Channel* channel, ServerConnection* client);
0048     void portChanged();
0049     void clientAccepted();
0050     void quitting();
0051     void finished();
0052 
0053 public slots:
0054     // controller
0055     void quit();
0056     void accept(qintptr handle, ServerConnection* connection);
0057 
0058     // network
0059     void messageReceived(QByteArray);
0060     void initClient();
0061 
0062     // void sendOffModel(ServerConnection*);
0063     // void sendOffModelToAll();
0064     void sendOffAdminAuthFail();
0065     void sendOffAdminAuthSuccessed();
0066 
0067     // Connection proccess tests
0068     void serverAcceptClient(ServerConnection* client);
0069     void checkAuthToServer(ServerConnection* client);
0070     void checkAuthToChannel(ServerConnection* client, QString channelId, QByteArray password);
0071     void checkAuthAsAdmin(ServerConnection* client);
0072 
0073     // Accessors
0074     void setChannelPassword(QString chanId, QByteArray passwd);
0075 
0076 protected:
0077     void removeSocket(QTcpSocket* socket);
0078     void kickClient(QString id, bool isAdmin, QString senderId);
0079     void banClient(QString id, bool isAdmin, QString senderId);
0080 
0081 protected slots:
0082     // admin
0083 
0084     void processMessageAdmin(NetworkMessageReader* msg, Channel* chan, ServerConnection* tcp);
0085     void sendOffAuthSuccessed();
0086     void sendOffAuthFail();
0087     void disconnectedUser();
0088     void error(QAbstractSocket::SocketError socketError);
0089     // memory
0090     void memoryChannelChanged(quint64);
0091 
0092 private:
0093     std::unique_ptr<ChannelModel> m_model;
0094     std::unique_ptr<ConnectionAccepter> m_corEndProcess;
0095     std::unique_ptr<ConnectionAccepter> m_tcpConnectionAccepter;
0096     std::unique_ptr<ConnectionAccepter> m_adminAccepter;
0097     std::unique_ptr<ConnectionAccepter> m_enterInRoomAccepter;
0098 
0099     MessageDispatcher* m_msgDispatcher= nullptr;
0100     QHash<QTcpSocket*, ServerConnection*> m_connections;
0101     const QMap<QString, QVariant>& m_parameters;
0102 
0103     mutable QReadWriteLock m_lock;
0104 };
0105 
0106 #endif // SERVERMANAGER_H