refactor: controller and socket decoupling

This commit is contained in:
Barry 2022-04-06 20:34:38 +08:00
commit 32cebd45aa
5 changed files with 47 additions and 60 deletions

View file

@ -7,7 +7,9 @@
#include "receiver.h" #include "receiver.h"
#include "videosocket.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); m_receiver = new Receiver(this);
Q_ASSERT(m_receiver); Q_ASSERT(m_receiver);
@ -17,15 +19,6 @@ Controller::Controller(QString gameScript, QObject *parent) : QObject(parent)
Controller::~Controller() {} Controller::~Controller() {}
void Controller::setControlSocket(QTcpSocket *controlSocket)
{
if (m_controlSocket || !controlSocket) {
return;
}
m_controlSocket = controlSocket;
m_receiver->setControlSocket(controlSocket);
}
void Controller::postControlMsg(ControlMsg *controlMsg) void Controller::postControlMsg(ControlMsg *controlMsg)
{ {
if (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) void Controller::test(QRect rc)
{ {
ControlMsg *controlMsg = new ControlMsg(ControlMsg::CMT_INJECT_TOUCH); ControlMsg *controlMsg = new ControlMsg(ControlMsg::CMT_INJECT_TOUCH);
@ -236,8 +238,8 @@ bool Controller::sendControl(const QByteArray &buffer)
return false; return false;
} }
qint32 len = 0; qint32 len = 0;
if (m_controlSocket) { if (m_sendData) {
len = static_cast<qint32>(m_controlSocket->write(buffer.data(), buffer.length())); len = static_cast<qint32>(m_sendData(buffer));
} }
return len == buffer.length() ? true : false; return len == buffer.length() ? true : false;
} }

View file

@ -1,3 +1,4 @@
#ifndef CONTROLLER_H #ifndef CONTROLLER_H
#define CONTROLLER_H #define CONTROLLER_H
@ -9,15 +10,16 @@
class QTcpSocket; class QTcpSocket;
class Receiver; class Receiver;
class InputConvertBase; class InputConvertBase;
class DeviceMsg;
class Controller : public QObject class Controller : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
Controller(QString gameScript = "", QObject *parent = Q_NULLPTR); Controller(std::function<qint64(const QByteArray&)> sendData, QString gameScript = "", QObject *parent = Q_NULLPTR);
virtual ~Controller(); virtual ~Controller();
void setControlSocket(QTcpSocket *controlSocket);
void postControlMsg(ControlMsg *controlMsg); void postControlMsg(ControlMsg *controlMsg);
void recvDeviceMsg(DeviceMsg *deviceMsg);
void test(QRect rc); void test(QRect rc);
void updateScript(QString gameScript = ""); void updateScript(QString gameScript = "");
@ -62,9 +64,9 @@ private:
void postKeyCodeClick(AndroidKeycode keycode); void postKeyCodeClick(AndroidKeycode keycode);
private: private:
QPointer<QTcpSocket> m_controlSocket;
QPointer<Receiver> m_receiver; QPointer<Receiver> m_receiver;
QPointer<InputConvertBase> m_inputConvert; QPointer<InputConvertBase> m_inputConvert;
std::function<qint64(const QByteArray&)> m_sendData = Q_NULLPTR;
}; };
#endif // CONTROLLER_H #endif // CONTROLLER_H

View file

@ -1,6 +1,5 @@
#include <QApplication> #include <QApplication>
#include <QClipboard> #include <QClipboard>
#include <QTcpSocket>
#include "devicemsg.h" #include "devicemsg.h"
#include "receiver.h" #include "receiver.h"
@ -9,34 +8,7 @@ Receiver::Receiver(QObject *parent) : QObject(parent) {}
Receiver::~Receiver() {} Receiver::~Receiver() {}
void Receiver::setControlSocket(QTcpSocket *controlSocket) void Receiver::recvDeviceMsg(DeviceMsg *deviceMsg)
{
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)
{ {
switch (deviceMsg->type()) { switch (deviceMsg->type()) {
case DeviceMsg::DMT_GET_CLIPBOARD: { case DeviceMsg::DMT_GET_CLIPBOARD: {

View file

@ -3,7 +3,6 @@
#include <QPointer> #include <QPointer>
class QTcpSocket;
class DeviceMsg; class DeviceMsg;
class Receiver : public QObject class Receiver : public QObject
{ {
@ -12,16 +11,7 @@ public:
explicit Receiver(QObject *parent = Q_NULLPTR); explicit Receiver(QObject *parent = Q_NULLPTR);
virtual ~Receiver(); virtual ~Receiver();
void setControlSocket(QTcpSocket *controlSocket); void recvDeviceMsg(DeviceMsg *deviceMsg);
public slots:
void onReadyRead();
protected:
void processMsg(DeviceMsg *deviceMsg);
private:
QPointer<QTcpSocket> m_controlSocket;
}; };
#endif // RECEIVER_H #endif // RECEIVER_H

View file

@ -5,6 +5,7 @@
#include "avframeconvert.h" #include "avframeconvert.h"
#include "config.h" #include "config.h"
#include "controller.h" #include "controller.h"
#include "devicemsg.h"
#include "decoder.h" #include "decoder.h"
#include "device.h" #include "device.h"
#include "filehandler.h" #include "filehandler.h"
@ -32,7 +33,13 @@ Device::Device(DeviceParams params, QObject *parent) : QObject(parent), m_params
m_vb->init(params.renderExpiredFrames); m_vb->init(params.renderExpiredFrames);
m_decoder = new Decoder(m_vb, this); m_decoder = new Decoder(m_vb, this);
m_fileHandler = new FileHandler(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 = new VideoForm(params.framelessWindow, Config::getInstance().getSkin());
m_videoForm->setDevice(this); m_videoForm->setDevice(this);
} }
@ -273,11 +280,25 @@ void Device::initSignals()
// init decoder // init decoder
m_stream->startDecode(); m_stream->startDecode();
// init controller // recv device msg
if (m_controller) { connect(m_server->getControlSocket(), &QTcpSocket::readyRead, this, [this](){
m_controller->setControlSocket(m_server->getControlSocket()); 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 // 显示界面时才自动息屏m_params.display
if (m_params.closeScreen && m_params.display && m_controller) { if (m_params.closeScreen && m_params.display && m_controller) {
emit m_controller->onSetScreenPowerMode(ControlMsg::SPM_OFF); emit m_controller->onSetScreenPowerMode(ControlMsg::SPM_OFF);