From c6613426247b9fc9cb644b873bd9cd600e2ce45b Mon Sep 17 00:00:00 2001 From: rankun Date: Wed, 10 Oct 2018 18:20:30 +0800 Subject: [PATCH] connect to --- src/QtScrcpy.pro.user | 14 ++++++------ src/server.cpp | 51 ++++++++++++++++++++++++++++++++++++++----- src/server.h | 4 +++- 3 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/QtScrcpy.pro.user b/src/QtScrcpy.pro.user index 0b25f99..24ede2b 100644 --- a/src/QtScrcpy.pro.user +++ b/src/QtScrcpy.pro.user @@ -1,10 +1,10 @@ - + EnvironmentId - {49c3991c-71bb-4f46-826d-1d1cf5cba2d4} + {37b22fe7-3e02-43d3-85e3-251a3ebe4093} ProjectExplorer.Project.ActiveTarget @@ -66,7 +66,7 @@ 0 0 - G:/QT/code/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Debug + G:/mygitcode/QtScrcpy/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Debug true @@ -120,7 +120,7 @@ true - G:/QT/code/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Release + G:/mygitcode/QtScrcpy/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Release true @@ -174,7 +174,7 @@ true - G:/QT/code/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Profile + G:/mygitcode/QtScrcpy/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Profile true @@ -285,14 +285,14 @@ 2 QtScrcpy - QtScrcpy2 + Qt4ProjectManager.Qt4RunConfiguration:G:/mygitcode/QtScrcpy/src/QtScrcpy.pro true QtScrcpy.pro false - G:/QT/code/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Debug + G:/mygitcode/QtScrcpy/build-QtScrcpy-Desktop_Qt_5_9_6_MSVC2015_32bit-Debug 3768 false true diff --git a/src/server.cpp b/src/server.cpp index 7e55e64..9e002db 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1,4 +1,5 @@ #include +#include #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; diff --git a/src/server.h b/src/server.h index 30338a2..7bb3df5 100644 --- a/src/server.h +++ b/src/server.h @@ -4,6 +4,7 @@ #include #include #include +#include #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 m_deviceSocket = Q_NULLPTR; quint16 m_localPort = 0; bool m_tunnelEnabled = false; bool m_tunnelForward = false; // use "adb forward" instead of "adb reverse"