From fefffbc9451499013b7af2a15fac0ccc3b394364 Mon Sep 17 00:00:00 2001
From: wxr <464027401@qq.com>
Date: 星期一, 15 四月 2024 16:31:25 +0800
Subject: [PATCH] 去掉Siri先
---
HDL_ON/DAL/DriverLayer/Control_TcpClient.cs | 397 ++++++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 337 insertions(+), 60 deletions(-)
diff --git a/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs b/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs
index aa8aeff..84e305b 100644
--- a/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs
+++ b/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs
@@ -1,7 +1,12 @@
锘縰sing System;
+using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
+using System.Threading;
using System.Threading.Tasks;
+using HDL_ON.Entity;
+using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
namespace HDL_ON.DriverLayer
{
@@ -11,11 +16,76 @@
//澹版槑IP锛岀鍙o紝鍜屼竴涓敤鏉ヨ繛鎺ョ殑Socket
public string _ip;
- private TcpClient _tcpClient;
+ private TcpClient _clinet;
+
+ private TcpClient _tcpClient {
+ get
+ {
+ return _clinet;
+ }
+ set
+ {
+ _clinet = value;
+ if (_clinet == null) {
+ if(connectThread!= null)
+ {
+ connectThread.Abort();
+ connectThread = null;
+ }
+ }
+ }
+ }
//鍒涘缓涓�涓鎵橈紝鐢ㄦ潵婊¤冻鍏朵粬绫昏皟鐢�
//public delegate void DelegateMessage(string str);
public Action<string> ReceiveEvent;
+
+ /// <summary>
+ /// 杩炴帴娆℃暟
+ /// </summary>
+ private int reconnectIndex = 0;
+
+ private bool _isConnected = false;
+ /// <summary>
+ /// 鏄惁杩炴帴鎴愬姛
+ /// </summary>
+ public bool isConnected {
+ get
+ {
+ return _isConnected;
+ }
+ set
+ {
+ _isConnected = value;
+ if (value)
+ {
+ Control.Ins.LoginGateway();
+ HeartBeat();
+ }
+ else
+ {
+ try
+ {
+ _tcpClient.Close();
+ _tcpClient = null;
+ }
+ catch (Exception)
+ {
+
+ }
+ }
+ }
+ }
+
+ private List<string> heartBeatLogIdList = new List<string>();
+ /// <summary>
+ /// 蹇冭烦璁板綍
+ /// </summary>
+ public void ClearHeartBeatLog()
+ {
+ heartBeatLogIdList.Clear();
+ }
+
/// <summary>
/// 鏋勯�犲嚱鏁�
@@ -26,26 +96,116 @@
}
//TCP杩炴帴
- public bool Connect(int _port = 8586)
+ private bool ConnectToTcp( )
{
- if (string.IsNullOrEmpty(_ip) || _port == 0)
+ if (string.IsNullOrEmpty(_ip) )
{
return false;
}
- _tcpClient = new TcpClient();
+ if (_tcpClient == null)
+ {
+ _tcpClient = new TcpClient();
+ }
try
{
- _tcpClient.Connect(IPAddress.Parse(_ip), _port);
- Task.Run(new Action(ReceiveMessage));//寮�鍚嚎绋嬶紝涓嶅仠鎺ユ敹娑堟伅
- MainPage.Log($"鎵撳紑tcp client{_ip}:{_port}");
+ _tcpClient.Connect(IPAddress.Parse(_ip), 8586);
+ ReceiveMessage();//寮�鍚嚎绋嬶紝涓嶅仠鎺ユ敹娑堟伅
+ MainPage.Log($"鎵撳紑tcp client{_ip}:8586");
+ isConnected = true;
}
catch (Exception e)
{
- MainPage.Log(e.Message);
- throw;
+ MainPage.Log($"鎵撳紑tcp寮傚父锛�"+e.Message);
+ return false;
}
return true;//杩斿洖杩炴帴鐘舵��
}
+ /// <summary>
+ /// 杩炴帴tcp绾跨▼
+ /// </summary>
+ private Thread connectThread;
+
+ /// <summary>
+ /// 杩炴帴绾跨▼
+ /// </summary>
+ public void Connect()
+ {
+ return;
+ lock (lockObj)
+ {
+ if (isConnected)
+ {
+ return;
+ }
+ if (connectThread == null)
+ {
+ connectThread = new Thread(() =>
+ {
+ while (Control.Ins.GatewayOnline_Local && !isConnected)
+ {
+ if (_tcpClient == null)
+ {
+ ConnectToTcp();
+ }
+ else
+ {
+ if (!_tcpClient.Connected)
+ {
+ try
+ {
+ //_tcpClient.ReceiveTimeout =
+ _tcpClient.Connect(IPAddress.Parse(_ip), 8586);
+ ReceiveMessage();//寮�鍚嚎绋嬶紝涓嶅仠鎺ユ敹娑堟伅
+ isConnected = true;
+ }
+ catch (Exception ex)
+ {
+ MainPage.Log($"tcp閲嶈繛寮傚父:{ex.Message}");
+ }
+ }
+ }
+ Thread.Sleep(1000);
+
+
+ }
+ });
+ connectThread.Start();
+ }
+ else
+ {
+ if (!isConnected)
+ {
+ try
+ {
+ connectThread?.Abort();
+ }
+ catch { }
+ finally
+ {
+ connectThread = null;
+ }
+ Connect();
+ }
+ }
+ }
+ }
+ /// <summary>
+ /// 閲嶈繛
+ /// </summary>
+ public void Reconect()
+ {
+ if (_tcpClient == null)
+ {
+ Connect();
+ }
+ else
+ {
+ _tcpClient.Close();
+ _tcpClient = null;
+ Connect();
+ }
+ }
+
/// <summary>
/// 鍏抽棴杩炴帴
/// </summary>
@@ -65,70 +225,187 @@
/// <param name="bytes">闇�瑕佸彂閫佺殑瀛楄妭</param>
public void SendMessage(byte[] bytes)
{
- NetworkStream networkStream = _tcpClient.GetStream();
- if (networkStream.CanWrite)
+ return;
+#if __IOS__
+
+#endif
+ if (heartBeatLogIdList.Count > 3)
{
- networkStream.Write(bytes, 0, bytes.Length);
+ try
+ {
+ MainPage.Log("蹇冭烦澶氭鏈洖澶嶏紝鏂紑tcp杩炴帴");
+ heartBeatLogIdList.Clear();
+ isConnected = false;
+ Reconect();
+ return;
+ }catch (Exception ex)
+ {
+ MainPage.Log($"閲嶈繛tcp寮傚父:{ex.Message}");
+ }
}
- //networkStream.Close();
+ try
+ {
+ if (_tcpClient == null)
+ {
+ return;
+ }
+ if (!_tcpClient.Connected)
+ {
+ return;
+ }
+ if (_tcpClient.GetStream().CanWrite&& isConnected)
+ {
+ _tcpClient.GetStream().Write(bytes, 0, bytes.Length);
+ }
+ }catch(Exception ex)
+ {
+ MainPage.Log($"tcp瀹㈡埛绔彂閫佹暟鎹紓甯�:{ex.Message}");
+ isConnected = false;
+ }
}
-
/// <summary>
- /// 鑾峰彇鍒楄〃鏁版嵁鍥炶皟鏂规硶
+ /// 蹇冭烦鍖呯嚎绋�
/// </summary>
- public Action<string> GetListResponseAction;
+ private Thread heartBeatThread;
+
+ private DateTime heartBeatTime;
+ public void HeartBeat()
+ {
+ return;
+ lock (lockObj)
+ {
+ if (heartBeatThread == null)
+ {
+ MainPage.Log($"蹇冭烦鍖呯嚎绋嬪惎鍔�");
+ heartBeatThread = new Thread(() =>
+ {
+ while (isConnected)
+ {
+ if (_tcpClient.Connected && 10 * 1000 < (System.DateTime.Now - heartBeatTime).TotalMilliseconds)
+ {
+ string msgId = Control.Ins.msg_id.ToString();
+ heartBeatLogIdList.Add(msgId);
+ var sendJob = new JObject { { "id", Control.Ins.msg_id.ToString() }, { "time_stamp", Utlis.GetTimestamp() } };
+ var bodyString = JsonConvert.SerializeObject(sendJob);
+
+ var sendBytes = Control.Ins.ConvertSendBodyData(CommunicationTopic.ct.HeartBeat, bodyString, false);
+ SendMessage(sendBytes);
+ heartBeatTime = DateTime.Now;
+ }
+ Thread.Sleep(100);
+ }
+ });
+ heartBeatThread.Start();
+ }
+ else
+ {
+ try
+ {
+ heartBeatThread?.Abort();
+ }
+ catch (Exception ex)
+ {
+ MainPage.Log($"鍚姩蹇冭烦绾跨▼锛岄噸鍚嚎绋嬪紓甯�:{ex.Message}");
+ }
+ finally
+ {
+ if (heartBeatThread != null)
+ {
+ heartBeatThread = null;
+ }
+ HeartBeat();
+ }
+ }
+ }
+ }
+ /// <summary>
+ /// 鎺ユ敹鏁版嵁绾跨▼
+ /// </summary>
+ private Thread receiveThread;
+
+ private object lockObj = new object();
+
//鎺ユ敹娑堟伅
public void ReceiveMessage()
{
- NetworkStream networkStream = _tcpClient.GetStream();
- while (true)
+ lock (lockObj)
{
- // 瀹氫箟涓�涓�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))
+ if (receiveThread == null)
{
- ReceiveEvent?.Invoke(tcpDataString);
+ receiveThread = new Thread(() =>
+ {
+ try
+ {
+ while (isConnected)
+ {
+ if (_tcpClient == null)
+ {
+ try
+ {
+ receiveThread?.Abort();
+ }
+ catch { }
+ finally
+ {
+ receiveThread = null;
+ }
+ return;
+ }
+ if (!_tcpClient.Connected)
+ {
+ MainPage.Log("tcp瀹㈡埛绔柇寮�浜嗚繛鎺�...");
+ isConnected = false;
+ return;
+ }
+ try
+ {
+ // 瀹氫箟涓�涓�2M鐨勭紦瀛樺尯锛�
+ byte[] arrMsgRec = new byte[1024 * 1024 * 2];
+ try
+ {
+ int size = _tcpClient.GetStream().Read(arrMsgRec, 0, arrMsgRec.Length);
+ }
+ catch (Exception ex)
+ {
+ isConnected = false;
+ MainPage.Log($"灞�鍩熺綉tcp鏁版嵁鎺ユ敹寮傚父:{ex.Message}");
+ return;
+ }
+ var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, arrMsgRec.Length);
+
+
+ if (!string.IsNullOrEmpty(tcpDataString))
+ {
+ MainPage.Log($"灞�鍩熺綉tcp鏁版嵁鎺ユ敹");
+ Control.Ins.ConvertReceiveData(arrMsgRec, null);
+
+ }
+ }
+ catch (Exception) { }
+ }
+ }catch (Exception ex)
+ {
+ MainPage.Log($"tcp ReceiveMessage error :{ex.Message}");
+ }
+ });
+ //receiveThread.IsBackground = true;
+ receiveThread.Start();
}
-
- //// 灏嗘帴鍙楀埌鐨勬暟鎹瓨鍏ュ埌杈撳叆 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}");
- //}
-
+ else
+ {
+ try
+ {
+ receiveThread?.Abort();
+ }
+ catch { }
+ finally
+ {
+ receiveThread = null;
+ ReceiveMessage();
+ }
+ }
}
}
-
}
}
\ No newline at end of file
--
Gitblit v1.8.0