From 32cebd45aaa6e1d944872be235f55ee3d99ff6ee Mon Sep 17 00:00:00 2001 From: Barry <870709864@qq.com> Date: Wed, 6 Apr 2022 20:34:38 +0800 Subject: [PATCH] refactor: controller and socket decoupling --- QtScrcpy/device/controller/controller.cpp | 26 +++++++++------- QtScrcpy/device/controller/controller.h | 8 +++-- .../device/controller/receiver/receiver.cpp | 30 +----------------- .../device/controller/receiver/receiver.h | 12 +------ QtScrcpy/device/device.cpp | 31 ++++++++++++++++--- 5 files changed, 47 insertions(+), 60 deletions(-) diff --git a/QtScrcpy/device/controller/controller.cpp b/QtScrcpy/device/controller/controller.cpp index b0faa82..014dc3e 100644 --- a/QtScrcpy/device/controller/controller.cpp +++ b/QtScrcpy/device/controller/controller.cpp @@ -7,7 +7,9 @@ #include "receiver.h" #include "videosocket.h" -Controller::Controller(QString gameScript, QObject *parent) : QObject(parent) +Controller::Controller(std::function sendData, QString gameScript, QObject *parent) + : QObject(parent) + , m_sendData(sendData) { m_receiver = new Receiver(this); Q_ASSERT(m_receiver); @@ -17,15 +19,6 @@ Controller::Controller(QString gameScript, QObject *parent) : QObject(parent) Controller::~Controller() {} -void Controller::setControlSocket(QTcpSocket *controlSocket) -{ - if (m_controlSocket || !controlSocket) { - return; - } - m_controlSocket = controlSocket; - m_receiver->setControlSocket(controlSocket); -} - void Controller::postControlMsg(ControlMsg *controlMsg) { if (controlMsg) { @@ -33,6 +26,15 @@ void Controller::postControlMsg(ControlMsg *controlMsg) } } +void Controller::recvDeviceMsg(DeviceMsg *deviceMsg) +{ + if (!m_receiver) { + return; + } + + m_receiver->recvDeviceMsg(deviceMsg); +} + void Controller::test(QRect rc) { ControlMsg *controlMsg = new ControlMsg(ControlMsg::CMT_INJECT_TOUCH); @@ -236,8 +238,8 @@ bool Controller::sendControl(const QByteArray &buffer) return false; } qint32 len = 0; - if (m_controlSocket) { - len = static_cast(m_controlSocket->write(buffer.data(), buffer.length())); + if (m_sendData) { + len = static_cast(m_sendData(buffer)); } return len == buffer.length() ? true : false; } diff --git a/QtScrcpy/device/controller/controller.h b/QtScrcpy/device/controller/controller.h index 0b2d886..1b50f73 100644 --- a/QtScrcpy/device/controller/controller.h +++ b/QtScrcpy/device/controller/controller.h @@ -1,3 +1,4 @@ + #ifndef CONTROLLER_H #define CONTROLLER_H @@ -9,15 +10,16 @@ class QTcpSocket; class Receiver; class InputConvertBase; +class DeviceMsg; class Controller : public QObject { Q_OBJECT public: - Controller(QString gameScript = "", QObject *parent = Q_NULLPTR); + Controller(std::function sendData, QString gameScript = "", QObject *parent = Q_NULLPTR); virtual ~Controller(); - void setControlSocket(QTcpSocket *controlSocket); void postControlMsg(ControlMsg *controlMsg); + void recvDeviceMsg(DeviceMsg *deviceMsg); void test(QRect rc); void updateScript(QString gameScript = ""); @@ -62,9 +64,9 @@ private: void postKeyCodeClick(AndroidKeycode keycode); private: - QPointer m_controlSocket; QPointer m_receiver; QPointer m_inputConvert; + std::function m_sendData = Q_NULLPTR; }; #endif // CONTROLLER_H diff --git a/QtScrcpy/device/controller/receiver/receiver.cpp b/QtScrcpy/device/controller/receiver/receiver.cpp index 32d819d..12f5b95 100644 --- a/QtScrcpy/device/controller/receiver/receiver.cpp +++ b/QtScrcpy/device/controller/receiver/receiver.cpp @@ -1,6 +1,5 @@ #include #include -#include #include "devicemsg.h" #include "receiver.h" @@ -9,34 +8,7 @@ Receiver::Receiver(QObject *parent) : QObject(parent) {} Receiver::~Receiver() {} -void Receiver::setControlSocket(QTcpSocket *controlSocket) -{ - if (m_controlSocket || !controlSocket) { - return; - } - m_controlSocket = controlSocket; - connect(controlSocket, &QTcpSocket::readyRead, this, &Receiver::onReadyRead); -} - -void Receiver::onReadyRead() -{ - if (!m_controlSocket) { - return; - } - - while (m_controlSocket->bytesAvailable()) { - QByteArray byteArray = m_controlSocket->peek(m_controlSocket->bytesAvailable()); - DeviceMsg deviceMsg; - qint32 consume = deviceMsg.deserialize(byteArray); - if (0 >= consume) { - break; - } - m_controlSocket->read(consume); - processMsg(&deviceMsg); - } -} - -void Receiver::processMsg(DeviceMsg *deviceMsg) +void Receiver::recvDeviceMsg(DeviceMsg *deviceMsg) { switch (deviceMsg->type()) { case DeviceMsg::DMT_GET_CLIPBOARD: { diff --git a/QtScrcpy/device/controller/receiver/receiver.h b/QtScrcpy/device/controller/receiver/receiver.h index 13266ed..a11d4df 100644 --- a/QtScrcpy/device/controller/receiver/receiver.h +++ b/QtScrcpy/device/controller/receiver/receiver.h @@ -3,7 +3,6 @@ #include -class QTcpSocket; class DeviceMsg; class Receiver : public QObject { @@ -12,16 +11,7 @@ public: explicit Receiver(QObject *parent = Q_NULLPTR); virtual ~Receiver(); - void setControlSocket(QTcpSocket *controlSocket); - -public slots: - void onReadyRead(); - -protected: - void processMsg(DeviceMsg *deviceMsg); - -private: - QPointer m_controlSocket; + void recvDeviceMsg(DeviceMsg *deviceMsg); }; #endif // RECEIVER_H diff --git a/QtScrcpy/device/device.cpp b/QtScrcpy/device/device.cpp index 664ea5d..fabe6a2 100644 --- a/QtScrcpy/device/device.cpp +++ b/QtScrcpy/device/device.cpp @@ -5,6 +5,7 @@ #include "avframeconvert.h" #include "config.h" #include "controller.h" +#include "devicemsg.h" #include "decoder.h" #include "device.h" #include "filehandler.h" @@ -32,7 +33,13 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params m_vb->init(params.renderExpiredFrames); m_decoder = new Decoder(m_vb, this); m_fileHandler = new FileHandler(this); - m_controller = new Controller(params.gameScript, this); + m_controller = new Controller([this](const QByteArray& buffer) -> qint64 { + if (!m_server || !m_server->getControlSocket()) { + return 0; + } + + return m_server->getControlSocket()->write(buffer.data(), buffer.length()); + }, params.gameScript, this); m_videoForm = new VideoForm(params.framelessWindow, Config::getInstance().getSkin()); m_videoForm->setDevice(this); } @@ -273,10 +280,24 @@ void Device::initSignals() // init decoder m_stream->startDecode(); - // init controller - if (m_controller) { - m_controller->setControlSocket(m_server->getControlSocket()); - } + // recv device msg + connect(m_server->getControlSocket(), &QTcpSocket::readyRead, this, [this](){ + if (!m_controller) { + return; + } + + auto controlSocket = m_server->getControlSocket(); + while (controlSocket->bytesAvailable()) { + QByteArray byteArray = controlSocket->peek(controlSocket->bytesAvailable()); + DeviceMsg deviceMsg; + qint32 consume = deviceMsg.deserialize(byteArray); + if (0 >= consume) { + break; + } + controlSocket->read(consume); + m_controller->recvDeviceMsg(&deviceMsg); + } + }); // 显示界面时才自动息屏(m_params.display) if (m_params.closeScreen && m_params.display && m_controller) {