mirror of
https://github.com/barry-ran/QtScrcpy.git
synced 2025-04-20 19:44:59 +00:00
refactor: controller and socket decoupling
This commit is contained in:
parent
d68d96b4ee
commit
32cebd45aa
5 changed files with 47 additions and 60 deletions
|
@ -7,7 +7,9 @@
|
|||
#include "receiver.h"
|
||||
#include "videosocket.h"
|
||||
|
||||
Controller::Controller(QString gameScript, QObject *parent) : QObject(parent)
|
||||
Controller::Controller(std::function<qint64(const QByteArray&)> 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<qint32>(m_controlSocket->write(buffer.data(), buffer.length()));
|
||||
if (m_sendData) {
|
||||
len = static_cast<qint32>(m_sendData(buffer));
|
||||
}
|
||||
return len == buffer.length() ? true : false;
|
||||
}
|
||||
|
|
|
@ -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<qint64(const QByteArray&)> 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<QTcpSocket> m_controlSocket;
|
||||
QPointer<Receiver> m_receiver;
|
||||
QPointer<InputConvertBase> m_inputConvert;
|
||||
std::function<qint64(const QByteArray&)> m_sendData = Q_NULLPTR;
|
||||
};
|
||||
|
||||
#endif // CONTROLLER_H
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#include <QApplication>
|
||||
#include <QClipboard>
|
||||
#include <QTcpSocket>
|
||||
|
||||
#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: {
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
#include <QPointer>
|
||||
|
||||
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<QTcpSocket> m_controlSocket;
|
||||
void recvDeviceMsg(DeviceMsg *deviceMsg);
|
||||
};
|
||||
|
||||
#endif // RECEIVER_H
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue