public bool QueueMsg(CmndMsg msg) { if (this._isConnected != true) { return(false); } msgStream.Enqueue(msg); return(true); }
private void SendMsg(string cmd, int delay) { var msg = new CmndMsg { DelayAfter = delay, Cmd = cmd }; if (client.QueueMsg(msg) == false) { Ellipse_Fill(Connection, '0'); } }
//private TcpClient client; //private NetworkStream clientStream; private void ClientTask() { while (true) { TcpClient client; NetworkStream clientStream; try { client = new TcpClient { ReceiveTimeout = 10000, SendTimeout = 10000 }; client.Connect(_hostName, _hostPort); clientStream = client.GetStream(); Send_ConnectionState("ThreadStart", true); while (true) { while (msgStream.IsEmpty) { Thread.Sleep(10); } if (this._isConnected == true) { CmndMsg msg = new CmndMsg(); if (msgStream.TryDequeue(out msg)) { long tFrom; Byte[] data = System.Text.Encoding.ASCII.GetBytes(msg.Cmd + "\r\n\0"); tFrom = DateTime.Now.Ticks; clientStream.Write(data, 0, data.Length); Thread.Sleep(1); var responseData = new StringBuilder(); do { var numberOfBytesRead = clientStream.Read(data, 0, data.Length); responseData.AppendFormat("{0}", Encoding.ASCII.GetString(data, 0, numberOfBytesRead)); }while (clientStream.DataAvailable); var tps = TimeSpan.FromTicks(DateTime.Now.Ticks - tFrom); SendNewLogMsg(tps, msg.Cmd, responseData.ToString()); } } } } catch (ArgumentNullException e) { Send_ConnectionState(e.Message.ToString(), false); client = null; clientStream = null; } catch (ArgumentOutOfRangeException e) { Send_ConnectionState(e.Message.ToString(), false); client = null; clientStream = null; } catch (SocketException e) { Send_ConnectionState(e.Message.ToString(), false); client = null; clientStream = null; } catch (IOException e) { Send_ConnectionState(e.Message.ToString(), false); client = null; clientStream = null; } catch (ObjectDisposedException e) { Send_ConnectionState(e.Message.ToString(), false); client = null; clientStream = null; } catch (Exception e) { Send_ConnectionState(e.Message.ToString(), false); client = null; clientStream = null; } finally { Thread.Sleep(1000); } } }