@@ -144,7 +144,29 @@ public static CommunicationTypes CommunicationType
144144 private static object Lck_Ping = new object ( ) ;
145145
146146 private static Thread SenderThread ;
147+ private static Thread ReceiverThread ;
147148
149+ private static bool IsReceiverThreadEnabled = false ;
150+ private static bool IsSubDataReceived
151+ {
152+ get
153+ {
154+ lock ( Lck_IsSubDataReceived )
155+ return _isSubDataReceived ;
156+ }
157+ set
158+ {
159+ lock ( Lck_IsSubDataReceived )
160+ _isSubDataReceived = value ;
161+ }
162+ }
163+
164+ private static byte [ ] SubReceivedData ;
165+
166+ private static bool _isSubDataReceived = false ;
167+
168+ private static object Lck_SubReceivedData = new object ( ) ;
169+ private static object Lck_IsSubDataReceived = new object ( ) ;
148170 #endregion
149171
150172 #region MQ Variables
@@ -168,6 +190,7 @@ public static CommunicationTypes CommunicationType
168190
169191 private static TimeSpan PublisherTimeBase ;
170192 private static TimeSpan SubscriberPreviousTime ;
193+ private static int ScanCounter = 0 ;
171194
172195 public enum CommunicationTypes
173196 {
@@ -252,8 +275,57 @@ public static void StartReceiving(string ip)
252275 TimeBaseSubscriber . OnDataReceived += TimeBaseSubscriber_OnDataReceived ;
253276 SubStopwatch = Stopwatch . StartNew ( ) ;
254277 CommunicationType = CommunicationTypes . Receiver ;
278+ IsReceiverThreadEnabled = true ;
279+ ReceiverThread = new Thread ( ReceiverCoreFcn ) ;
280+ ReceiverThread . Start ( ) ;
255281 }
282+ private static void ReceiverCoreFcn ( )
283+ {
284+ while ( IsReceiverThreadEnabled )
285+ {
286+ if ( IsSubDataReceived )
287+ {
288+ IsSubDataReceived = false ;
289+ Stopwatch stp = Stopwatch . StartNew ( ) ;
290+ byte [ ] timeBytes = new byte [ 16 ] ;
291+ byte [ ] receivedData ;
292+ lock ( Lck_SubReceivedData )
293+ {
294+ receivedData = new byte [ SubReceivedData . Length ] ;
295+ SubReceivedData . CopyTo ( receivedData , 0 ) ;
296+ }
256297
298+ Array . Copy ( receivedData , 0 , timeBytes , 0 , timeBytes . Length ) ;
299+ string timeString = Encoding . ASCII . GetString ( timeBytes ) ;
300+ TimeSpan SentTime = TimeSpan . Parse ( timeString ) ;
301+ TimeSpan CurrentTime = DateTime . UtcNow . TimeOfDay ;
302+ PublisherTimeBase += ( CurrentTime - SubscriberPreviousTime ) ;
303+ SubscriberPreviousTime = CurrentTime ;
304+ double deltaTime = PublisherTimeBase . TotalMilliseconds - SentTime . TotalMilliseconds ;
305+ if ( Ping <= 0 )
306+ Ping = deltaTime ;
307+ Ping = Ping * 0.99 + 0.01 * deltaTime ;
308+ byte [ ] ScreenBytes = new byte [ receivedData . Length - timeBytes . Length ] ;
309+ Array . Copy ( receivedData , timeBytes . Length , ScreenBytes , 0 , ScreenBytes . Length ) ;
310+ ScreenImage = ImageProcessing . ImageFromByteArray ( ScreenBytes ) ;
311+ TotalBytesReceived += receivedData . Length ;
312+ FpsCounter ++ ;
313+ if ( SubStopwatch . ElapsedMilliseconds >= 1000 )
314+ {
315+ UpdateStats ( TotalBytesReceived , SubStopwatch . Elapsed . TotalSeconds ) ;
316+ TotalBytesReceived = 0 ;
317+ SubStopwatch . Restart ( ) ;
318+ }
319+ IsImageShowed = true ;
320+ }
321+ else
322+ {
323+ ScanCounter ++ ;
324+
325+ Thread . Sleep ( 1 ) ;
326+ }
327+ }
328+ }
257329 private static void TimeBaseSubscriber_OnDataReceived ( byte [ ] data )
258330 {
259331 if ( data != null )
@@ -265,32 +337,18 @@ private static void TimeBaseSubscriber_OnDataReceived(byte[] data)
265337
266338 private static void Subscriber_OnDataReceived ( byte [ ] data )
267339 {
268- Stopwatch stp = Stopwatch . StartNew ( ) ;
269340 if ( data != null )
270341 {
271- byte [ ] timeBytes = new byte [ 16 ] ;
272- Array . Copy ( data , 0 , timeBytes , 0 , timeBytes . Length ) ;
273- string timeString = Encoding . ASCII . GetString ( timeBytes ) ;
274- TimeSpan SentTime = TimeSpan . Parse ( timeString ) ;
275- TimeSpan CurrentTime = DateTime . UtcNow . TimeOfDay ;
276- PublisherTimeBase += ( CurrentTime - SubscriberPreviousTime ) ;
277- SubscriberPreviousTime = CurrentTime ;
278- double deltaTime = PublisherTimeBase . TotalMilliseconds - SentTime . TotalMilliseconds ;
279- if ( Ping <= 0 )
280- Ping = deltaTime ;
281- Ping = Ping * 0.99 + 0.01 * deltaTime ;
282- byte [ ] ScreenBytes = new byte [ data . Length - timeBytes . Length ] ;
283- Array . Copy ( data , timeBytes . Length , ScreenBytes , 0 , ScreenBytes . Length ) ;
284- ScreenImage = ImageProcessing . ImageFromByteArray ( ScreenBytes ) ;
285- TotalBytesReceived += data . Length ;
286- FpsCounter ++ ;
287- if ( SubStopwatch . ElapsedMilliseconds >= 1000 )
342+ lock ( Lck_SubReceivedData )
343+ {
344+ SubReceivedData = data ;
345+ }
346+ IsSubDataReceived = true ;
347+ if ( ScanCounter > 250 )
288348 {
289- UpdateStats ( TotalBytesReceived , SubStopwatch . Elapsed . TotalSeconds ) ;
290- TotalBytesReceived = 0 ;
291- SubStopwatch . Restart ( ) ;
349+ NetworkScanner . ScanAvailableDevices ( ) ;
350+ ScanCounter = 0 ;
292351 }
293- IsImageShowed = true ;
294352 }
295353 else
296354 {
@@ -301,6 +359,7 @@ public static void StopReceiving()
301359 {
302360 Subscriber . Stop ( ) ;
303361 TimeBaseSubscriber . Stop ( ) ;
362+ IsReceiverThreadEnabled = false ;
304363 FPS = 0 ;
305364 TransferSpeed = 0 ;
306365 }
0 commit comments