Example #1
0
        public override void Close()
        {
            if (Socket != null)
            {
                Socket.OnConnectionChanged -= OnConnectionChangedHandler;
                Socket.OnMessageReceived   -= OnMessageReceivedHandler;
                Socket.Close();
                Socket = null;

                // Force le dispatch de la déconnexion
                OnConnectChanged?.Invoke(this, EventArgs.Empty);
            }
        }
Example #2
0
 private void OnConnectionChangedHandler(object sender, EventArgs e)
 {
     OnConnectChanged?.Invoke(this, EventArgs.Empty);
 }
        private async void StatusLoop()
        {
            try
            {
                Dictionary <string, DateTime> exceptions = new Dictionary <string, DateTime>();
                bool connected = false;

                while (!statusToken.IsCancellationRequested && Running)
                {
                    try
                    {
                        this.connected = tcp.Client.IsConnected();

                        if (connected != this.connected)
                        {
                            connected = this.connected;
                            OnConnectChanged?.Invoke(this.connected);
                        }

                        if (!this.connected)
                        {
                            using (Ping ping = new Ping())
                            {
                                PingReply reply = ping.Send(Host);

                                if (reply.Status == IPStatus.Success)
                                {
                                    CreateClient();
                                }
                            }

                            continue;
                        }

                        OnOutputReceived?.Invoke(GetOutput(OutputStartAddress, OutputChannelNumber));

                        OnInputReceived?.Invoke(GetInput(InputStartAddress, InputChannelNumber));
                    }
                    catch (Exception ex)
                    {
                        bool writable = false;
                        if (!exceptions.ContainsKey(ex.Message))
                        {
                            exceptions.Add(ex.Message, DateTime.Now);
                            writable = true;
                        }
                        else
                        {
                            if ((DateTime.Now - exceptions[ex.Message]) < TimeSpan.FromSeconds(60))
                            {
                                continue;
                            }

                            exceptions[ex.Message] = DateTime.Now;
                            writable = true;
                        }

                        if (writable)
                        {
                            OnException?.Invoke(ex);
                        }
                    }
                    finally
                    {
                        if (!statusToken.IsCancellationRequested && Running)
                        {
                            await Task.Delay(DelayTime);
                        }
                    }
                }
            }
            catch (OperationCanceledException) { }
            catch (Exception ex)
            {
                OnException?.Invoke(ex);
            }

            if (Running)
            {
                BeginStatusListener();
            }
        }