|
2 | 2 | #include <QDebug> |
3 | 3 | #include <QThread> |
4 | 4 |
|
5 | | -#include "qscrcpyevent.h" |
6 | 5 | #include "videosocket.h" |
7 | 6 |
|
8 | 7 | VideoSocket::VideoSocket(QObject *parent) : QTcpSocket(parent) |
9 | 8 | { |
10 | | - connect(this, &VideoSocket::readyRead, this, &VideoSocket::onReadyRead); |
11 | | - connect(this, &VideoSocket::aboutToClose, this, &VideoSocket::quitNotify); |
12 | | - connect(this, &VideoSocket::disconnected, this, &VideoSocket::quitNotify); |
13 | 9 | } |
14 | 10 |
|
15 | 11 | VideoSocket::~VideoSocket() |
16 | 12 | { |
17 | | - quitNotify(); |
18 | 13 | } |
19 | 14 |
|
20 | 15 | qint32 VideoSocket::subThreadRecvData(quint8 *buf, qint32 bufSize) |
21 | 16 | { |
22 | | - // this function cant call in main thread |
23 | | - Q_ASSERT(QCoreApplication::instance()->thread() != QThread::currentThread()); |
24 | | - if (m_quit) { |
| 17 | + if (!buf) { |
25 | 18 | return 0; |
26 | 19 | } |
27 | | - QMutexLocker locker(&m_mutex); |
28 | | - |
29 | | - m_buffer = buf; |
30 | | - m_bufferSize = bufSize; |
31 | | - m_dataSize = 0; |
32 | | - |
33 | | - // post event |
34 | | - VideoSocketEvent *getDataEvent = new VideoSocketEvent(); |
35 | | - QCoreApplication::postEvent(this, getDataEvent); |
36 | | - |
37 | | - // wait |
38 | | - while (!m_recvData) { |
39 | | - m_recvDataCond.wait(&m_mutex); |
40 | | - } |
41 | | - |
42 | | - m_recvData = false; |
43 | | - return m_dataSize; |
44 | | -} |
45 | | - |
46 | | -bool VideoSocket::event(QEvent *event) |
47 | | -{ |
48 | | - if (static_cast<QScrcpyEvent::Type>(event->type()) == QScrcpyEvent::VideoSocket) { |
49 | | - onReadyRead(); |
50 | | - return true; |
51 | | - } |
52 | | - return QTcpSocket::event(event); |
53 | | -} |
54 | | - |
55 | | -void VideoSocket::onReadyRead() |
56 | | -{ |
57 | | - QMutexLocker locker(&m_mutex); |
58 | | - if (m_buffer && m_bufferSize <= bytesAvailable()) { |
59 | | - // recv data |
60 | | - qint64 readSize = qMin(bytesAvailable(), (qint64)m_bufferSize); |
61 | | - m_dataSize = read((char *)m_buffer, readSize); |
| 20 | + // this function cant call in main thread |
| 21 | + Q_ASSERT(QCoreApplication::instance()->thread() != QThread::currentThread()); |
62 | 22 |
|
63 | | - m_buffer = Q_NULLPTR; |
64 | | - m_bufferSize = 0; |
65 | | - m_recvData = true; |
66 | | - m_recvDataCond.wakeOne(); |
| 23 | + while (bytesAvailable() < bufSize) { |
| 24 | + if (!waitForReadyRead(-1)) { |
| 25 | + return 0; |
| 26 | + } |
67 | 27 | } |
68 | | -} |
69 | 28 |
|
70 | | -void VideoSocket::quitNotify() |
71 | | -{ |
72 | | - m_quit = true; |
73 | | - QMutexLocker locker(&m_mutex); |
74 | | - if (m_buffer) { |
75 | | - m_buffer = Q_NULLPTR; |
76 | | - m_bufferSize = 0; |
77 | | - m_recvData = true; |
78 | | - m_dataSize = 0; |
79 | | - m_recvDataCond.wakeOne(); |
80 | | - } |
| 29 | + // recv data |
| 30 | + return read((char *)buf, bufSize); |
81 | 31 | } |
0 commit comments