Example #1
0
        private void RemoveClient(string clientName)
        {
            try
            {
                VaiTcpIpClient clientToRemove = null;
                foreach (var client in clients)
                {
                    if (string.Compare(client.RemoteSocketName, clientName, StringComparison.Ordinal) == 0)
                    {
                        clientToRemove = client;
                        break;
                    }
                }

                if (clientToRemove == null)
                {
                    return;
                }

                Debug.WriteLine(DateTimeEx.NowToStringWithMs + "\tRemoving client " + clientName);

                WriteToLog("Removing client:" + clientName);

                lock (clientLock)
                {
                    clients.Remove(clientToRemove);
                    Task.Delay(10).Wait();
                    clientToRemove.Dispose();
                }
                Debug.WriteLine(DateTimeEx.NowToStringWithMs + "\tClient removed: " + clientName);
                WriteToLog(clients.Count + " Client removed:" + clientName);
            }
            catch (Exception ex)
            {
                Debug.Assert(true, ex.Message);
                ExceptionHandler.HandleException(ex, "RemoveClient");
            }
        }
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);
                }
            }
        }