mirror of
https://github.com/barry-ran/QtScrcpy.git
synced 2025-04-20 19:44:59 +00:00
更新devicesocket
This commit is contained in:
parent
0747472fb5
commit
6ddbf115e9
3 changed files with 49 additions and 1 deletions
|
@ -1,6 +1,39 @@
|
|||
#include "devicesocket.h"
|
||||
|
||||
DeviceSocket::DeviceSocket(QObject *parent) : QTcpSocket(parent)
|
||||
{
|
||||
connect(this, &DeviceSocket::readyRead, this, &DeviceSocket::onReadyRead);
|
||||
}
|
||||
|
||||
DeviceSocket::~DeviceSocket()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
qint32 DeviceSocket::recvData(quint8 *buf, qint32 bufSize)
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
||||
m_buffer = buf;
|
||||
m_bufferSize = bufSize;
|
||||
// post event
|
||||
while (!m_recvData) {
|
||||
m_recvDataCond.wait(&m_mutex);
|
||||
}
|
||||
return m_dataSize;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,16 +2,30 @@
|
|||
#define DEVICESOCKET_H
|
||||
|
||||
#include <QTcpSocket>
|
||||
#include <QMutex>
|
||||
#include <QWaitCondition>
|
||||
|
||||
class DeviceSocket : public QTcpSocket
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit DeviceSocket(QObject *parent = nullptr);
|
||||
virtual ~DeviceSocket();
|
||||
|
||||
qint32 recvData(quint8* buf, qint32 bufSize);
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
protected slots:
|
||||
void onReadyRead();
|
||||
|
||||
private:
|
||||
QMutex m_mutex;
|
||||
QWaitCondition m_recvDataCond;
|
||||
bool m_recvData = false;
|
||||
quint8* m_buffer = Q_NULLPTR;
|
||||
qint32 m_bufferSize = 0;
|
||||
qint32 m_dataSize = 0;
|
||||
};
|
||||
|
||||
#endif // DEVICESOCKET_H
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#ifndef FRAMES_H
|
||||
#define FRAMES_H
|
||||
|
||||
#include <QMutex>
|
||||
#include <QWaitCondition>
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue