Example #1
0
 public VaiListener(INamedPort namedPort, PortDataParser portDataParser, int lineTimeoutSeconds = 300)
 {
     NamedPort          = namedPort;
     PortDataParser     = portDataParser;
     lineTimeoutManager = new LineTimeoutManager(this);
     lineTimeoutManager.LineIdleTimeout = TimeSpan.FromSeconds(lineTimeoutSeconds);
 }
Example #2
0
        private void Listen()
        {
            while (IsListening)
            {
                // use Monitor to prevent possible exceptions when Disposing an active Listener
                if (Monitor.TryEnter(NamedPort))
                {
                    try
                    {
                        if (NamedPort.HasDataAvailable())
                        {
                            string msg = NamedPort.Read();
                            lineTimeoutManager.LastDataReceived = DateTimeEx.Now;

                            PortDataParser.AppendAndParse(msg);
                        }
                    }
                    finally {
                        Monitor.Exit(NamedPort);
                    }
                }
            }
        }
Example #3
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);
                }
            }
        }