Skip to content

Commit d57d0db

Browse files
fix: Remove hardcoded port for livesync on Android
Currently the LiveSync operations on Android devices uses a hardcoded port - 18182 to wait for a message from the runtime and determines if we should clean the LiveSync temp directories on the device. The port forwarding is executed every time on all of the devices. This is a problem when the hardcoded port is already used by another application. In order to fix this, execute the port forwarding only once per device and persist the port. Use the newly introduced method in android-process-service, which will check if there's already a forwarded port for the specified abstract port. In case there's no such forwarding, a new free port is taken and the forwarding is executed. Once CLI finishes its work, the port forwarding is removed. This ensures we'll execute only one port forwarding per device and also we'll not clash with other applications as the port is no longer hardcoded.
1 parent 4c259e4 commit d57d0db

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/services/livesync/android-device-livesync-service.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import * as path from "path";
88
import * as net from "net";
99

1010
export class AndroidDeviceLiveSyncService extends DeviceLiveSyncServiceBase implements IAndroidNativeScriptDeviceLiveSyncService, INativeScriptDeviceLiveSyncService {
11-
private static BACKEND_PORT = 18182;
1211
private device: Mobile.IAndroidDevice;
12+
private port: number;
1313

1414
constructor(_device: Mobile.IDevice,
1515
private $mobileHelper: Mobile.IMobileHelper,
1616
private $devicePathProvider: IDevicePathProvider,
1717
private $injector: IInjector,
18+
private $androidProcessService: Mobile.IAndroidProcessService,
1819
protected $platformsData: IPlatformsData) {
1920
super($platformsData);
2021
this.device = <Mobile.IAndroidDevice>(_device);
@@ -85,7 +86,13 @@ export class AndroidDeviceLiveSyncService extends DeviceLiveSyncServiceBase impl
8586
}
8687

8788
private async reloadApplicationFiles(deviceAppData: Mobile.IDeviceAppData, localToDevicePaths: Mobile.ILocalToDevicePathData[]): Promise<boolean> {
88-
await this.device.adb.executeCommand(["forward", `tcp:${AndroidDeviceLiveSyncService.BACKEND_PORT.toString()}`, `localabstract:${deviceAppData.appIdentifier}-livesync`]);
89+
if (!this.port) {
90+
this.port = await this.$androidProcessService.forwardFreeTcpToAbstractPort({
91+
deviceIdentifier: deviceAppData.device.deviceInfo.identifier,
92+
appIdentifier: deviceAppData.appIdentifier,
93+
abstractPort: `localabstract:${deviceAppData.appIdentifier}-livesync`
94+
});
95+
}
8996

9097
if (await this.awaitRuntimeReloadSuccessMessage()) {
9198
await this.cleanLivesyncDirectories(deviceAppData);
@@ -121,10 +128,11 @@ export class AndroidDeviceLiveSyncService extends DeviceLiveSyncServiceBase impl
121128
let isResolved = false;
122129
const socket = new net.Socket();
123130

124-
socket.connect(AndroidDeviceLiveSyncService.BACKEND_PORT, '127.0.0.1', () => {
131+
socket.connect(this.port, '127.0.0.1', () => {
125132
socket.write(new Buffer([0, 0, 0, 1, 1]));
126133
});
127134
socket.on("data", (data: any) => {
135+
isResolved = true;
128136
socket.destroy();
129137
resolve(true);
130138
});

0 commit comments

Comments
 (0)