1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <QtNetwork>
#include "client.h"
//-------------------------------------------------------------------------------------------------
Client::Client(const QString& strHostIPAdress, int iPort, QObject *parent /*= 0*/)
: QObject(parent)
, m_strHostIPAdress(strHostIPAdress)
, m_iPort(iPort)
, m_bIsConnected(false)
, m_iBlockSize(0)
{
m_TcpSocket = new QTcpSocket(this);
connect(m_TcpSocket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead()));
connect(m_TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
connect(m_TcpSocket, SIGNAL(connected()), this, SLOT(onConnectedToHost()));
connect(m_TcpSocket, SIGNAL(disconnected()), this, SLOT(onDisconnectedFromHost()));
connect(&m_RetryConnectionTimer, SIGNAL(timeout()), this, SLOT(connectToHost()));
connectToHost();
m_RetryConnectionTimer.start(iDTReconnection);
QTimer *timer = new QTimer();
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
timer->start(1000);
}
//-------------------------------------------------------------------------------------------------
void Client::onTimeout()
{
addSendMessage("Blabla");
}
//-------------------------------------------------------------------------------------------------
Client::~Client()
{
delete m_TcpSocket;
}
//-------------------------------------------------------------------------------------------------
void Client::connectToHost()
{
if (!m_bIsConnected)
{
m_iBlockSize = 0;
m_TcpSocket->connectToHost(m_strHostIPAdress, m_iPort, QIODevice::ReadWrite);
m_TcpSocket->waitForConnected();
}
}
//-------------------------------------------------------------------------------------------------
void Client::onConnectedToHost()
{
m_bIsConnected = true;
qDebug() << "Client::connectToHost() : Connected.";
}
//-------------------------------------------------------------------------------------------------
void Client::disconnect()
{
m_TcpSocket->disconnectFromHost();
m_RetryConnectionTimer.stop();
}
//-------------------------------------------------------------------------------------------------
void Client::onDisconnectedFromHost()
{
m_bIsConnected = false;
}
//-------------------------------------------------------------------------------------------------
void Client::onSocketReadyRead()
{
QDataStream in(m_TcpSocket);
in.setVersion(QDataStream::Qt_4_7);
if (m_iBlockSize == 0)
{
if (m_TcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> m_iBlockSize;
}
if (m_TcpSocket->bytesAvailable() < m_iBlockSize)
return;
m_iBlockSize = 0;
QString lastMsg;
in >> lastMsg;
qDebug() << lastMsg;
emit newMessage(lastMsg);
}
//-------------------------------------------------------------------------------------------------
void Client::onSocketError(QAbstractSocket::SocketError socketError)
{
m_bIsConnected = false;
}
//-------------------------------------------------------------------------------------------------
void Client::addSendMessage(const QString& sendMessage)
{
if (m_bIsConnected && !sendMessage.isEmpty())
{
QByteArray packet;
QDataStream out(&packet, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << (quint16)0;
out << sendMessage;
out.device()->seek(0);
out << (quint16)(packet.size() - sizeof(quint16));
m_TcpSocket->write(packet);
m_TcpSocket->waitForBytesWritten();
}
} |
Partager