2121
2222namespace VncDotnet . WPF
2323{
24- public class VncDotnetControl : Control
24+ public class VncDotnetControl : Control , IVncHandler
2525 {
2626 static VncDotnetControl ( )
2727 {
@@ -56,12 +56,11 @@ public void Start(string host, int port, string? password, IEnumerable<SecurityT
5656 Task . Run ( ( ) => ReconnectLoop ( host , port , password , securityTypes , section , token ) ) ;
5757 }
5858
59- public void Start ( RfbConnection preEstablishedConnection , MonitorSnippet ? section )
59+ public async Task Attach ( RfbConnection preEstablishedConnection , MonitorSnippet ? section )
6060 {
6161 Section = section ;
6262 PreEstablishedConnection = preEstablishedConnection ;
63- PreEstablishedConnection . OnVncUpdate += Client_OnVncUpdate ;
64- PreEstablishedConnection . OnResolutionUpdate += Client_OnResolutionUpdate ;
63+ await PreEstablishedConnection . Attach ( this ) ;
6564 }
6665
6766 private async Task ReconnectLoop ( string host , int port , string ? password , IEnumerable < SecurityType > securityTypes , MonitorSnippet ? section , CancellationToken token )
@@ -72,8 +71,6 @@ private async Task ReconnectLoop(string host, int port, string? password, IEnume
7271 {
7372 Connection = await RfbConnection . ConnectAsync ( host , port , password , securityTypes , section , token ) ;
7473 Section = section ;
75- Connection . OnVncUpdate += Client_OnVncUpdate ;
76- Connection . OnResolutionUpdate += Client_OnResolutionUpdate ;
7774 await Connection . Start ( ) ;
7875 }
7976 catch ( OperationCanceledException ) { }
@@ -85,35 +82,45 @@ private async Task ReconnectLoop(string host, int port, string? password, IEnume
8582 }
8683 }
8784
88- private void Client_OnResolutionUpdate ( int framebufferWidth , int framebufferHeight )
85+ private int BitmapX ( )
8986 {
90- Application . Current ? . Dispatcher . Invoke ( new Action ( ( ) =>
87+ if ( Section != null )
9188 {
92- var image = ( Image ) GetTemplateChild ( "Scene" ) ;
93- if ( Section != null )
94- {
95- FramebufferWidth = Section . Width ;
96- FramebufferHeight = Section . Height ;
97- Bitmap = BitmapFactory . New ( FramebufferWidth , FramebufferHeight ) ;
98- }
99- else
100- {
101- FramebufferWidth = framebufferWidth ;
102- FramebufferHeight = framebufferHeight ;
103- Bitmap = BitmapFactory . New ( framebufferWidth , framebufferHeight ) ;
104- }
105- image . Source = Bitmap ;
106- } ) ) ;
89+ return Section . X ;
90+ }
91+ return 0 ;
92+ }
93+
94+ private int BitmapY ( )
95+ {
96+ if ( Section != null )
97+ {
98+ return Section . Y ;
99+ }
100+ return 0 ;
101+ }
102+
103+ public async Task Stop ( )
104+ {
105+ if ( Connection != null )
106+ {
107+ Connection . Stop ( ) ;
108+ }
109+
110+ if ( PreEstablishedConnection != null )
111+ {
112+ await PreEstablishedConnection . Detach ( this ) ;
113+ }
107114 }
108115
109- private void Client_OnVncUpdate ( IEnumerable < ( RfbRectangleHeader header , byte [ ] data ) > rectangles )
116+ public void HandleFramebufferUpdate ( IEnumerable < ( RfbRectangleHeader , byte [ ] ) > rectangles )
110117 {
111118 Application . Current ? . Dispatcher . Invoke ( new Action ( ( ) =>
112119 {
113120 var stopwatch = new Stopwatch ( ) ;
114121 stopwatch . Start ( ) ;
115122 if ( Bitmap == null )
116- throw new InvalidOperationException ( ) ;
123+ throw new InvalidOperationException ( "Bitmap is null" ) ;
117124 using ( var ctx = Bitmap . GetBitmapContext ( ) )
118125 {
119126 Bitmap . Lock ( ) ;
@@ -168,51 +175,35 @@ private void Client_OnVncUpdate(IEnumerable<(RfbRectangleHeader header, byte[] d
168175 rowLength * 4 ) ;
169176 }
170177 }
171- ArrayPool < byte > . Shared . Return ( data ) ;
172178 }
173179 }
174180
175- Bitmap . DrawRectangle ( 0 , 0 , 64 , 64 , Colors . GreenYellow ) ;
176181 Bitmap . AddDirtyRect ( new Int32Rect ( 0 , 0 , Bitmap . PixelWidth , Bitmap . PixelHeight ) ) ;
177182 Bitmap . Unlock ( ) ;
178183 }
179184 stopwatch . Stop ( ) ;
180- //Debug.WriteLine($"Client_OnVncUpdate invocation took {stopwatch.Elapsed}");
181185 } ) ) ;
182186 }
183187
184- private int BitmapX ( )
185- {
186- if ( Section != null )
187- {
188- return Section . X ;
189- }
190- return 0 ;
191- }
192-
193- private int BitmapY ( )
188+ public void HandleResolutionUpdate ( int framebufferWidth , int framebufferHeight )
194189 {
195- if ( Section != null )
196- {
197- return Section . Y ;
198- }
199- return 0 ;
200- }
201-
202- public void Stop ( )
203- {
204- if ( Connection != null )
205- {
206- Connection . OnResolutionUpdate -= Client_OnResolutionUpdate ;
207- Connection . OnVncUpdate -= Client_OnVncUpdate ;
208- Connection . Stop ( ) ;
209- }
210-
211- if ( PreEstablishedConnection != null )
190+ Application . Current ? . Dispatcher . Invoke ( new Action ( ( ) =>
212191 {
213- PreEstablishedConnection . OnResolutionUpdate -= Client_OnResolutionUpdate ;
214- PreEstablishedConnection . OnVncUpdate -= Client_OnVncUpdate ;
215- }
192+ var image = ( Image ) GetTemplateChild ( "Scene" ) ;
193+ if ( Section != null )
194+ {
195+ FramebufferWidth = Section . Width ;
196+ FramebufferHeight = Section . Height ;
197+ Bitmap = BitmapFactory . New ( FramebufferWidth , FramebufferHeight ) ;
198+ }
199+ else
200+ {
201+ FramebufferWidth = framebufferWidth ;
202+ FramebufferHeight = framebufferHeight ;
203+ Bitmap = BitmapFactory . New ( framebufferWidth , framebufferHeight ) ;
204+ }
205+ image . Source = Bitmap ;
206+ } ) ) ;
216207 }
217208 }
218209}
0 commit comments