You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.7 KiB
75 lines
1.7 KiB
#include <QCoreApplication>
|
|
#include <QDebug>
|
|
|
|
#include "qscrcpyevent.h"
|
|
#include "devicesocket.h"
|
|
|
|
DeviceSocket::DeviceSocket(QObject *parent) : QTcpSocket(parent)
|
|
{
|
|
connect(this, &DeviceSocket::readyRead, this, &DeviceSocket::onReadyRead);
|
|
connect(this, &DeviceSocket::aboutToClose, this, &DeviceSocket::quitNotify);
|
|
connect(this, &DeviceSocket::disconnected, this, &DeviceSocket::quitNotify);
|
|
}
|
|
|
|
DeviceSocket::~DeviceSocket()
|
|
{
|
|
quitNotify();
|
|
}
|
|
|
|
qint32 DeviceSocket::recvData(quint8 *buf, qint32 bufSize)
|
|
{
|
|
QMutexLocker locker(&m_mutex);
|
|
|
|
m_buffer = buf;
|
|
m_bufferSize = bufSize;
|
|
m_dataSize = 0;
|
|
|
|
// post event
|
|
DeviceSocketEvent* getDataEvent = new DeviceSocketEvent();
|
|
QCoreApplication::postEvent(this, getDataEvent);
|
|
|
|
// wait
|
|
while (!m_recvData) {
|
|
m_recvDataCond.wait(&m_mutex);
|
|
}
|
|
|
|
m_recvData = false;
|
|
return m_dataSize;
|
|
}
|
|
|
|
bool DeviceSocket::event(QEvent *event)
|
|
{
|
|
if (event->type() == QScrcpyEvent::DeviceSocket) {
|
|
onReadyRead();
|
|
return true;
|
|
}
|
|
return QTcpSocket::event(event);
|
|
}
|
|
|
|
void DeviceSocket::onReadyRead()
|
|
{
|
|
QMutexLocker locker(&m_mutex);
|
|
if (m_buffer && 0 < bytesAvailable()) {
|
|
// recv data
|
|
qint64 readSize = qMin(bytesAvailable(), (qint64)m_bufferSize);
|
|
m_dataSize = read((char*)m_buffer, readSize);
|
|
|
|
m_buffer = Q_NULLPTR;
|
|
m_bufferSize = 0;
|
|
m_recvData = true;
|
|
m_recvDataCond.wakeOne();
|
|
}
|
|
}
|
|
|
|
void DeviceSocket::quitNotify()
|
|
{
|
|
QMutexLocker locker(&m_mutex);
|
|
if (m_buffer) {
|
|
m_buffer = Q_NULLPTR;
|
|
m_bufferSize = 0;
|
|
m_recvData = true;
|
|
m_dataSize = 0;
|
|
m_recvDataCond.wakeOne();
|
|
}
|
|
}
|