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  *   Copyright (C) 2011 by Joseph Boudou                                 *
0005  *                                                                       *
0006  *   rolisteam is free software; you can redistribute it and/or modify   *
0007  *   it under the terms of the GNU General Public License as published   *
0008  *   by the Free Software Foundation; either version 2 of the License,   *
0009  *   or (at your option) any later version.                              *
0010  *                                                                       *
0011  *   This program is distributed in the hope that it will be useful,     *
0012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
0013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
0014  *   GNU General Public License for more details.                        *
0015  *                                                                       *
0016  *   You should have received a copy of the GNU General Public License   *
0017  *   along with this program; if not, write to the                       *
0018  *   Free Software Foundation, Inc.,                                     *
0019  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
0020  *************************************************************************/
0021 
0022 #ifndef CLIENTCONNECTION_H
0023 #define CLIENTCONNECTION_H
0024 
0025 #include <QPointer>
0026 #include <QTcpSocket>
0027 #include <QtNetwork>
0028 
0029 #include "connectionprofile.h"
0030 #include "network/networkmessage.h"
0031 #include "network_global.h"
0032 #include "networkreceiver.h"
0033 
0034 /**
0035  * @brief The NetworkLink [Client side] class.
0036  * Send data from client to server
0037  * Read data from server to client
0038  * manage the socket to server
0039  */
0040 class NETWORK_EXPORT ClientConnection : public QObject, public MessageSenderInterface
0041 {
0042     Q_OBJECT
0043     Q_PROPERTY(bool connected READ connected NOTIFY connectedChanged)
0044 public:
0045     ClientConnection();
0046     virtual ~ClientConnection() override;
0047 
0048     bool connected() const;
0049     QString lastErrorMessage() const;
0050 
0051 public slots:
0052     void reset();
0053     void connectTo(const QString& host, int port);
0054     void closeCommunicationWithServer();
0055 
0056     void sendData(char* data, qint64 size);
0057     void sendMessage(const NetworkMessage* msg) override;
0058 
0059 signals:
0060     void readDataReceived(quint64, quint64);
0061     void messageReceived(const QByteArray);
0062     void connectedChanged(bool);
0063     void errorOccured(QString error);
0064 
0065 private slots:
0066     void setConnected(bool);
0067     void receivingData();
0068 
0069 private:
0070     void makeSignalConnection();
0071 
0072 private:
0073     QPointer<QTcpSocket> m_socketTcp;
0074     bool m_inError= false;
0075     bool m_connected= false;
0076     QString m_lastErrorMsg;
0077 
0078     NetworkMessageHeader m_header;
0079     bool m_receivingData;
0080     char* m_buffer= nullptr;
0081     quint32 m_remainingData;
0082     quint64 m_headerRead;
0083 };
0084 
0085 #endif // CLIENTCONNECTION_H