Newer
Older
CasicIrisIdentify / utils / SocketClientUtil.cpp
tanyue on 16 Dec 2023 1 KB 20231216 debug on ubuntu arm
#include "SocketClientUtil.h"
#include "utils/ByteUtil.h"

SocketClientUtil::SocketClientUtil(QObject *parent) : QObject(parent)
{
    QObject::connect(&objClient, &QTcpSocket::readyRead, this, &SocketClientUtil::readData);
    QObject::connect(&objClient, &QTcpSocket::disconnected, this, &SocketClientUtil::socketReconnect);
}

void SocketClientUtil::connect(QString host, int port)
{
    this->host = host;
    this->port = port;

    objClient.connectToHost(this->host, this->port);
}

void SocketClientUtil::closeConnect()
{
    QObject::disconnect(&objClient, &QTcpSocket::readyRead, this, &SocketClientUtil::readData);
    QObject::disconnect(&objClient, &QTcpSocket::disconnected, this, &SocketClientUtil::socketReconnect);

    objClient.disconnectFromHost();
    objClient.close();
}

QByteArray SocketClientUtil::getResponse()
{
    return this->response;
}

void SocketClientUtil::resetRecvBuffer()
{
    response.clear();
    response.resize(0);
}

void SocketClientUtil::sendData(QByteArray data)
{
//    data.append(0x0D).append(0x0A); // 自动加\r\n标识一帧结束
    objClient.write(data);
}

void SocketClientUtil::readData()
{
    QByteArray buffer = objClient.readAll();
    response.append(buffer);

    if (response.size() >= 320 * 240 * 3) {
        emit this->responseReaded();
    }
}

void SocketClientUtil::socketReconnect()
{
    objClient.close();
    objClient.connectToHost(this->host, this->port);
}