Example #1
0
 public LineTimeoutManager(VaiListener listener)
 {
     this.timer    = new TimerOnce(defaultTimeoutMs, OnTimerOnceEvent);
     this.Listener = listener;
 }
Example #2
0
        private async void AcceptClientsAsync()
        {
            TcpClient   tcpClient = null;
            VaiListener listener  = null;

            try
            {
                while (KeepServerRunning && !cancellation.IsCancellationRequested)
                {
                    // If you don't check for pending messages,
                    // and there have been at least one active connection,
                    // AcceptTcpClientAsync will throw a "cannot used disposed object exception"
                    // when tcpServer stops.
                    if (!socketServer.Pending())
                    {
                        Thread.Sleep(0);
                        continue;
                    }

                    //Accept the pending client connection and return a TcpClient object initialized for communication.
                    tcpClient = await socketServer.AcceptTcpClientAsync().WithWaitCancellation(cancellation.Token);

                    var tcpIpClient = new VaiTcpIpClient(tcpClient);
                    clients.Add(tcpIpClient);

                    if (enableClientListening)
                    {
                        var portDataParser = new PortDataParser(tcpIpClient);
                        listener = new VaiListener(tcpIpClient, portDataParser);
                        Debug.WriteLine("Adding listener " + tcpIpClient.RemoteSocketName);
                        listeners.Add(listener);
                        var t1 = new Task(() => listener.StartListening());
                        t1.Start();
                    }
                }
            }
            // this exception may occur if there have been open connections
            catch (ObjectDisposedException ex)
            {
                Debug.WriteLine("Exception in CheckForClient." + ex.Message);
                Debug.Assert(true, ex.Message);
            }
            // this exception may occur if there have been open connections
            catch (OperationCanceledException ex)
            {
                Debug.Assert(true, ex.Message);
                Debug.WriteLine("Exception in CheckForClient." + ex.Message);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception in CheckForClient." + ex.Message);
                Debug.Assert(false, ex.Message);
            }
            finally
            {
                listener?.Dispose();
                if (!KeepServerRunning)
                {
                    Debug.WriteLine("CheckForClient finally. keepServerRunning:" + KeepServerRunning);
                }
            }
        }