Skip to content

Commit

Permalink
Support for preestablished connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Trolldemorted committed Sep 18, 2019
1 parent 0f36269 commit eb698f5
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions VncDotnet.WPF/VncDotnet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@

namespace VncDotnet.WPF
{
public class VncDotnet : Control, IDisposable
public class VncDotnet : Control
{
static VncDotnet()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(VncDotnet), new FrameworkPropertyMetadata(typeof(VncDotnet)));
}

private WriteableBitmap? Bitmap;
private RfbConnection? Client = null;
private RfbConnection? Connection = null;
private RfbConnection? PreEstablishedConnection = null;
private MonitorSnippet? Section = null;
private int FramebufferWidth;
private int FramebufferHeight;
Expand All @@ -54,17 +55,24 @@ public void Start(string host, int port, string password, IEnumerable<SecurityTy
Task.Run(() => ReconnectLoop(host, port, password, securityTypes, section, token));
}

public void Start(RfbConnection preEstablishedConnection)
{
PreEstablishedConnection = preEstablishedConnection;
PreEstablishedConnection.OnVncUpdate += Client_OnVncUpdate;
PreEstablishedConnection.OnResolutionUpdate += Client_OnResolutionUpdate;
}

private async Task ReconnectLoop(string host, int port, string password, IEnumerable<SecurityType> securityTypes, MonitorSnippet? section, CancellationToken token)
{
while (!token.IsCancellationRequested)
{
try
{
Client = await RfbConnection.ConnectAsync(host, port, password, securityTypes, section, token);
Connection = await RfbConnection.ConnectAsync(host, port, password, securityTypes, section, token);
Section = section;
Client.OnVncUpdate += Client_OnVncUpdate;
Client.OnResolutionUpdate += Client_OnResolutionUpdate;
await Client.Start();
Connection.OnVncUpdate += Client_OnVncUpdate;
Connection.OnResolutionUpdate += Client_OnResolutionUpdate;
await Connection.Start();
}
catch (OperationCanceledException) { }
catch (Exception e)
Expand Down Expand Up @@ -180,9 +188,20 @@ private int BitmapY()
return 0;
}

public void Dispose()
public void Stop()
{
Client?.Stop();
if (Connection != null)
{
Connection.OnResolutionUpdate -= Client_OnResolutionUpdate;
Connection.OnVncUpdate -= Client_OnVncUpdate;
Connection.Stop();
}

if (PreEstablishedConnection != null)
{
PreEstablishedConnection.OnResolutionUpdate -= Client_OnResolutionUpdate;
PreEstablishedConnection.OnVncUpdate -= Client_OnVncUpdate;
}
}
}
}

0 comments on commit eb698f5

Please sign in to comment.