public static void UpdateDevices <T>(Logger log, Dictionary <T, string> enumeratedDevices, Dictionary <T, ConnectedDevice> currentDevices, Action <ConnectedDevice> connectDevice) { // Stop tasks used by disconnected devices foreach (var oldDevice in currentDevices.ToArray()) { if (enumeratedDevices.ContainsKey(oldDevice.Key)) { continue; } oldDevice.Value.DeviceDisconnected = true; currentDevices.Remove(oldDevice.Key); log.Info($"Device removed: {oldDevice.Value.Name} ({oldDevice.Key})"); } // Start new devices foreach (var androidDevice in enumeratedDevices) { if (currentDevices.ContainsKey(androidDevice.Key)) { continue; } var connectedDevice = new ConnectedDevice { Key = androidDevice.Key, Name = androidDevice.Value, }; currentDevices.Add(androidDevice.Key, connectedDevice); connectDevice(connectedDevice); } }
internal Process SetupProxy(ConnectedDevice device) { var iosId = "iproxy.exe"; int testedLocalPort; do { testedLocalPort = startLocalPort++; if (startLocalPort >= 65536) // Make sure we stay in the range of dynamic ports: 49152-65535 { startLocalPort = 49152; } } while (!CheckAvailableServerPort(testedLocalPort)); Task.Run(async() => { while (!device.DeviceDisconnected) { try { await router.TryConnect("localhost", testedLocalPort); } catch (Exception) { // Mute exceptions and try to connect again // TODO: Mute connection only, not message loop? } await Task.Delay(200); } }); var process = new Process { StartInfo = { UseShellExecute = false, CreateNoWindow = true, FileName = iosId, Arguments = $"{testedLocalPort} {RouterClient.DefaultListenPort} {device.Name}" } }; process.Start(); new AttachedChildProcessJob(process); Log.Info($"iOS Device connected: {device.Name}; successfully mapped port {testedLocalPort}:{RouterClient.DefaultListenPort}"); return(process); }
public static async Task LaunchPersistentClient(ConnectedDevice connectedDevice, Router router, string address, int localPort) { while (!connectedDevice.DeviceDisconnected) { try { await router.TryConnect(address, localPort); } catch (Exception) { // Mute exceptions and try to connect again // TODO: Mute connection only, not message loop? } await Task.Delay(200); } }
public async Task TrackDevices() { var iosId = "idevice_id.exe"; while (true) { var thisRunDevices = new List <string>(); var process = new Process { StartInfo = { UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, FileName = iosId, Arguments = "-l" } }; process.OutputDataReceived += (sender, args) => { if (string.IsNullOrEmpty(args.Data)) { return; } thisRunDevices.Add(args.Data); if (devices.ContainsKey(args.Data)) { return; } Log.Info($"New iOS devices: {args.Data}"); var newDev = new ConnectedDevice { DeviceDisconnected = false, Name = args.Data }; proxies[args.Data] = SetupProxy(newDev); devices[args.Data] = newDev; }; process.Start(); process.BeginOutputReadLine(); process.WaitForExit(); var toRemove = devices.Where(device => !thisRunDevices.Contains(device.Key)).ToList(); foreach (var device in toRemove) { device.Value.DeviceDisconnected = true; proxies[device.Key].Kill(); proxies[device.Key].WaitForExit(); proxies.Remove(device.Key); devices.Remove(device.Key); Log.Info($"Disconnected iOS devices: {device}"); } await Task.Delay(1000); } }