File indexing completed on 2024-05-05 05:40:32
0001 /************************************************************************ 0002 * Copyright (C) 2007 by Romain Campioni * 0003 * Copyright (C) 2009 by Renaud Guezennec * 0004 * Copyright (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 #include "network/clientmanager.h" 0025 0026 #include <QDebug> 0027 #include <QTcpSocket> 0028 0029 #include "data/person.h" 0030 #include "data/player.h" 0031 #include "network/clientconnection.h" 0032 #include "network/connectionprofile.h" 0033 0034 #define second 1000 0035 0036 /***************** 0037 * ClientManager * 0038 *****************/ 0039 ClientManager::ClientManager(QObject* parent) : QObject(parent), m_networkLinkToServer(new ClientConnection) 0040 { 0041 qRegisterMetaType<ClientConnection*>("ClientConnection*"); 0042 qRegisterMetaType<char*>("char*"); 0043 NetworkMessage::setMessageSender(m_networkLinkToServer.get()); 0044 0045 m_connecting= new QState(); 0046 m_connected= new QState(); 0047 m_authentified= new QState(); 0048 m_error= new QState(); 0049 m_disconnected= new QState(); 0050 0051 connect(m_connected, &QAbstractState::entered, this, [this]() { 0052 qDebug() << "client connected state"; 0053 setConnectionState(CONNECTED); 0054 emit connectedToServer(); 0055 }); 0056 connect(m_connecting, &QAbstractState::entered, this, [this]() { 0057 qDebug() << "client connecting state"; 0058 setConnectionState(CONNECTING); 0059 }); 0060 0061 connect(m_disconnected, &QAbstractState::entered, this, [this]() { 0062 qDebug() << "client disconnected state"; 0063 setConnectionState(DISCONNECTED); 0064 m_networkLinkToServer->reset(); 0065 setReady(true); 0066 }); 0067 connect(m_authentified, &QAbstractState::entered, this, [this]() { 0068 qDebug() << "client authentified state"; 0069 setConnectionState(AUTHENTIFIED); 0070 }); 0071 0072 connect(m_error, &QAbstractState::entered, this, [=]() { 0073 qDebug() << "Error state"; 0074 setConnectionState(DISCONNECTED); 0075 }); 0076 0077 m_states.addState(m_connecting); 0078 m_states.addState(m_connected); 0079 m_states.addState(m_authentified); 0080 m_states.addState(m_disconnected); 0081 m_states.addState(m_error); 0082 m_states.setInitialState(m_disconnected); 0083 0084 m_disconnected->addTransition(this, &ClientManager::connecting, m_connecting); 0085 m_connecting->addTransition(m_networkLinkToServer.get(), &ClientConnection::connectedChanged, m_connected); 0086 m_connected->addTransition(this, &ClientManager::authentificationSuccessed, m_authentified); 0087 0088 m_authentified->addTransition(m_networkLinkToServer.get(), &ClientConnection::connectedChanged, m_disconnected); 0089 m_connected->addTransition(m_networkLinkToServer.get(), &ClientConnection::connectedChanged, m_disconnected); 0090 0091 m_connecting->addTransition(this, &ClientManager::authentificationFailed, m_disconnected); 0092 m_connecting->addTransition(this, &ClientManager::stopConnecting, m_disconnected); 0093 m_connected->addTransition(this, &ClientManager::authentificationFailed, m_disconnected); 0094 0095 m_error->addTransition(this, &ClientManager::connecting, m_connecting); 0096 m_connecting->addTransition(m_networkLinkToServer.get(), &ClientConnection::errorOccured, m_error); 0097 m_connected->addTransition(m_networkLinkToServer.get(), &ClientConnection::errorOccured, m_error); 0098 m_authentified->addTransition(m_networkLinkToServer.get(), &ClientConnection::errorOccured, m_error); 0099 0100 connect(m_networkLinkToServer.get(), &ClientConnection::messageReceived, this, &ClientManager::messageReceived); 0101 connect(m_networkLinkToServer.get(), &ClientConnection::readDataReceived, this, &ClientManager::dataReceived); 0102 // connect(m_networkLinkToServer.get(), &NetworkLink::errorChanged, this, &ClientManager::errorOccur); 0103 // connect(m_networkLinkToServer, &NetworkLink::clearData, this, &ClientManager::clearData); 0104 /*connect(m_networkLinkToServer, &NetworkLink::gameMasterStatusChanged, this, 0105 &ClientManager::gameMasterStatusChanged);*/ 0106 // connect(m_networkLinkToServer, &NetworkLink::moveToAnotherChannel, this, &ClientManager::moveToAnotherChannel); 0107 0108 connect(&m_states, &QStateMachine::started, this, [this]() { setReady(true); }); 0109 0110 m_states.start(); 0111 } 0112 0113 ClientManager::~ClientManager() {} 0114 0115 void ClientManager::connectTo(const QString& host, int port) 0116 { 0117 m_networkLinkToServer->connectTo(host, port); 0118 emit connecting(); 0119 } 0120 0121 void ClientManager::disconnectAndClose() 0122 { 0123 m_networkLinkToServer->closeCommunicationWithServer(); 0124 emit stopConnecting(); 0125 emit notifyUser(tr("Connection to the server has been closed.")); 0126 } 0127 0128 ClientManager::ConnectionState ClientManager::connectionState() const 0129 { 0130 return m_connectionState; 0131 } 0132 0133 bool ClientManager::ready() const 0134 { 0135 return (m_states.isRunning() & (m_connectionState == DISCONNECTED)); 0136 } 0137 0138 void ClientManager::setAuthentificationStatus(bool status) 0139 { 0140 if(status) 0141 emit authentificationSuccessed(); 0142 else 0143 emit authentificationFailed(); 0144 // m_networkLinkToServer->adminAuthSuccessed(); 0145 } 0146 0147 void ClientManager::setConnectionState(ConnectionState state) 0148 { 0149 if(m_connectionState == state) 0150 return; 0151 0152 m_connectionState= state; 0153 emit connectionStateChanged(m_connectionState); 0154 } 0155 0156 void ClientManager::setReady(bool ready) 0157 { 0158 if(ready == m_ready) 0159 return; 0160 m_ready= ready; 0161 emit readyChanged(); 0162 } 0163 0164 void ClientManager::reset() 0165 { 0166 // auto const connection= new QMetaObject::Connection; 0167 // connect(this, &ClientManager::isDisconnected, this, [this]() { emit isReady(); }); 0168 0169 m_networkLinkToServer->reset(); 0170 }