server 功能更新

This commit is contained in:
Barry 2018-10-10 09:21:40 +08:00
commit 74a5d8712f
4 changed files with 85 additions and 40 deletions

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.6.2, 2018-10-09T23:46:57. --> <!-- Written by QtCreator 4.6.2, 2018-10-10T09:20:36. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
@ -285,8 +285,8 @@
<value type="int" key="PE.EnvironmentAspect.Base">2</value> <value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">QtScrcpy</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">QtScrcpy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">QtScrcpy2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:G:/QT/code/QtScrcpy/QtScrcpy.pro</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:G:/mygitcode/QtScrcpy/src/QtScrcpy.pro</value>
<value type="bool" key="QmakeProjectManager.QmakeRunConfiguration.UseLibrarySearchPath">true</value> <value type="bool" key="QmakeProjectManager.QmakeRunConfiguration.UseLibrarySearchPath">true</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value> <value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">QtScrcpy.pro</value> <value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">QtScrcpy.pro</value>

View file

@ -2,7 +2,8 @@
#include <QApplication> #include <QApplication>
#include <QDebug> #include <QDebug>
#include <QTcpSocket>
#include <QTcpServer>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {

View file

@ -32,10 +32,14 @@ bool Server::pushServer()
bool Server::removeServer() bool Server::removeServer()
{ {
if (m_workProcess.isRuning()) { AdbProcess* adb = new AdbProcess();
m_workProcess.kill(); if (!adb) {
return false;
} }
m_workProcess.removePath(m_serial, DEVICE_SERVER_PATH); connect(adb, &AdbProcess::adbProcessResult, this, [this](AdbProcess::ADB_EXEC_RESULT processResult){
sender()->deleteLater();
});
adb->removePath(m_serial, DEVICE_SERVER_PATH);
return true; return true;
} }
@ -50,10 +54,14 @@ bool Server::enableTunnelReverse()
bool Server::disableTunnelReverse() bool Server::disableTunnelReverse()
{ {
if (m_workProcess.isRuning()) { AdbProcess* adb = new AdbProcess();
m_workProcess.kill(); if (!adb) {
return false;
} }
m_workProcess.reverseRemove(m_serial, SOCKET_NAME); connect(adb, &AdbProcess::adbProcessResult, this, [this](AdbProcess::ADB_EXEC_RESULT processResult){
sender()->deleteLater();
});
adb->reverseRemove(m_serial, SOCKET_NAME);
return true; return true;
} }
@ -67,18 +75,21 @@ bool Server::enableTunnelForward()
} }
bool Server::disableTunnelForward() bool Server::disableTunnelForward()
{ {
if (m_workProcess.isRuning()) { AdbProcess* adb = new AdbProcess();
m_workProcess.kill(); if (!adb) {
return false;
} }
m_workProcess.forwardRemove(m_serial, m_localPort); connect(adb, &AdbProcess::adbProcessResult, this, [this](AdbProcess::ADB_EXEC_RESULT processResult){
sender()->deleteLater();
});
adb->forwardRemove(m_serial, m_localPort);
return true; return true;
} }
bool Server::execute() bool Server::execute()
{ {
AdbProcess* adb = new AdbProcess(); if (m_workProcess.isRuning()) {
if (!adb) { m_workProcess.kill();
return false;
} }
QStringList args; QStringList args;
args << "shell"; args << "shell";
@ -89,15 +100,17 @@ bool Server::execute()
args << QString::number(m_maxSize); args << QString::number(m_maxSize);
args << QString::number(m_bitRate); args << QString::number(m_bitRate);
args << (m_tunnelForward ? "true" : "false"); args << (m_tunnelForward ? "true" : "false");
args << (m_crop.isEmpty() ? "" : m_crop); if (!m_crop.isEmpty()) {
args << m_crop;
connect(adb, &AdbProcess::adbProcessResult, this, [this](AdbProcess::ADB_EXEC_RESULT processResult){
if (AdbProcess::AER_SUCCESS == processResult) {
} }
sender()->deleteLater();
}); // connect(adb, &AdbProcess::adbProcessResult, this, [this](AdbProcess::ADB_EXEC_RESULT processResult){
adb->execute(m_serial, args); // if (AdbProcess::AER_SUCCESS == processResult) {
// }
// sender()->deleteLater();
// });
m_workProcess.execute(m_serial, args);
return true; return true;
} }
@ -116,23 +129,50 @@ bool Server::start(const QString& serial, quint16 localPort, quint16 maxSize, qu
return startServerByStep(); return startServerByStep();
} }
void Server::connectTo()
{
if (!m_tunnelForward) {
//m_deviceSocket
}
}
void Server::stop()
{
// ignore failure
m_workProcess.terminate();
if (m_tunnelEnabled) {
if (m_tunnelForward) {
disableTunnelForward();
} else {
disableTunnelReverse();
}
}
if (m_serverCopiedToDevice) {
removeServer();
}
m_serverSocket.close();
m_deviceSocket.close();
}
bool Server::startServerByStep() bool Server::startServerByStep()
{ {
bool stepSuccess = false;
// push, enable tunnel et start the server // push, enable tunnel et start the server
if (SSS_NULL != m_serverStartStep) { if (SSS_NULL != m_serverStartStep) {
switch (m_serverStartStep) { switch (m_serverStartStep) {
case SSS_PUSH: case SSS_PUSH:
return pushServer(); stepSuccess = pushServer();
case SSS_ENABLE_TUNNEL_REVERSE: case SSS_ENABLE_TUNNEL_REVERSE:
return enableTunnelReverse(); stepSuccess = enableTunnelReverse();
break; break;
case SSS_ENABLE_TUNNEL_FORWARD: case SSS_ENABLE_TUNNEL_FORWARD:
return enableTunnelForward(); stepSuccess = enableTunnelForward();
break; break;
case SSS_EXECUTE_SERVER: case SSS_EXECUTE_SERVER:
// if "adb reverse" does not work (e.g. over "adb connect"), it fallbacks to // if "adb reverse" does not work (e.g. over "adb connect"), it fallbacks to
// "adb forward", so the app socket is the client // "adb forward", so the app socket is the client
if (m_tunnelForward) { if (!m_tunnelForward) {
// At the application level, the device part is "the server" because it // At the application level, the device part is "the server" because it
// serves video stream and control. However, at the network level, the // serves video stream and control. However, at the network level, the
// client listens and the server connects to the client. That way, the // client listens and the server connects to the client. That way, the
@ -152,13 +192,17 @@ bool Server::startServerByStep()
} }
} }
// server will connect to our server socket // server will connect to our server socket
return execute(); stepSuccess = execute();
break; break;
default: default:
break; break;
} }
} }
return true;
if (!stepSuccess) {
emit serverStartResult(false);
}
return stepSuccess;
} }
void Server::onWorkProcessResult(AdbProcess::ADB_EXEC_RESULT processResult) void Server::onWorkProcessResult(AdbProcess::ADB_EXEC_RESULT processResult)

View file

@ -22,24 +22,24 @@ public:
explicit Server(QObject *parent = nullptr); explicit Server(QObject *parent = nullptr);
bool start(const QString& serial, quint16 localPort, quint16 maxSize, quint32 bitRate, const QString& crop); bool start(const QString& serial, quint16 localPort, quint16 maxSize, quint32 bitRate, const QString& crop);
void connectTo();
void stop();
signals:
void serverStartResult(bool success);
private slots:
void onWorkProcessResult(AdbProcess::ADB_EXEC_RESULT processResult);
private:
const QString& getServerPath();
bool pushServer(); bool pushServer();
bool removeServer(); bool removeServer();
bool enableTunnelReverse(); bool enableTunnelReverse();
bool disableTunnelReverse(); bool disableTunnelReverse();
bool enableTunnelForward(); bool enableTunnelForward();
bool disableTunnelForward(); bool disableTunnelForward();
bool execute(); bool execute();
signals:
void serverStartResult(bool success);
public slots:
void onWorkProcessResult(AdbProcess::ADB_EXEC_RESULT processResult);
private:
const QString& getServerPath();
bool startServerByStep(); bool startServerByStep();
QString m_serverPath = ""; QString m_serverPath = "";