Added automatic reconnect for GUI clients.

This commit is contained in:
Jared Goodwin 2020-03-10 20:19:47 -07:00
parent 17ab7bc296
commit bce4f7dc44
5 changed files with 104 additions and 44 deletions

View File

@ -149,8 +149,31 @@ namespace Remotely.Desktop.Linux.ViewModels
await PromptForHostName();
}
Conductor.ProcessArgs(new string[] { "-mode", "Normal", "-host", Host });
await Conductor.Connect();
Conductor.CasterSocket.Connection.Closed += async (ex) =>
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
SessionID = "Disconnected";
});
};
Conductor.CasterSocket.Connection.Reconnecting += async (ex) =>
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
SessionID = "Reconnecting";
});
};
Conductor.CasterSocket.Connection.Reconnected += async (arg) =>
{
await GetSessionID();
};
await Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await Conductor.CasterSocket.GetSessionID();
}
@ -162,6 +185,13 @@ namespace Remotely.Desktop.Linux.ViewModels
}
}
public async Task GetSessionID()
{
await Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await Conductor.CasterSocket.GetSessionID();
}
public async Task PromptForHostName()
{
var prompt = new HostNamePrompt();
@ -205,6 +235,7 @@ namespace Remotely.Desktop.Linux.ViewModels
ServiceContainer.Instance = serviceCollection.BuildServiceProvider();
}
private void ScreenCastRequested(object sender, ScreenCastRequest screenCastRequest)
{
Dispatcher.UIThread.InvokeAsync(async () =>

View File

@ -64,39 +64,6 @@ namespace Remotely.Desktop.Win.ViewModels
public Conductor Conductor { get; }
public CursorIconWatcher CursorIconWatcher { get; private set; }
public string Host
{
get => host;
set
{
host = value;
FirePropertyChanged("Host");
}
}
public bool IsAdministrator => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
public ICommand RemoveViewersCommand
{
get
{
return new Executor(async (param) =>
{
foreach (Viewer viewer in (param as IList<object>).ToArray())
{
viewer.DisconnectRequested = true;
ViewerRemoved(this, viewer.ViewerConnectionID);
await Conductor.CasterSocket.SendViewerRemoved(viewer.ViewerConnectionID);
}
},
(param) =>
{
return (param as IList<object>)?.Count > 0;
});
}
}
public ICommand ElevateToAdminCommand
{
get
@ -151,6 +118,38 @@ namespace Remotely.Desktop.Win.ViewModels
}
}
public string Host
{
get => host;
set
{
host = value;
FirePropertyChanged("Host");
}
}
public bool IsAdministrator => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
public ICommand RemoveViewersCommand
{
get
{
return new Executor(async (param) =>
{
foreach (Viewer viewer in (param as IList<object>).ToArray())
{
viewer.DisconnectRequested = true;
ViewerRemoved(this, viewer.ViewerConnectionID);
await Conductor.CasterSocket.SendViewerRemoved(viewer.ViewerConnectionID);
}
},
(param) =>
{
return (param as IList<object>)?.Count > 0;
});
}
}
public string SessionID
{
get => sessionID;
@ -184,6 +183,30 @@ namespace Remotely.Desktop.Win.ViewModels
try
{
await Conductor.Connect();
Conductor.CasterSocket.Connection.Closed += async (ex) =>
{
App.Current.Dispatcher.Invoke(() =>
{
SessionID = "Disconnected";
});
};
Conductor.CasterSocket.Connection.Reconnecting += async (ex) =>
{
App.Current.Dispatcher.Invoke(() =>
{
SessionID = "Reconnecting";
});
};
Conductor.CasterSocket.Connection.Reconnected += async (arg) =>
{
await GetSessionID();
};
await GetSessionID();
}
catch (Exception ex)
{
@ -191,7 +214,10 @@ namespace Remotely.Desktop.Win.ViewModels
MessageBox.Show(Application.Current.MainWindow, "Failed to connect to server.", "Connection Failed", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}
}
public async Task GetSessionID()
{
await Conductor.CasterSocket.SendDeviceInfo(Conductor.ServiceID, Environment.MachineName, Conductor.DeviceID);
await Conductor.CasterSocket.GetSessionID();
}
@ -235,7 +261,8 @@ namespace Remotely.Desktop.Win.ViewModels
serviceCollection.AddSingleton<CasterSocket>();
serviceCollection.AddSingleton<IdleTimer>();
serviceCollection.AddSingleton<Conductor>();
serviceCollection.AddTransient<ICapturer>(provider => {
serviceCollection.AddTransient<ICapturer>(provider =>
{
try
{
var dxCapture = new DXCapture();
@ -259,9 +286,10 @@ namespace Remotely.Desktop.Win.ViewModels
ServiceContainer.Instance = serviceCollection.BuildServiceProvider();
}
private async void CursorIconWatcher_OnChange(object sender, CursorInfo cursor)
{
if (Conductor?.CasterSocket != null && Conductor?.Viewers?.Count > 0)
if (Conductor?.CasterSocket?.IsConnected == true && Conductor?.Viewers?.Count > 0)
{
await Conductor?.CasterSocket?.SendCursorChange(cursor, Conductor.Viewers.Keys.ToList());
}

View File

@ -71,7 +71,7 @@ namespace Remotely.ScreenCast.Core.Capture
await casterSocket.SendScreenSize(bounds.Width, bounds.Height, viewerID);
};
while (!viewer.DisconnectRequested)
while (!viewer.DisconnectRequested && casterSocket.IsConnected)
{
try
{

View File

@ -35,16 +35,16 @@ namespace Remotely.ScreenCast.Core.Communication
ClipboardService.ClipboardTextChanged += ClipboardService_ClipboardTextChanged;
}
public bool IsConnected => Connection?.State == HubConnectionState.Connected;
public IScreenCaster ScreenCaster { get; }
private IAudioCapturer AudioCapturer { get; }
private IClipboardService ClipboardService { get; }
private HubConnection Connection { get; set; }
public HubConnection Connection { get; private set; }
private IKeyboardMouseInput KeyboardMouseInput { get; }
public async Task Connect(string host)
{
if (Connection != null)
@ -55,6 +55,7 @@ namespace Remotely.ScreenCast.Core.Communication
Connection = new HubConnectionBuilder()
.WithUrl($"{host}/RCDeviceHub")
.AddMessagePackProtocol()
.WithAutomaticReconnect()
.Build();
ApplyConnectionHandlers();
@ -108,6 +109,11 @@ namespace Remotely.ScreenCast.Core.Communication
await Connection.SendAsync("ReceiveDeviceInfo", serviceID, machineName, deviceID);
}
public async Task SendIceCandidateToBrowser(string candidate, int sdpMlineIndex, string sdpMid, string viewerConnectionID)
{
await Connection.SendAsync("SendIceCandidateToBrowser", candidate, sdpMlineIndex, sdpMid, viewerConnectionID);
}
public async Task SendMachineName(string machineName, string viewerID)
{
await Connection.SendAsync("SendMachineName", machineName, viewerID);
@ -137,11 +143,6 @@ namespace Remotely.ScreenCast.Core.Communication
{
await Connection.SendAsync("SendViewerRemoved", viewerID);
}
public async Task SendIceCandidateToBrowser(string candidate, int sdpMlineIndex, string sdpMid, string viewerConnectionID)
{
await Connection.SendAsync("SendIceCandidateToBrowser", candidate, sdpMlineIndex, sdpMid, viewerConnectionID);
}
private void ApplyConnectionHandlers()
{
var conductor = ServiceContainer.Instance.GetRequiredService<Conductor>();

View File

@ -27,7 +27,7 @@ namespace Remotely.ScreenCast.Win
}
public static async void CursorIconWatcher_OnChange(object sender, CursorInfo cursor)
{
if (Conductor?.CasterSocket != null)
if (Conductor?.CasterSocket?.IsConnected == true)
{
await Conductor.CasterSocket.SendCursorChange(cursor, Conductor.Viewers.Keys.ToList());
}