async void RemoteConnection_DeviceStateChanged(object sender, DeviceStateEventArgs e)
 {
     try
     {
         if (RemotePipeDevicesDictionary.Count > 0)
         {
             if (sender is IPipeDevice pipeDevice)
             {
                 await _messenger.Publish(new RemoteConnection_DeviceStateChangedMessage(pipeDevice.Id, new DeviceStateDTO(e.State)));
             }
         }
     }
     catch (Exception ex)
     {
         Error(ex);
     }
 }
Ejemplo n.º 2
0
        private void Listen(CancellationToken token)
        {
            CheckDisposed();
            DeviceStateEventArgs state = new DeviceStateEventArgs {
                IsEnabled = true
            };

            resetWait.Reset();
            NetworkStream stream = client.Connected ? client.GetStream() : null;

            while (!token.IsCancellationRequested)
            {
                if (state.IsEnabled)
                {
                    try
                    {
                        if (!client.Connected)
                        {
                            lock (lockerClient)
                            {
                                client.Dispose();
                                client = null;
                                OpenClient();
                                stream = client.Connected ? client.GetStream() : null;
                            }
                        }

                        PollingRequests();

                        if (stream?.DataAvailable ?? false)
                        {
                            byte[] buffer = new byte[client.ReceiveBufferSize];

                            int length = 0;
                            lock (lockerClient)
                            {
                                length = stream.Read(buffer, 0, buffer.Length);
                            }
                            try
                            {
                                IDeviceEventArgs message = RecivedData(buffer, length);
                                if (message != null)
                                {
                                    DeviceRecivedMessage?.Invoke(this, message);
                                }
                            }
                            catch (Exception ex)
                            {
                                DeviceError?.Invoke(this, new DeviceErrorEventArgs {
                                    Ex = ex
                                });
                            }
                        }

                        DeviceCheckState?.Invoke(this, state);
                        if (!state.IsEnabled)
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        DeviceError?.Invoke(this, new DeviceErrorEventArgs {
                            Ex = ex
                        });
                    }
                }
                else
                {
                    Disconnection(false);
                }
                Thread.Sleep(pollingTimeout);
            }
            resetWait.Set();
        }