Skip to content

Commit 8a73d49

Browse files
huayuanroadqufangtao
andauthored
fix: 修复手机端调试器切换应用、卡片模式后调试失败的问题 (#151)
Signed-off-by: qufangtao <[email protected]> Co-authored-by: qufangtao <[email protected]>
1 parent 337fcd2 commit 8a73d49

File tree

3 files changed

+56
-14
lines changed

3 files changed

+56
-14
lines changed

packages/hap-debugger/src/adb/module.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,50 @@ class ADBModule {
162162
this.currentDeviceMap.set(sn, currentDevice)
163163
this.cachedDeviceMap.set(sn, currentDevice)
164164
// 记录发送update http请求需要的ip和端口
165+
const remote2local = {}
166+
remote2local[currentDevice.upForwardPortPair[1]] = currentDevice.upForwardPortPair[0]
165167
await this._writeClientLogFile({
166168
sn,
167169
ip: `127.0.0.1`,
168-
port: currentDevice.upForwardPortPair[0]
170+
port: currentDevice.upForwardPortPair[0],
171+
remote2local
169172
})
170173
debuglog(`onDeviceAdded():(${sn}) end`)
171174
// 增加设备连接检测
172175
this.onCheckDeviceReverse(event)
173176
}
174177

178+
async getForwardPort(client, remotePort) {
179+
let remote2local = client.remote2local
180+
if (!remote2local) return client.port
181+
182+
let port = remote2local[remotePort]
183+
if (!port) {
184+
// addForwardPort
185+
port = await this.addForwardPort(client, remotePort)
186+
}
187+
return port
188+
}
189+
190+
async addForwardPort(client, remotePort) {
191+
const port = this._getNextLocalForwardPort()
192+
debuglog(`addForwardPort():(${client.sn}) (local port: ${port}, remote port: ${remotePort}) start`)
193+
const upForwardResult = await this.establishADBProxyLink(
194+
'forward',
195+
[client.sn].concat([port, remotePort])
196+
)
197+
if (upForwardResult.err) {
198+
colorconsole.error(
199+
`### App Server ### addForwardPort(): (${client.sn})建立adb forward失败(local port: ${port}, remote port: ${remotePort}) `
200+
)
201+
return
202+
}
203+
client.remote2local[remotePort] = port
204+
await this._writeClientLogFile(client)
205+
debuglog(`addForwardPort():(${client.sn}) (local port: ${port}, remote port: ${remotePort}) end`)
206+
return port
207+
}
208+
175209
/**
176210
* 设备连接检测,用于检查当前窗口端口是否连接手机调试器
177211
*/

packages/hap-debugger/src/router/routes.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,25 @@ export async function searchSn(context, next) {
116116
const recordData = getRecords(clientRecordPath)
117117
const clients = getProjectClients(recordData)
118118
if (clients.length > 0) {
119+
const remotePort = context.request.query.port
119120
colorconsole.log('### App Loader ### 通知设备开始下发SN')
120-
clients.forEach(function (client) {
121+
for (const client of clients) {
121122
// 匹配hap-toolkit-client-records.json表里已存的通过ADB连接的设备信息
122123
if (client.ip === '127.0.0.1') {
123-
// 仅向ADB现在连接的且没有SN的设备下发SN请求
124-
getDeviceInfo(client, (err, data) => {
125-
if (err || data.sn) {
126-
return
127-
}
128-
callDeviceWithOwnSn(client)
129-
})
124+
if (remotePort) {
125+
const port = await context.adbDebugger.getForwardPort(client, remotePort)
126+
callDeviceWithOwnSn(client, port)
127+
} else {
128+
// 仅向ADB现在连接的且没有SN的设备下发SN请求
129+
getDeviceInfo(client, (err, data) => {
130+
if (err || data.sn) {
131+
return
132+
}
133+
callDeviceWithOwnSn(client)
134+
})
135+
}
130136
}
131-
})
137+
}
132138
}
133139
} else {
134140
await next()
@@ -154,6 +160,7 @@ export async function startDebug(context, next) {
154160
// ADB调试模式
155161
if (linkMode === LINK_MODE.ADB) {
156162
const { localWsPort, err } = await context.adbDebugger.forwardForWsChannel(sn, devicePort)
163+
console.error(`startDebug(): adb forward 端口映射: sn=${sn},devicePort=${devicePort}`)
157164
if (err) {
158165
console.error(`startDebug(): adb forward 端口映射失败: ${err.message}`)
159166
trackDebug(eventAlias.h_forward_err)

packages/hap-debugger/src/router/service.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ export function getDebugInfoFromRequest(request) {
6666
* 迫于device目前不支持POST,暂时把sn放在header中
6767
* @param client 本地存储的设备信息(ip、port、sn)
6868
*/
69-
export function callDeviceWithOwnSn(client) {
69+
export function callDeviceWithOwnSn(client, port) {
70+
if (!port) port = client.port
7071
const options = {
7172
host: client.ip,
72-
port: client.port,
73+
port,
7374
path: '/reportsn',
7475
headers: {
7576
'device-serial-number': client.sn
@@ -78,11 +79,11 @@ export function callDeviceWithOwnSn(client) {
7879
}
7980
const req = http
8081
.request(options, () => {
81-
colorconsole.log(`### App Server ### 通知手机设备(${client.sn})下发SN成功`)
82+
colorconsole.log(`### App Server ### 通知手机设备(${client.sn}) port ${port} 下发SN成功`)
8283
})
8384
.on('error', (err) => {
8485
colorconsole.warn(
85-
`### App Server ### 通知手机设备(${client.sn})下发SN失败 错误信息: ${err.message}`
86+
`### App Server ### 通知手机设备(${client.sn}) port ${port} 下发SN失败 错误信息: ${err.message}`
8687
)
8788
})
8889
.on('timeout', function () {

0 commit comments

Comments
 (0)