feat: manage multiple desktop device connections independently
This commit is contained in:
1 parent
08d60de364
commit
05d4d5fea6
12 files changed
+504
-249
No files matched your search
@@ -3,6 +3,7 @@ using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Headless;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
@@ -23,7 +24,7 @@ internal static class Program
|
||||
Exception? failure = null;
|
||||
Dispatcher.UIThread.Post(async () =>
|
||||
{
|
||||
try { await RunAsync(args.Contains("--live"), args.Contains("--screen"), args.Contains("--usb")); }
|
||||
try { if (args.Contains("--multi")) await RunMultiAsync(args.Contains("--usb")); else await RunAsync(args.Contains("--live"), args.Contains("--screen"), args.Contains("--usb")); }
|
||||
catch (Exception error) { failure = error; }
|
||||
finally { finished.Cancel(); }
|
||||
});
|
||||
@@ -39,7 +40,7 @@ internal static class Program
|
||||
|
||||
private static async Task UntilAsync(Func<bool> condition, string message)
|
||||
{
|
||||
var deadline = DateTime.UtcNow.AddSeconds(35);
|
||||
var deadline = DateTime.UtcNow.AddSeconds(60);
|
||||
while (!condition())
|
||||
{
|
||||
if (DateTime.UtcNow > deadline) throw new InvalidOperationException(message);
|
||||
@@ -47,6 +48,68 @@ internal static class Program
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task RunMultiAsync(bool usb)
|
||||
{
|
||||
var root = DesktopPaths.ProjectRoot()!;
|
||||
var folder = Path.Combine(root, "work", "desktop-multi", Guid.NewGuid().ToString("N"));
|
||||
using var vm = new MainViewModel(folder, backgroundMonitoring: true, loadLocalCredentials: true);
|
||||
int FreePort() { var probe = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Loopback, 0); probe.Start(); var port = ((System.Net.IPEndPoint)probe.LocalEndpoint).Port; probe.Stop(); return port; }
|
||||
vm.Port = FreePort(); vm.UsbPort = FreePort(); vm.UsbMode = usb;
|
||||
var secondId = Environment.GetEnvironmentVariable("ADS_MULTI_SECOND_DEVICE") ?? "smoke-multi-phone";
|
||||
var window = new MainWindow { DataContext = vm }; window.Show();
|
||||
try {
|
||||
await vm.InitializeAsync();
|
||||
await UntilAsync(() => vm.Devices.Any(d => d.Id == "home-phone") && vm.Devices.Any(d => d.Id == secondId), "Two test devices were not discovered");
|
||||
vm.SelectedDevice = vm.Devices.Single(d => d.Id == "home-phone");
|
||||
Require(!vm.SelectedDevice.Busy, "First device is occupied");
|
||||
await vm.ConnectCommand.ExecuteAsync(null);
|
||||
await UntilAsync(() => vm.Ready, "First phone was not ready: " + vm.Notice);
|
||||
var first = vm.Connections.Single(); var firstEndpoint = first.Endpoint;
|
||||
vm.SelectedDevice = vm.Devices.Single(d => d.Id == secondId);
|
||||
Require(vm.ConnectCommand.CanExecute(null), "A connected phone prevented connecting another");
|
||||
await vm.ConnectCommand.ExecuteAsync(null);
|
||||
await UntilAsync(() => vm.Connections.Count == 2 && vm.Connections.All(c => c.Ready && c.Model != "—"), "Both phones did not become ready: " + vm.Notice);
|
||||
var second = vm.Connections.Single(c => c.DeviceId == secondId);
|
||||
Require(first.Options.Port != second.Options.Port && first.Endpoint != second.Endpoint, "Device endpoints collided");
|
||||
if (usb) Require(first.Options.UsbPort != second.Options.UsbPort && first.Options.UsbPort != second.Options.Port && second.Options.UsbPort != first.Options.Port, "USB/IP ports collided");
|
||||
Require(first.Endpoint == firstEndpoint && first.HasConnection, "Selecting another phone altered the first connection");
|
||||
Require(vm.Devices.Count(d => d.IsLocal) == 2, "Local ownership was only shown for the selected phone");
|
||||
await vm.StartAllScreensCommand.ExecuteAsync(null);
|
||||
await UntilAsync(() => vm.Connections.All(c => c.ScreenRunning) && vm.Logs.Any(l => l.Contains("[home-phone]") && l.Contains("INFO: Texture")) && vm.Logs.Any(l => l.Contains("[" + secondId + "]") && l.Contains("INFO: Texture")), "Two independent scrcpy streams did not start");
|
||||
Require(first.ScreenPort != second.ScreenPort, "scrcpy forwarding ports collided");
|
||||
Require(window.FindControl<ItemsControl>("ConnectionsList")?.ItemCount == 2, "Overview did not display both connections");
|
||||
using (var frame = window.CaptureRenderedFrame()) frame?.Save(Path.Combine(root, "work", usb ? "desktop-multi-usb.png" : "desktop-multi.png"));
|
||||
var buttons = window.GetVisualDescendants().OfType<Button>().Where(b => b.Content as string == "查看").ToArray();
|
||||
Require(buttons.Length == 2 && buttons.All(b => b.Command != null), "Overview action bindings were not resolved");
|
||||
var selectFirst = buttons.Single(b => b.CommandParameter == first);
|
||||
selectFirst.Command!.Execute(selectFirst.CommandParameter);
|
||||
Require(vm.Current == first && vm.Endpoint == firstEndpoint, "Overview did not select the requested device");
|
||||
Console.WriteLine("PASS: two " + (usb ? "USB" : "network") + " connections, distinct ports/serials, overview actions, two real scrcpy video streams");
|
||||
await vm.ReconnectDeviceCommand.ExecuteAsync(first);
|
||||
await UntilAsync(() => vm.Connections.Count == 2 && vm.Connections.All(c => c.Ready), "Reconnecting first phone did not recover it");
|
||||
Require(second.HasConnection && second.Ready && second.ScreenRunning, "Reconnecting one phone interrupted the other");
|
||||
first = vm.Connections.Single(c => c.DeviceId == "home-phone");
|
||||
if (usb) {
|
||||
vm.SelectConnectionCommand.Execute(first);
|
||||
await vm.ClearPortCommand.ExecuteAsync(null);
|
||||
} else await vm.DisconnectDeviceCommand.ExecuteAsync(first);
|
||||
await UntilAsync(() => vm.Connections.Count == 1, "Disconnect did not remove only the first phone");
|
||||
Require(second.HasConnection && second.Ready && second.ScreenRunning, "Disconnecting one phone interrupted the other");
|
||||
vm.SelectedDevice = vm.Devices.Single(d => d.Id == secondId);
|
||||
await vm.ReadDeviceInfoCommand.ExecuteAsync(null);
|
||||
Require(vm.Model != "—", "Second phone stopped responding after first disconnected");
|
||||
Console.WriteLine("PASS: disconnecting one phone preserves the other's ADB and video");
|
||||
await vm.DisconnectAllCommand.ExecuteAsync(null);
|
||||
Require(vm.Connections.Count == 0 && !vm.HasConnections, "Disconnect all leaked a connection");
|
||||
await vm.RefreshCommand.ExecuteAsync(null);
|
||||
Require(vm.Devices.Where(d => d.Id == "home-phone" || d.Id == secondId).All(d => !d.Busy), "Multi-device leases were not released");
|
||||
Require(!vm.Logs.Any(l => l.Contains(vm.Token)), "A multi-device log leaked a token");
|
||||
Console.WriteLine("PASS: selected reconnect, disconnect all and independent lease cleanup");
|
||||
} finally {
|
||||
await vm.ShutdownAsync(); File.WriteAllLines(Path.Combine(root, "work", "desktop-multi.log"), vm.Logs); window.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task RunAsync(bool live, bool screen, bool usb)
|
||||
{
|
||||
var root = DesktopPaths.ProjectRoot() ?? throw new InvalidOperationException("Project root not found");
|
||||
@@ -83,8 +146,9 @@ internal static class Program
|
||||
try
|
||||
{
|
||||
vm.Port = ((System.Net.IPEndPoint)occupied.LocalEndpoint).Port;
|
||||
await vm.ConnectCommand.ExecuteAsync(null);
|
||||
Require(!vm.HasConnection && vm.Notice.Contains("已被占用"), "Occupied port was not rejected safely");
|
||||
var allocated = ConnectionPorts.Allocate(vm.Port, []);
|
||||
Require(allocated != vm.Port, "Automatic allocation did not skip occupied port");
|
||||
Require(!vm.HasConnection, "Port check started an unwanted connection");
|
||||
}
|
||||
finally { occupied.Stop(); }
|
||||
var childInfo = ProcessTools.CreateStartInfo("dotnet", [typeof(Program).Assembly.Location, "--port-child", vm.Port.ToString()]);
|
||||
|
||||
Reference in new issue
Block a user