From 43b0d5870d528f23ecd6aeceb6cfd4325188b46f Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期四, 01 七月 2021 15:50:43 +0800
Subject: [PATCH] Revert "1"

---
 HDL_ON/DAL/DriverLayer/Control_TcpClient.cs |  134 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 134 insertions(+), 0 deletions(-)

diff --git a/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs b/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs
new file mode 100644
index 0000000..aa8aeff
--- /dev/null
+++ b/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs
@@ -0,0 +1,134 @@
+锘縰sing System;
+using System.Net;
+using System.Net.Sockets;
+using System.Threading.Tasks;
+
+namespace HDL_ON.DriverLayer
+{
+    public class Control_TcpClient
+    {
+
+        //澹版槑IP锛岀鍙o紝鍜屼竴涓敤鏉ヨ繛鎺ョ殑Socket
+        public string _ip;
+
+        private TcpClient _tcpClient;
+
+        //鍒涘缓涓�涓鎵橈紝鐢ㄦ潵婊¤冻鍏朵粬绫昏皟鐢�
+        //public delegate void DelegateMessage(string str);
+        public Action<string> ReceiveEvent;
+
+        /// <summary>
+        /// 鏋勯�犲嚱鏁�
+        /// </summary>
+        public Control_TcpClient(string serverIp)
+        {
+            _ip = serverIp;
+        }
+
+        //TCP杩炴帴
+        public bool Connect(int _port = 8586)
+        {
+            if (string.IsNullOrEmpty(_ip) || _port == 0)
+            {
+                return false;
+            }
+            _tcpClient = new TcpClient();
+            try
+            {
+                _tcpClient.Connect(IPAddress.Parse(_ip), _port);
+                Task.Run(new Action(ReceiveMessage));//寮�鍚嚎绋嬶紝涓嶅仠鎺ユ敹娑堟伅
+                MainPage.Log($"鎵撳紑tcp client{_ip}:{_port}");
+            }
+            catch (Exception e)
+            {
+                MainPage.Log(e.Message);
+                throw;
+            }
+            return true;//杩斿洖杩炴帴鐘舵��
+        }
+        /// <summary>
+        /// 鍏抽棴杩炴帴
+        /// </summary>
+        /// <returns></returns>
+        public bool Close()
+        {
+            if (_tcpClient == null)
+                return true;
+            _tcpClient.Close();
+            _tcpClient = null;
+            return true;
+        }
+
+        /// <summary>
+        /// 鍙戦�佹秷鎭�
+        /// </summary>
+        /// <param name="bytes">闇�瑕佸彂閫佺殑瀛楄妭</param>
+        public void SendMessage(byte[] bytes)
+        {
+            NetworkStream networkStream = _tcpClient.GetStream();
+            if (networkStream.CanWrite)
+            {
+                networkStream.Write(bytes, 0, bytes.Length);
+            }
+            //networkStream.Close();
+        }
+
+        /// <summary>
+        /// 鑾峰彇鍒楄〃鏁版嵁鍥炶皟鏂规硶
+        /// </summary>
+        public Action<string> GetListResponseAction;
+        //鎺ユ敹娑堟伅
+        public void ReceiveMessage()
+        {
+            NetworkStream networkStream = _tcpClient.GetStream();
+            while (true)
+            {
+                // 瀹氫箟涓�涓�2M鐨勭紦瀛樺尯锛�
+                byte[] arrMsgRec = new byte[1024 * 1024 * 2];
+                int size = networkStream.Read(arrMsgRec, 0, arrMsgRec.Length);
+                var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, arrMsgRec.Length);
+                if (!string.IsNullOrEmpty(tcpDataString))
+                {
+                    ReceiveEvent?.Invoke(tcpDataString);
+                }
+
+                //// 灏嗘帴鍙楀埌鐨勬暟鎹瓨鍏ュ埌杈撳叆  arrMsgRec涓紱
+                //int length = -1;
+                //try
+                //{
+                //    length = socketClient.Receive(arrMsgRec); // 鎺ユ敹鏁版嵁锛屽苟杩斿洖鏁版嵁鐨勯暱搴︼紱
+                //}
+                //catch (Exception ex)
+                //{
+                //    MainPage.Log($"tcpListener  error 1 :  {ex.Message}");
+
+                //    Flag_Receive = false;
+                //    // 浠庨�氫俊绾跨▼闆嗗悎涓垹闄よ涓柇杩炴帴鐨勯�氫俊绾跨▼瀵硅薄锛�
+                //    string keystr = socketClient.RemoteEndPoint.ToString();
+                //    dic_ClientSocket.Remove(keystr);//鍒犻櫎瀹㈡埛绔瓧鍏镐腑璇ocket
+                //    dic_ClientThread[keystr].Abort();//鍏抽棴绾跨▼
+                //    dic_ClientThread.Remove(keystr);//鍒犻櫎瀛楀吀涓绾跨▼
+
+                //    tcpClient = null;
+                //    socketClient = null;
+                //    break;
+                //}
+                //byte[] buf = new byte[length];
+                //Array.Copy(arrMsgRec, buf, length);
+                //lock (tcpClient.m_Buffer)
+                //{
+                //    var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);
+                //    if (!string.IsNullOrEmpty(tcpDataString))
+                //    {
+                //        ReceiveEvent?.Invoke(tcpDataString);
+                //    }
+
+                //    MainPage.Log($"鎺ユ敹鏈嶅姟绔暟鎹�:{tcpDataString}");
+                //}
+
+            }
+        }
+
+
+    }
+}
\ No newline at end of file

--
Gitblit v1.8.0