Skip to content

Commit 1b52128

Browse files
committed
fix: allow device socket and client proxy socket reuse on iOS
1 parent 45ea8ba commit 1b52128

18 files changed

+217
-66
lines changed

.vscode/launch.json

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
{
88
"type": "node",
99
"request": "launch",
10-
"cwd": "${workspaceRoot}",
10+
"cwd": "/Users/tachev/Work/Test/testJsHw",
1111
"sourceMaps": true,
1212
// In case you want to debug child processes started from CLI:
1313
// "autoAttachChildProcesses": true,
1414
"name": "Launch CLI (Node 6+)",
1515
"program": "${workspaceRoot}/lib/nativescript-cli.js",
16-
1716
// example commands
18-
"args": [ "create", "cliapp"]
17+
"args": [
18+
"debug",
19+
"ios"
20+
]
1921
// "args": [ "test", "android", "--justlaunch"]
2022
// "args": [ "platform", "add", "[email protected]", "--path", "cliapp"]
2123
// "args": [ "platform", "remove", "android", "--path", "cliapp"]
@@ -38,7 +40,6 @@
3840
"cwd": "${workspaceRoot}",
3941
"sourceMaps": true
4042
},
41-
4243
{
4344
"type": "node",
4445
"runtimeArgs": [
@@ -51,11 +52,8 @@
5152
"sourceMaps": true,
5253
// define the arguments that you would like to pass to CLI, for example
5354
// "args": [ "build", "android", "--justlaunch" ]
54-
"args": [
55-
56-
]
55+
"args": []
5756
},
58-
5957
{
6058
// in case you want to debug a single test, modify it's code to be `it.only(...` instead of `it(...`
6159
"type": "node",
@@ -68,7 +66,6 @@
6866
"cwd": "${workspaceRoot}",
6967
"sourceMaps": true
7068
},
71-
7269
{
7370
"type": "node",
7471
"request": "attach",
@@ -77,7 +74,6 @@
7774
"port": 9897,
7875
"sourceMaps": true
7976
},
80-
8177
{
8278
"type": "node",
8379
"request": "attach",
@@ -87,6 +83,5 @@
8783
"port": 9855,
8884
"sourceMaps": true
8985
}
90-
9186
]
9287
}

lib/bootstrap.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ $injector.require("androidPluginBuildService", "./services/android-plugin-build-
1414
$injector.require("iOSEntitlementsService", "./services/ios-entitlements-service");
1515
$injector.require("iOSProjectService", "./services/ios-project-service");
1616
$injector.require("iOSProvisionService", "./services/ios-provision-service");
17+
$injector.require("iOSDeviceSocketService", "./services/ios-device-socket-service");
1718
$injector.require("xCConfigService", "./services/xcconfig-service");
1819

1920
$injector.require("cocoapodsService", "./services/cocoapods-service");

lib/commands/debug.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { isInteractive } from "../common/helpers";
33
import { cache } from "../common/decorators";
44
import { DebugCommandErrors } from "../constants";
55
import { ValidatePlatformCommandBase } from "./command-base";
6+
import { LiveSyncCommandHelper } from "../helpers/livesync-command-helper";
67

78
export class DebugPlatformCommand extends ValidatePlatformCommandBase implements ICommand {
89
public allowedParameters: ICommandParameter[] = [];
910

1011
constructor(private platform: string,
12+
private $bundleValidatorHelper: IBundleValidatorHelper,
1113
private $debugService: IDebugService,
1214
protected $devicesService: Mobile.IDevicesService,
1315
$platformService: IPlatformService,
@@ -21,7 +23,7 @@ export class DebugPlatformCommand extends ValidatePlatformCommandBase implements
2123
private $prompter: IPrompter,
2224
private $liveSyncCommandHelper: ILiveSyncCommandHelper,
2325
private $androidBundleValidatorHelper: IAndroidBundleValidatorHelper) {
24-
super($options, $platformsData, $platformService, $projectData);
26+
super($options, $platformsData, $platformService, $projectData);
2527
}
2628

2729
public async execute(args: string[]): Promise<void> {
@@ -36,7 +38,7 @@ export class DebugPlatformCommand extends ValidatePlatformCommandBase implements
3638

3739
const selectedDeviceForDebug = await this.getDeviceForDebug();
3840

39-
const debugData = this.$debugDataService.createDebugData(this.$projectData, {device: selectedDeviceForDebug.deviceInfo.identifier});
41+
const debugData = this.$debugDataService.createDebugData(this.$projectData, { device: selectedDeviceForDebug.deviceInfo.identifier });
4042

4143
if (this.$options.start) {
4244
await this.$liveSyncService.printDebugInformation(await this.$debugService.debug(debugData, debugOptions));
@@ -118,7 +120,10 @@ export class DebugPlatformCommand extends ValidatePlatformCommandBase implements
118120
this.$errors.fail("--release flag is not applicable to this command");
119121
}
120122

121-
const result = await super.canExecuteCommandBase(this.platform, { validateOptions: true, notConfiguredEnvOptions: { hideCloudBuildOption: true }});
123+
const minSupportedWebpackVersion = this.$options.hmr ? LiveSyncCommandHelper.MIN_SUPPORTED_WEBPACK_VERSION_WITH_HMR : null;
124+
this.$bundleValidatorHelper.validate(minSupportedWebpackVersion);
125+
126+
const result = await super.canExecuteCommandBase(this.platform, { validateOptions: true, notConfiguredEnvOptions: { hideCloudBuildOption: true, hideSyncToPreviewAppOption: true } });
122127
return result;
123128
}
124129
}
@@ -209,7 +214,7 @@ export class DebugAndroidCommand implements ICommand {
209214
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
210215
private $injector: IInjector,
211216
private $projectData: IProjectData) {
212-
this.$projectData.initializeProjectData();
217+
this.$projectData.initializeProjectData();
213218
}
214219

215220
public execute(args: string[]): Promise<void> {

lib/common/definitions/mobile.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ declare module Mobile {
115115
openDeviceLogStream(options?: IiOSLogStreamOptions): Promise<void>;
116116
}
117117

118+
interface IiOSDeviceSocketsService {
119+
getSocket(deviceId: string): any;
120+
addSocket(deviceId: string, socket: any): void;
121+
}
122+
118123
interface IAndroidDevice extends IDevice {
119124
adb: Mobile.IDeviceAndroidDebugBridge;
120125
init(): Promise<void>;
@@ -125,7 +130,7 @@ declare module Mobile {
125130
getDeviceHashService(appIdentifier: string): Mobile.IAndroidDeviceHashService;
126131
}
127132

128-
interface IiOSSimulator extends IDevice { }
133+
// interface IiOSSimulator extends IDevice { }
129134

130135
/**
131136
* Describes log stream options

lib/common/mobile/ios/device/ios-device.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,17 @@ export class IOSDevice implements Mobile.IiOSDevice {
88
public applicationManager: Mobile.IDeviceApplicationManager;
99
public fileSystem: Mobile.IDeviceFileSystem;
1010
public deviceInfo: Mobile.IDeviceInfo;
11+
private socket: net.Socket;
12+
13+
// private static sockets: { [id: string]: net.Socket; } = {};
14+
15+
// get socket(): net.Socket {
16+
// return IOSDevice.sockets[this.deviceInfo.identifier];
17+
// }
18+
// set socket(newSocket: net.Socket) {
19+
// IOSDevice.sockets[this.deviceInfo.identifier] = newSocket;
20+
// }
1121

12-
private _socket: net.Socket;
1322
private _deviceLogHandler: (...args: any[]) => void;
1423

1524
constructor(private deviceActionInfo: IOSDeviceLib.IDeviceActionInfo,
@@ -19,6 +28,7 @@ export class IOSDevice implements Mobile.IiOSDevice {
1928
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
2029
private $iOSDeviceProductNameMapper: Mobile.IiOSDeviceProductNameMapper,
2130
private $iosDeviceOperations: IIOSDeviceOperations,
31+
private $logger: ILogger,
2232
private $mobileHelper: Mobile.IMobileHelper) {
2333

2434
this.applicationManager = this.$injector.resolve(applicationManagerPath.IOSApplicationManager, { device: this, devicePointer: this.deviceActionInfo });
@@ -74,15 +84,20 @@ export class IOSDevice implements Mobile.IiOSDevice {
7484

7585
// This function works only on OSX
7686
public async connectToPort(port: number): Promise<net.Socket> {
87+
console.log("connectToPort");
7788
const deviceId = this.deviceInfo.identifier;
7889
const deviceResponse = _.first((await this.$iosDeviceOperations.connectToPort([{ deviceId: deviceId, port: port }]))[deviceId]);
7990

80-
const socket = new net.Socket();
81-
socket.connect(deviceResponse.port, deviceResponse.host);
82-
this._socket = socket;
91+
const _socket = new net.Socket();
92+
_socket.connect(deviceResponse.port, deviceResponse.host);
93+
this.socket = _socket;
94+
_socket.on("close", () => {
95+
this.socket = null;
96+
this.$logger.info("iOS Device socket closed!");
97+
});
8398

8499
this.$processService.attachToProcessExitSignals(this, this.destroySocket);
85-
return this._socket;
100+
return this.socket;
86101
}
87102

88103
private getActiveArchitecture(productType: string): string {
@@ -108,9 +123,9 @@ export class IOSDevice implements Mobile.IiOSDevice {
108123
}
109124

110125
private destroySocket() {
111-
if (this._socket) {
112-
this._socket.destroy();
113-
this._socket = null;
126+
if (this.socket) {
127+
this.socket.destroy();
128+
this.socket = null;
114129
}
115130
}
116131
}

lib/common/mobile/ios/simulator/ios-emulator-services.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class IosEmulatorServices implements Mobile.IiOSSimulatorService {
8585

8686
const output = await this.tryGetiOSSimDevices();
8787
if (output.devices && output.devices.length) {
88-
devices = _(output.devices)
88+
devices = _(output.devices)
8989
.map(simDevice => this.convertSimDeviceToDeviceInfo(simDevice))
9090
.sortBy(deviceInfo => deviceInfo.version)
9191
.value();
@@ -102,7 +102,7 @@ class IosEmulatorServices implements Mobile.IiOSSimulatorService {
102102
return [];
103103
}
104104

105-
private async tryGetiOSSimDevices(): Promise<{devices: Mobile.IiSimDevice[], error: string}> {
105+
private async tryGetiOSSimDevices(): Promise<{ devices: Mobile.IiSimDevice[], error: string }> {
106106
let devices: Mobile.IiSimDevice[] = [];
107107
let error: string = null;
108108

lib/common/mobile/ios/simulator/ios-simulator-device.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
import * as applicationManagerPath from "./ios-simulator-application-manager";
22
import * as fileSystemPath from "./ios-simulator-file-system";
33
import * as constants from "../../../constants";
4+
import * as net from "net";
45
import { cache } from "../../../decorators";
56

6-
export class IOSSimulator implements Mobile.IiOSSimulator {
7+
export class IOSSimulator implements Mobile.IiOSDevice {
78
private _applicationManager: Mobile.IDeviceApplicationManager;
89
private _fileSystem: Mobile.IDeviceFileSystem;
10+
private socket: net.Socket;
11+
12+
// private static sockets: { [id: string]: net.Socket; } = {};
13+
14+
// get socket(): net.Socket {
15+
// return IOSSimulator.sockets[this.deviceInfo.identifier];
16+
// }
17+
// set socket(newSocket: net.Socket) {
18+
// IOSSimulator.sockets[this.deviceInfo.identifier] = newSocket;
19+
// }
920

1021
constructor(private simulator: Mobile.IiSimDevice,
1122
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
1223
private $injector: IInjector,
1324
private $iOSSimResolver: Mobile.IiOSSimResolver,
14-
private $iOSSimulatorLogProvider: Mobile.IiOSSimulatorLogProvider) { }
25+
private $iOSEmulatorServices: Mobile.IiOSSimulatorService,
26+
private $iOSSimulatorLogProvider: Mobile.IiOSSimulatorLogProvider,
27+
private $logger: ILogger) { }
28+
29+
public async connectToPort(port: number): Promise<net.Socket> {
30+
console.log("connectToPort");
31+
this.socket = await this.$iOSEmulatorServices.connectToPort({ port });
32+
this.socket.on("close", () => {
33+
this.socket = null;
34+
this.$logger.info("iOS Simulator socket closed!");
35+
});
36+
37+
return this.socket;
38+
}
1539

1640
public get deviceInfo(): Mobile.IDeviceInfo {
1741
return {

lib/common/test/unit-tests/mobile/ios-simulator-discovery.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ describe("ios-simulator-discovery", () => {
5858
let defaultRunningSimulator: any;
5959
let expectedDeviceInfo: Mobile.IDeviceInfo = null;
6060

61-
const detectNewSimulatorAttached = async (runningSimulator: any): Promise<Mobile.IiOSSimulator> => {
62-
return new Promise<Mobile.IiOSSimulator>(async (resolve, reject) => {
61+
const detectNewSimulatorAttached = async (runningSimulator: any): Promise<Mobile.IiOSDevice> => {
62+
return new Promise<Mobile.IiOSDevice>(async (resolve, reject) => {
6363
currentlyRunningSimulators.push(_.cloneDeep(runningSimulator));
64-
iOSSimulatorDiscovery.once(DeviceDiscoveryEventNames.DEVICE_FOUND, (device: Mobile.IDevice) => {
64+
iOSSimulatorDiscovery.once(DeviceDiscoveryEventNames.DEVICE_FOUND, (device: Mobile.IiOSDevice) => {
6565
resolve(device);
6666
});
6767
await iOSSimulatorDiscovery.startLookingForDevices();
6868
});
6969
};
7070

71-
const detectSimulatorDetached = async (simulatorId: string): Promise<Mobile.IiOSSimulator> => {
71+
const detectSimulatorDetached = async (simulatorId: string): Promise<Mobile.IiOSDevice> => {
7272
_.remove(currentlyRunningSimulators, simulator => simulator.id === simulatorId);
73-
return new Promise<Mobile.IDevice>(async (resolve, reject) => {
74-
iOSSimulatorDiscovery.once(DeviceDiscoveryEventNames.DEVICE_LOST, (device: Mobile.IDevice) => {
73+
return new Promise<Mobile.IiOSDevice>(async (resolve, reject) => {
74+
iOSSimulatorDiscovery.once(DeviceDiscoveryEventNames.DEVICE_LOST, (device: Mobile.IiOSDevice) => {
7575
resolve(device);
7676
});
7777
await iOSSimulatorDiscovery.startLookingForDevices();

lib/declarations.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,8 +732,13 @@ interface IAndroidToolsInfoValidateInput extends IAndroidToolsInfoOptions {
732732
}
733733

734734
interface ISocketProxyFactory extends NodeJS.EventEmitter {
735-
createTCPSocketProxy(factory: () => Promise<any>): Promise<any>;
736-
createWebSocketProxy(factory: () => Promise<any>, deviceIdentifier: string): Promise<any>;
735+
getTCPSocketProxy(deviceIdentifier: string): any;
736+
addTCPSocketProxy(factory: () => Promise<any>, deviceIdentifier: string): Promise<any>;
737+
738+
getWebSocketProxy(deviceIdentifier: string): any;
739+
addWebSocketProxy(factory: () => Promise<any>, deviceIdentifier: string): Promise<any>;
740+
741+
removeAllProxies(): void;
737742
}
738743

739744
interface IiOSNotification extends NodeJS.EventEmitter {

0 commit comments

Comments
 (0)