Пример #1
0
        /// <summary>
        /// Listen for data coming from the TPI
        /// </summary>
        private void ListenForData()
        {
            // Listen forever
            while (true)
            {
                EnsureConnection();

                string[]      lines;
                NetworkStream clientStream = null;
                try
                {
                    clientStream = _client.GetStream();
                    var message = new byte[4096];

                    while (!_shutdown)
                    {
                        var bytesRead = 0;

                        try
                        {
                            //blocks until a client sends a message
                            bytesRead = clientStream.Read(message, 0, 4096);
                        }
                        catch (Exception ex)
                        {
                            //a socket error has occured
                            MyLogger.LogError($"ListenData Exception: {MyLogger.ExMsg(ex)}");

                            // Wait 5 seconds before trying to re-connect
                            MyLogger.LogInfo("Waiting 5 seconds before trying to re-connect");
                            Thread.Sleep(5000);

                            break;
                        }

                        if (bytesRead == 0)
                        {
                            //the client has disconnected from the server
                            break;
                        }

                        //message has successfully been received
                        var encoder = new ASCIIEncoding();


                        var value = encoder.GetString(message, 0, bytesRead);
                        lines = Regex.Split(value, "\r\n");
                        foreach (string line in lines)
                        {
                            if (!String.IsNullOrEmpty(line))
                            {
                                var r = new TpiResponse(line);

                                MyLogger.LogInfo($"< {((int)r.Command):D3}:{r.Command.ToString().PadRight(20)} - {r.Data}");

                                if (ResponseReceived != null)
                                {
                                    ResponseReceived(this, r);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MyLogger.LogError(MyLogger.ExMsg(ex));
                }
            }
        }
Пример #2
0
        private void EnsureConnection()
        {
            do
            {
                lock (thisLock)
                {
                    try
                    {
                        // Make sure we have not already connnected on another thread
                        if (_client != null && _client.Connected)
                        {
                            return;
                        }

                        // Clean up anything that is outstanding
                        if (_client != null)
                        {
                            //_client.Client.Shutdown(SocketShutdown.Both);
                            _client.Dispose();
                        }

                        MyLogger.LogInfo("Reconnecting the socket");
                        _client = new TcpClient();
                        Task ca = _client.ConnectAsync(Host, Port);
                        if (ca.Wait(15000) == false)
                        {
                            MyLogger.LogError($"ERROR: Could not connect within 15 seconds to {Host} on Port {Port}");
                        }

                        // Return if we connected properly
                        if (_client.Connected)
                        {
                            return;
                        }

                        _client.Dispose();
                        _client = null;
                        MyLogger.LogError($"ERROR: Could not connect to {Host} on Port {Port}");
                    }
                    catch (Exception ex)
                    {
                        MyLogger.LogError($"ERROR: trying to connect {Host} on Port {Port} - {MyLogger.ExMsg(ex)}");
                        _client = null;
                    }

                    // Wait 5 seconds before trying to re-connect
                    MyLogger.LogInfo("Waiting 5 seconds before trying to re-connect");
                    Thread.Sleep(5000);
                }
            } while (_client == null);
        }