connect to

This commit is contained in:
rankun 2018-10-10 18:20:30 +08:00
parent 74a5d8712f
commit c661342624
3 changed files with 56 additions and 13 deletions

View file

@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.6.2, 2018-10-10T09:20:36. -->
<!-- Written by QtCreator 4.6.2, 2018-10-10T18:19:32. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
<value type="QByteArray">{49c3991c-71bb-4f46-826d-1d1cf5cba2d4}</value>
<value type="QByteArray">{37b22fe7-3e02-43d3-85e3-251a3ebe4093}</value>
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
@ -66,7 +66,7 @@
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">G:/QT/code/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Debug</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">G:/mygitcode/QtScrcpy/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Debug</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@ -120,7 +120,7 @@
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.1">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">G:/QT/code/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Release</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">G:/mygitcode/QtScrcpy/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Release</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@ -174,7 +174,7 @@
<value type="bool" key="Qt4ProjectManager.Qt4BuildConfiguration.UseShadowBuild">true</value>
</valuemap>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.2">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">G:/QT/code/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Profile</value>
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">G:/mygitcode/QtScrcpy/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Profile</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildStepList.Step.0">
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
@ -285,14 +285,14 @@
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">QtScrcpy</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">QtScrcpy2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></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="QString" key="Qt4ProjectManager.Qt4RunConfiguration.CommandLineArguments"></value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">QtScrcpy.pro</value>
<value type="bool" key="Qt4ProjectManager.Qt4RunConfiguration.UseDyldImageSuffix">false</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory"></value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default">G:/QT/code/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Debug</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.UserWorkingDirectory.default">G:/mygitcode/QtScrcpy/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Debug</value>
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>

View file

@ -1,4 +1,5 @@
#include <QDebug>
#include <QTimer>
#include "server.h"
@ -8,6 +9,13 @@
Server::Server(QObject *parent) : QObject(parent)
{
connect(&m_workProcess, &AdbProcess::adbProcessResult, this, &Server::onWorkProcessResult);
connect(&m_serverSocket, &QTcpServer::newConnection, this, [this](){
m_deviceSocket = m_serverSocket.nextPendingConnection();
connect(m_deviceSocket, &QTcpSocket::disconnected, m_deviceSocket, &QTcpSocket::deleteLater);
connect(m_deviceSocket, &QTcpSocket::error, m_deviceSocket, &QTcpSocket::deleteLater);
});
}
const QString& Server::getServerPath()
@ -131,10 +139,41 @@ bool Server::start(const QString& serial, quint16 localPort, quint16 maxSize, qu
void Server::connectTo()
{
if (!m_tunnelForward) {
//m_deviceSocket
if (m_tunnelForward) {
m_deviceSocket = new QTcpSocket(this);
connect(m_deviceSocket, &QTcpSocket::disconnected, m_deviceSocket, &QTcpSocket::deleteLater);
connect(m_deviceSocket, &QTcpSocket::error, m_deviceSocket, &QTcpSocket::deleteLater);
m_deviceSocket->connectToHost(QHostAddress::LocalHost, m_localPort);
}
QTimer::singleShot(300, this, [this](){
if (!m_deviceSocket) {
emit connectToResult(false);
return;
}
bool success = false;
if (m_tunnelForward) {
if (m_deviceSocket->isValid()) {
if (m_deviceSocket->read(1)) {
success = true;
} else {
success = false;
}
} else {
m_deviceSocket->deleteLater();
success = false;
}
} else {
if (m_deviceSocket->isValid()) {
success = true;
} else {
m_deviceSocket->deleteLater();
success = false;
}
}
emit connectToResult(success);
});
}
void Server::stop()
@ -151,8 +190,10 @@ void Server::stop()
if (m_serverCopiedToDevice) {
removeServer();
}
m_serverSocket.close();
m_deviceSocket.close();
m_serverSocket.disconnect();
if (m_deviceSocket) {
m_deviceSocket->disconnectFromHost();
}
}
bool Server::startServerByStep()
@ -178,7 +219,7 @@ bool Server::startServerByStep()
// client listens and the server connects to the client. That way, the
// client can listen before starting the server app, so there is no need to
// try to connect until the server socket is listening on the device.
m_serverSocket.setMaxPendingConnections(1);
m_serverSocket.setMaxPendingConnections(1);
if (!m_serverSocket.listen(QHostAddress::LocalHost, m_localPort)) {
qCritical(QString("Could not listen on port %1").arg(m_localPort).toStdString().c_str());
m_serverStartStep = SSS_NULL;

View file

@ -4,6 +4,7 @@
#include <QObject>
#include <QTcpSocket>
#include <QTcpServer>
#include <QPointer>
#include "adbprocess.h"
@ -27,6 +28,7 @@ public:
signals:
void serverStartResult(bool success);
void connectToResult(bool success);
private slots:
void onWorkProcessResult(AdbProcess::ADB_EXEC_RESULT processResult);
@ -47,7 +49,7 @@ private:
QString m_serial = "";
AdbProcess m_serverProcess;
QTcpServer m_serverSocket; // only used if !tunnel_forward
QTcpSocket m_deviceSocket;
QPointer<QTcpSocket> m_deviceSocket = Q_NULLPTR;
quint16 m_localPort = 0;
bool m_tunnelEnabled = false;
bool m_tunnelForward = false; // use "adb forward" instead of "adb reverse"