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

0001 /*************************************************************************
0002  *   Copyright (C) 2007 by Romain Campioni                                *
0003  *   Copyright (C) 2009 by Renaud Guezennec                              *
0004  *   Copyrigth (C) 2010 by Joseph Boudou                                 *
0005  *                                                                       *
0006  *   https://rolisteam.org/                                           *
0007  *                                                                       *
0008  *   rolisteam is free software; you can redistribute it and/or modify   *
0009  *   it under the terms of the GNU General Public License as published   *
0010  *   by the Free Software Foundation; either version 2 of the License,   *
0011  *   or (at your option) any later version.                              *
0012  *                                                                       *
0013  *   This program is distributed in the hope that it will be useful,     *
0014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
0015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
0016  *   GNU General Public License for more details.                        *
0017  *                                                                       *
0018  *   You should have received a copy of the GNU General Public License   *
0019  *   along with this program; if not, write to the                       *
0020  *   Free Software Foundation, Inc.,                                     *
0021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
0022  *************************************************************************/
0023 
0024 #ifndef NETWORKMANAGER_H
0025 #define NETWORKMANAGER_H
0026 
0027 #include <QList>
0028 #include <QTcpServer>
0029 
0030 #include <QState>
0031 #include <QStateMachine>
0032 #include <QTimer>
0033 
0034 #include "heartbeatsender.h"
0035 #include "network/clientconnection.h"
0036 #include "network/networkmessage.h"
0037 #include "network/networkmessagewriter.h"
0038 #include "network_global.h"
0039 
0040 /**
0041  * @brief ClientManager manages the state of current client: connected, authentified…
0042  * and send networkmessage to the server, or notify from new received messages.
0043  *
0044  */
0045 class NETWORK_EXPORT ClientManager : public QObject
0046 {
0047     Q_OBJECT
0048     Q_PROPERTY(bool ready READ ready NOTIFY readyChanged)
0049     Q_PROPERTY(ConnectionState connectionStateChanged READ connectionState NOTIFY connectionStateChanged)
0050 public:
0051     enum ConnectionState
0052     {
0053         UNREADY,
0054         DISCONNECTED,
0055         CONNECTING,
0056         CONNECTED,
0057         AUTHENTIFIED
0058     };
0059     Q_ENUM(ConnectionState)
0060 
0061     ClientManager(QObject* parent= nullptr);
0062     virtual ~ClientManager();
0063     ConnectionState connectionState() const;
0064 
0065     bool ready() const;
0066 public slots:
0067     // void processPlayerMessage(NetworkMessageReader* msg);
0068     // void processSetupMessage(NetworkMessageReader* msg);
0069 
0070     void connectTo(const QString& host, int port);
0071     void disconnectAndClose();
0072     void reset();
0073     void setAuthentificationStatus(bool status);
0074 
0075 signals:
0076     void messageReceived(QByteArray);
0077     void dataReceived(quint64, quint64);
0078 
0079     void connectionStateChanged(ClientManager::ConnectionState);
0080     void notifyUser(QString);
0081     void gameMasterStatusChanged(bool status);
0082 
0083     // State signal
0084     void connecting();
0085     void stopConnecting();
0086     void connectedToServer();
0087     void authentificationSuccessed();
0088     void authentificationFailed();
0089 
0090     void readyChanged();
0091     void clearData();
0092     void moveToAnotherChannel();
0093 
0094 private slots:
0095     void setConnectionState(ClientManager::ConnectionState);
0096     void setReady(bool ready);
0097 
0098 private:
0099     std::unique_ptr<ClientConnection> m_networkLinkToServer;
0100     ConnectionState m_connectionState= UNREADY;
0101     bool m_ready= false;
0102     bool m_isAdmin= false;
0103 
0104     QState* m_connecting= nullptr;
0105     QState* m_connected= nullptr;
0106     QState* m_waitingData= nullptr;
0107     QState* m_authentified= nullptr;
0108     QState* m_error= nullptr;
0109     QState* m_disconnected= nullptr;
0110     QStateMachine m_states;
0111 };
0112 
0113 #endif