Skip to content

Commit 0be61c8

Browse files
Merge pull request #2215 from NativeScript/KristinaKoeva/ChromeDebugging
Some refactorings. socket-proxy-factory is a Factory so move out the stop logic out of it
2 parents d1c2d4e + 174a231 commit 0be61c8

File tree

4 files changed

+48
-69
lines changed

4 files changed

+48
-69
lines changed

lib/declarations.ts

-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ interface IAndroidToolsInfoData {
234234

235235
interface ISocketProxyFactory {
236236
createSocketProxy(factory: () => any): IFuture<any>;
237-
stopServer(): void;
238237
}
239238

240239
interface IiOSNotification {

lib/device-sockets/ios/socket-proxy-factory.ts

+13-25
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ export class SocketProxyFactory implements ISocketProxyFactory {
1010
private $config: IConfiguration,
1111
private $projectData: IProjectData,
1212
private $projectDataService: IProjectDataService,
13-
private $processService: IProcessService,
1413
private $options: IOptions) { }
1514

16-
private _server: any;
17-
private _sockets: net.Socket[] = [];
18-
1915
public createSocketProxy(factory: () => net.Socket): IFuture<any> {
2016
return (() => {
2117
let socketFactory = (callback: (_socket: net.Socket) => void) => helpers.connectEventually(factory, callback);
@@ -87,14 +83,11 @@ export class SocketProxyFactory implements ISocketProxyFactory {
8783

8884
});
8985

90-
this._server = server;
91-
this.$processService.attachToProcessExitSignals(this, this.stopServer);
92-
9386
this.$logger.info("Opened localhost " + localPort);
9487
return server;
9588
}
9689

97-
private createTcpSocketProxy(socketFactory: (handler: (socket: net.Socket) => void) => void): string {
90+
private createTcpSocketProxy(socketFactory: (handler: (socket: net.Socket) => void) => void): net.Server {
9891
this.$logger.info("\nSetting up proxy...\nPress Ctrl + C to terminate, or disconnect.\n");
9992

10093
let server = net.createServer({
@@ -104,8 +97,6 @@ export class SocketProxyFactory implements ISocketProxyFactory {
10497
server.on("connection", (frontendSocket: net.Socket) => {
10598
this.$logger.info("Frontend client connected.");
10699

107-
this._sockets.push(frontendSocket);
108-
109100
frontendSocket.on("end", () => {
110101
this.$logger.info('Frontend socket closed!');
111102
if (!(this.$config.debugLivesync && this.$options.watch)) {
@@ -123,6 +114,17 @@ export class SocketProxyFactory implements ISocketProxyFactory {
123114
}
124115
});
125116

117+
frontendSocket.on("close", () => {
118+
if (!(<any>backendSocket).destroyed) {
119+
backendSocket.destroy();
120+
}
121+
});
122+
backendSocket.on("close", () => {
123+
if (!(<any>frontendSocket).destroyed) {
124+
frontendSocket.destroy();
125+
}
126+
});
127+
126128
backendSocket.pipe(frontendSocket);
127129
frontendSocket.pipe(backendSocket);
128130
frontendSocket.resume();
@@ -135,21 +137,7 @@ export class SocketProxyFactory implements ISocketProxyFactory {
135137
this.$logger.info("socket-file-location: " + socketFileLocation);
136138
}
137139

138-
this._server = server;
139-
this.$processService.attachToProcessExitSignals(this, this.stopServer);
140-
141-
return socketFileLocation;
142-
}
143-
144-
public stopServer() {
145-
if (this._server) {
146-
this._server.close();
147-
for (let socket of this._sockets) {
148-
socket.destroy();
149-
}
150-
this._sockets = [];
151-
this._server = null;
152-
}
140+
return server;
153141
}
154142
}
155143
$injector.register("socketProxyFactory", SocketProxyFactory);

lib/device-sockets/ios/socket-request-executor.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
77
private $iOSNotification: IiOSNotification,
88
private $iOSNotificationService: IiOSNotificationService,
99
private $logger: ILogger,
10-
private $projectData: IProjectData,
11-
private $socketProxyFactory: ISocketProxyFactory) { }
10+
private $projectData: IProjectData) { }
1211

1312
public executeAttachRequest(device: Mobile.IiOSDevice, timeout: number): IFuture<void> {
1413
return (() => {

lib/services/ios-debug-service.ts

+34-41
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ const inspectorBackendPort = 18181;
88
const inspectorAppName = "NativeScript Inspector.app";
99
const inspectorNpmPackageName = "tns-ios-inspector";
1010
const inspectorUiDir = "WebInspectorUI/";
11-
const TIMEOUT_SECONDS = 90;
11+
const TIMEOUT_SECONDS = 9;
1212

1313
class IOSDebugService implements IDebugService {
14+
private _lldbProcess: ChildProcess;
15+
private _sockets: net.Socket[] = [];
16+
private _childProcess: ChildProcess;
17+
private _socketProxy: net.Server;
18+
1419
constructor(
1520
private $config: IConfiguration,
1621
private $platformService: IPlatformService,
@@ -35,10 +40,6 @@ class IOSDebugService implements IDebugService {
3540
this.$processService.attachToProcessExitSignals(this, this.debugStop);
3641
}
3742

38-
private _lldbProcess: ChildProcess;
39-
private _sockets: net.Socket[] = [];
40-
private _childProcess: ChildProcess;
41-
4243
public get platform(): string {
4344
return "ios";
4445
}
@@ -80,16 +81,20 @@ class IOSDebugService implements IDebugService {
8081

8182
public debugStop(): IFuture<void> {
8283
return (() => {
83-
this.$socketProxyFactory.stopServer();
84-
for (let socket of this._sockets) {
85-
socket.destroy();
84+
if (this._socketProxy) {
85+
this._socketProxy.close();
86+
this._socketProxy = null;
8687
}
88+
89+
_.forEach(this._sockets, socket => socket.destroy());
8790
this._sockets = [];
91+
8892
if (this._lldbProcess) {
8993
this._lldbProcess.stdin.write("process detach\n");
9094
this._lldbProcess.kill();
9195
this._lldbProcess = undefined;
9296
}
97+
9398
if (this._childProcess) {
9499
this._childProcess.kill();
95100
this._childProcess = undefined;
@@ -163,9 +168,7 @@ class IOSDebugService implements IDebugService {
163168
private debugBrkCore(device: Mobile.IiOSDevice, shouldBreak?: boolean): IFuture<void> {
164169
return (() => {
165170
let timeout = this.$utils.getMilliSecondsTimeout(TIMEOUT_SECONDS);
166-
let readyForAttachTimeout = this.getReadyForAttachTimeout(timeout);
167-
168-
this.$iOSSocketRequestExecutor.executeLaunchRequest(device, timeout, readyForAttachTimeout, shouldBreak).wait();
171+
this.$iOSSocketRequestExecutor.executeLaunchRequest(device, timeout, timeout, shouldBreak).wait();
169172
this.wireDebuggerClient(device).wait();
170173
}).future<void>()();
171174
}
@@ -179,56 +182,46 @@ class IOSDebugService implements IDebugService {
179182

180183
private deviceStartCore(device: Mobile.IiOSDevice): IFuture<void> {
181184
return (() => {
182-
let timeout = this.getReadyForAttachTimeout();
185+
let timeout = this.$utils.getMilliSecondsTimeout(TIMEOUT_SECONDS);
183186
this.$iOSSocketRequestExecutor.executeAttachRequest(device, timeout).wait();
184187
this.wireDebuggerClient(device).wait();
185188
}).future<void>()();
186189
}
187190

188191
private wireDebuggerClient(device?: Mobile.IiOSDevice): IFuture<void> {
189192
return (() => {
190-
let socketProxy = this.$socketProxyFactory.createSocketProxy(() => {
193+
this._socketProxy = this.$socketProxyFactory.createSocketProxy(() => {
191194
let socket = device ? device.connectToPort(inspectorBackendPort) : net.connect(inspectorBackendPort);
192195
this._sockets.push(socket);
193196
return socket;
194197
}).wait();
195-
this.executeOpenDebuggerClient(socketProxy).wait();
198+
199+
this.openDebuggerClient(<any>this._socketProxy.address()).wait();
196200
}).future<void>()();
197201
}
198202

199-
public executeOpenDebuggerClient(fileDescriptor: string): IFuture<void> {
203+
private openDebuggerClient(fileDescriptor: string): IFuture<void> {
200204
if (this.$options.client) {
201-
return this.openDebuggingClient(fileDescriptor);
205+
return (() => {
206+
let inspectorPath = this.$npmInstallationManager.install(inspectorNpmPackageName).wait();
207+
let inspectorSourceLocation = path.join(inspectorPath, inspectorUiDir, "Main.html");
208+
let inspectorApplicationPath = path.join(inspectorPath, inspectorAppName);
209+
210+
// TODO : Sadly $npmInstallationManager.install does not install the package, it only inserts it in the cache through the npm cache add command
211+
// Since npm cache add command does not execute scripts our posinstall script that extract the Inspector Application does not execute as well
212+
// So until this behavior is changed this ugly workaround should not be deleted
213+
if (!this.$fs.exists(inspectorApplicationPath).wait()) {
214+
this.$npm.executeNpmCommand("npm run-script postinstall", inspectorPath).wait();
215+
}
216+
217+
let cmd = `open -a '${inspectorApplicationPath}' --args '${inspectorSourceLocation}' '${this.$projectData.projectName}' '${fileDescriptor}'`;
218+
this.$childProcess.exec(cmd).wait();
219+
}).future<void>()();
202220
} else {
203221
return (() => {
204222
this.$logger.info("Suppressing debugging client.");
205223
}).future<void>()();
206224
}
207225
}
208-
209-
private openDebuggingClient(fileDescriptor: string): IFuture<void> {
210-
return (() => {
211-
let inspectorPath = this.$npmInstallationManager.install(inspectorNpmPackageName).wait();
212-
let inspectorSourceLocation = path.join(inspectorPath, inspectorUiDir, "Main.html");
213-
let inspectorApplicationPath = path.join(inspectorPath, inspectorAppName);
214-
215-
// TODO : Sadly $npmInstallationManager.install does not install the package, it only inserts it in the cache through the npm cache add command
216-
// Since npm cache add command does not execute scripts our posinstall script that extract the Inspector Application does not execute as well
217-
// So until this behavior is changed this ugly workaround should not be deleted
218-
if (!this.$fs.exists(inspectorApplicationPath).wait()) {
219-
this.$npm.executeNpmCommand("npm run-script postinstall", inspectorPath).wait();
220-
}
221-
222-
let cmd = `open -a '${inspectorApplicationPath}' --args '${inspectorSourceLocation}' '${this.$projectData.projectName}' '${fileDescriptor}'`;
223-
this.$childProcess.exec(cmd).wait();
224-
}).future<void>()();
225-
}
226-
227-
private getReadyForAttachTimeout(timeoutInMilliseconds?: number): number {
228-
let timeout = timeoutInMilliseconds || this.$utils.getMilliSecondsTimeout(TIMEOUT_SECONDS);
229-
let readyForAttachTimeout = timeout / 10;
230-
let defaultReadyForAttachTimeout = 5000;
231-
return readyForAttachTimeout > defaultReadyForAttachTimeout ? readyForAttachTimeout : defaultReadyForAttachTimeout;
232-
}
233226
}
234227
$injector.register("iOSDebugService", IOSDebugService);

0 commit comments

Comments
 (0)