Warning, file /rolisteam/rolisteam/src/libraries/core/include/network/serverconnection.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*************************************************************************
0002  *   Copyright (C) 2011 by Joseph Boudou                                 *
0003  *   Copyright (C) 2015 by Renaud Guezennec                              *
0004  *                                                                       *
0005  *   https://rolisteam.org/                                           *
0006  *                                                                       *
0007  *   Rolisteam is free software; you can redistribute it and/or modify   *
0008  *   it under the terms of the GNU General Public License as published   *
0009  *   by the Free Software Foundation; either version 2 of the License,   *
0010  *   or (at your option) any later version.                              *
0011  *                                                                       *
0012  *   This program is distributed in the hope that it will be useful,     *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
0015  *   GNU General Public License for more details.                        *
0016  *                                                                       *
0017  *   You should have received a copy of the GNU General Public License   *
0018  *   along with this program; if not, write to the                       *
0019  *   Free Software Foundation, Inc.,                                     *
0020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
0021  *************************************************************************/
0022 #ifndef SERVERCONNECTION_H
0023 #define SERVERCONNECTION_H
0024 
0025 #include <QObject>
0026 #include <QPointer>
0027 #include <QState>
0028 #include <QStateMachine>
0029 #include <QTcpSocket>
0030 
0031 #include "network_global.h"
0032 
0033 #include "network/networkmessage.h"
0034 #include "network/networkmessagereader.h"
0035 #include "network/networkmessagewriter.h"
0036 #include "network/treeitem.h"
0037 
0038 #include "data/player.h"
0039 
0040 class Channel;
0041 /**
0042  * @brief The TcpClient class represents connection state with client on server side.
0043  */
0044 class NETWORK_EXPORT ServerConnection : public TreeItem
0045 {
0046     Q_OBJECT
0047     Q_PROPERTY(QString playerName READ playerName NOTIFY playerNameChanged)
0048     Q_PROPERTY(QString playerId READ playerId() NOTIFY playerIdChanged)
0049 
0050 public:
0051     enum ConnectionEvent
0052     {
0053         CheckSuccessEvent,
0054         CheckFailEvent,
0055         ControlFailEvent,
0056         ControlSuccessEvent,
0057         ServerAuthDataReceivedEvent,
0058         ServerAuthFailEvent,
0059         ServerAuthSuccessEvent,
0060         AdminAuthFailEvent,
0061         AdminAuthSuccessEvent,
0062         ChannelAuthSuccessEvent,
0063         ChannelAuthFailEvent,
0064         ChannelAuthDataReceivedEvent,
0065         ChannelChanged,
0066         MoveChanEvent
0067     };
0068     Q_ENUM(ConnectionEvent)
0069     /**
0070      * @brief ServerConnection
0071      * @param socket
0072      * @param parent
0073      */
0074     explicit ServerConnection(QTcpSocket* socket, QObject* parent= nullptr);
0075     ~ServerConnection();
0076     /**
0077      * @brief getParentChannel
0078      * @return
0079      */
0080     Channel* getParentChannel() const;
0081     /**
0082      * @brief setParentChannel
0083      * @param parent
0084      */
0085     void setParentChannel(Channel* parent);
0086     void readFromJson(QJsonObject& json);
0087     void writeIntoJson(QJsonObject& json);
0088     virtual int indexOf(TreeItem*);
0089     /**
0090      * @brief getSocket
0091      * @return
0092      */
0093     QTcpSocket* getSocket();
0094     void resetStateMachine();
0095     qintptr getSocketHandleId() const;
0096     void setSocketHandleId(const qintptr& socketHandleId);
0097 
0098     bool isGM() const;
0099     QString playerId() const;
0100     void setPlayerId(const QString& id);
0101     QString playerName() const;
0102 
0103     void setInfoPlayer(NetworkMessageReader* msg);
0104     void fill(NetworkMessageWriter* msg);
0105 
0106     // void addPlayerFeature(QString uuid, QString name, quint8 version);
0107 
0108     bool isFullyDefined();
0109 
0110     bool isAdmin() const;
0111     void setIsAdmin(bool isAdmin);
0112     bool isConnected() const;
0113     QString getIpAddress();
0114     QString getWantedChannel();
0115     QString getServerPassword() const;
0116     QString getAdminPassword() const;
0117     QString getChannelPassword() const;
0118 signals:
0119     void readDataReceived(quint64, quint64);
0120     void dataReceived(QByteArray);
0121 
0122     void checkSuccess();
0123     void checkFail();
0124 
0125     void controlFail();
0126     void controlSuccess();
0127 
0128     void serverAuthDataReceived();
0129     void serverAuthFail();
0130     void serverAuthSuccess();
0131 
0132     void adminAuthFailed();
0133     void adminAuthSucceed();
0134 
0135     void channelAuthFail();
0136     void channelAuthSuccess();
0137 
0138     void moveChannel();
0139 
0140     void clientSaysGoodBye(QString id);
0141     void playerStatusChanged();
0142 
0143     // Signal to check
0144     void checkServerAcceptClient(ServerConnection* client);
0145     void checkServerPassword(ServerConnection* client);
0146     void checkAdminPassword(ServerConnection* client);
0147     void checkChannelPassword(ServerConnection* client, QString channelId, QByteArray password);
0148     void channelPassword(QString channelId, QByteArray password);
0149 
0150     void isReady();
0151     void hasNoRestriction();
0152     void hasRestriction();
0153     void socketDisconnection();
0154     void socketError(QAbstractSocket::SocketError);
0155     void socketInitiliazed();
0156     void protocolViolation();
0157 
0158     // properties signals
0159     void playerNameChanged();
0160     void playerIdChanged();
0161     void playerInfoDefined();
0162 public slots:
0163     void receivingData();
0164     void forwardMessage();
0165     void sendMessage(NetworkMessage* msg, bool deleteMsg);
0166     void connectionError(QAbstractSocket::SocketError error);
0167     void sendEvent(ServerConnection::ConnectionEvent);
0168     void startReading();
0169     void closeConnection();
0170 
0171 protected:
0172     bool isCurrentState(QState* state);
0173     void readAdministrationMessages(NetworkMessageReader& msg);
0174     void sendOffChannelChanged();
0175 
0176 private:
0177     QPointer<QTcpSocket> m_socket; // use std::unique_ptr
0178     NetworkMessageHeader m_header= {0, 0, 0};
0179     char* m_buffer= nullptr;
0180     quint64 m_headerRead;
0181     quint64 m_remainingData;
0182 
0183     QStateMachine* m_stateMachine= nullptr;
0184     QState* m_incomingConnection= nullptr;
0185     QState* m_controlConnection= nullptr;
0186     QState* m_authentificationServer= nullptr;
0187     QState* m_disconnected= nullptr;
0188 
0189     QStateMachine* m_connected= nullptr;
0190     QState* m_inChannel= nullptr;
0191     QState* m_wantToGoToChannel= nullptr;
0192 
0193     QState* m_currentState= nullptr;
0194 
0195     bool m_isAdmin= false;
0196     bool m_forwardMessage= false;
0197 
0198     bool m_waitingData= false;
0199     bool m_receivingData= false;
0200     quint32 m_dataReceivedTotal= 0;
0201     std::unique_ptr<Player> m_player;
0202     qintptr m_socketHandleId;
0203     QString m_wantedChannel;
0204 
0205     bool m_knownUser= false;
0206     QString m_serverPassword;
0207     QString m_adminPassword;
0208     QString m_channelPassword;
0209 };
0210 Q_DECLARE_METATYPE(ServerConnection::ConnectionEvent)
0211 #endif // SERVERCONNECTION_H