mirror of
https://gitea.msk.dinamika-avia.ru/Constanta-Design/RRJClient.git
synced 2026-03-28 05:25:39 +03:00
107 lines
2.3 KiB
C++
107 lines
2.3 KiB
C++
#include "tcpclient.h"
|
|
|
|
TCPClient::TCPClient(QObject *parent) :
|
|
QObject(parent)
|
|
{
|
|
}
|
|
|
|
void TCPClient::initialize(RecognizeSystem *recognize,ExternalExecuter *externalExecuter,SendSystem *sendSystem)
|
|
{
|
|
this->recognizeSystem = recognize;
|
|
this->externalExecuter = externalExecuter;
|
|
this->sendSystem = sendSystem;
|
|
isConnected = false;
|
|
|
|
connect(recognize,&RecognizeSystem::sigSocketWaitForReadyRead,this,&TCPClient::waitRead,Qt::DirectConnection);
|
|
|
|
emit sigSendDebugLog(Tools::getTime() + " Client started");
|
|
}
|
|
|
|
void TCPClient::setConnect(ServerSettings *serverSettings)
|
|
{
|
|
socket = new QTcpSocket();
|
|
qDebug() << "TCPCLient thread: " << thread();
|
|
if (socket != NULL && socket->state() == QTcpSocket::ConnectedState)
|
|
{
|
|
emit sigSendDebugLog("already connected");
|
|
return;
|
|
}
|
|
|
|
|
|
socket->connectToHost(serverSettings->Address,serverSettings->Port.toShort());
|
|
emit sigSendDebugLog("Try connect...");
|
|
isConnected = true;
|
|
|
|
if (socket->waitForConnected(2000))
|
|
{
|
|
connect(socket,&QTcpSocket::readyRead,this,&TCPClient::slotReadyRead,Qt::AutoConnection);
|
|
connect(socket,&QTcpSocket::disconnected,this,&TCPClient::setDisconnect);
|
|
|
|
sendSystem->setSocket(socket);
|
|
slotConnectNotify();
|
|
}
|
|
else
|
|
{
|
|
isConnected = false;
|
|
emit sigServerDisconnect();
|
|
}
|
|
}
|
|
|
|
|
|
void TCPClient::setDisconnect()
|
|
{
|
|
socket->disconnect();
|
|
isConnected = false;
|
|
emit sigServerDisconnect();
|
|
emit sigSendDebugLog("Server disabled");
|
|
}
|
|
|
|
|
|
void TCPClient:: waitRead(int time)
|
|
{
|
|
socket->waitForReadyRead(time);
|
|
}
|
|
|
|
QTcpSocket *TCPClient::getSocket()
|
|
{
|
|
return socket;
|
|
}
|
|
|
|
void TCPClient::slotConnectNotify()
|
|
{
|
|
if(socket->state() != QTcpSocket::ConnectedState)
|
|
{
|
|
isConnected = false;
|
|
emit sigSendDebugLog("Connect invalid");
|
|
emit sigConnectionState(false);
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
isConnected = true;
|
|
emit sigSendDebugLog("Connect complete");
|
|
emit sigConnectionState(true);
|
|
sendSystem->sendQTConnect();
|
|
}
|
|
}
|
|
|
|
void TCPClient::slotReadyRead()
|
|
{
|
|
if(!socket){
|
|
emit sigSendDebugLog("WRONG SOCKET");
|
|
return;
|
|
}
|
|
|
|
recognizeSystem->recognize(socket);
|
|
}
|
|
|
|
bool TCPClient::getIsConnected() const
|
|
{
|
|
return isConnected;
|
|
}
|
|
|
|
TCPClient::~TCPClient()
|
|
{
|
|
|
|
}
|