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 | 347 +++++++++++++++++++++++++++++++++++++++++++++------------
1 files changed, 275 insertions(+), 72 deletions(-)
diff --git a/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs b/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs
index d6f61f1..84e305b 100644
--- a/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs
+++ b/HDL_ON/DAL/DriverLayer/Control_TcpClient.cs
@@ -6,6 +6,7 @@
using System.Threading.Tasks;
using HDL_ON.Entity;
using Newtonsoft.Json;
+using Newtonsoft.Json.Linq;
namespace HDL_ON.DriverLayer
{
@@ -15,7 +16,25 @@
//澹版槑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);
@@ -25,10 +44,47 @@
/// 杩炴帴娆℃暟
/// </summary>
private int reconnectIndex = 0;
+
+ private bool _isConnected = false;
/// <summary>
/// 鏄惁杩炴帴鎴愬姛
/// </summary>
- public bool isConnected = false;
+ 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>
@@ -53,14 +109,14 @@
try
{
_tcpClient.Connect(IPAddress.Parse(_ip), 8586);
- Task.Run(new Action(ReceiveMessage));//寮�鍚嚎绋嬶紝涓嶅仠鎺ユ敹娑堟伅
+ 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;//杩斿洖杩炴帴鐘舵��
}
@@ -74,42 +130,81 @@
/// </summary>
public void Connect()
{
- if(connectThread == null)
+ return;
+ lock (lockObj)
{
- connectThread = new Thread(() => {
- while (Control.Ins.GatewayOnline_Local)
+ if (isConnected)
+ {
+ return;
+ }
+ if (connectThread == null)
+ {
+ connectThread = new Thread(() =>
{
- if (_tcpClient == null)
+ while (Control.Ins.GatewayOnline_Local && !isConnected)
{
- ConnectToTcp();
- }
- else
- {
- if (!_tcpClient.Connected)
+ if (_tcpClient == null)
{
- try
+ ConnectToTcp();
+ }
+ else
+ {
+ if (!_tcpClient.Connected)
{
- //_tcpClient.ReceiveTimeout =
- _tcpClient.Connect(IPAddress.Parse(_ip), 8586);
- Task.Run(new Action(ReceiveMessage));//寮�鍚嚎绋嬶紝涓嶅仠鎺ユ敹娑堟伅
- }
- catch (Exception ex)
- {
- MainPage.Log($"tcp閲嶈繛寮傚父:{ex.Message}");
- _tcpClient.Close();
- _tcpClient = null;
+ try
+ {
+ //_tcpClient.ReceiveTimeout =
+ _tcpClient.Connect(IPAddress.Parse(_ip), 8586);
+ ReceiveMessage();//寮�鍚嚎绋嬶紝涓嶅仠鎺ユ敹娑堟伅
+ isConnected = true;
+ }
+ catch (Exception ex)
+ {
+ MainPage.Log($"tcp閲嶈繛寮傚父:{ex.Message}");
+ }
}
}
+ Thread.Sleep(1000);
+
+
}
- Thread.Sleep(1000);
-
-
+ });
+ connectThread.Start();
+ }
+ else
+ {
+ if (!isConnected)
+ {
+ try
+ {
+ connectThread?.Abort();
+ }
+ catch { }
+ finally
+ {
+ connectThread = null;
+ }
+ Connect();
}
- });
- connectThread.Start();
+ }
}
}
-
+ /// <summary>
+ /// 閲嶈繛
+ /// </summary>
+ public void Reconect()
+ {
+ if (_tcpClient == null)
+ {
+ Connect();
+ }
+ else
+ {
+ _tcpClient.Close();
+ _tcpClient = null;
+ Connect();
+ }
+ }
/// <summary>
/// 鍏抽棴杩炴帴
@@ -130,29 +225,97 @@
/// <param name="bytes">闇�瑕佸彂閫佺殑瀛楄妭</param>
public void SendMessage(byte[] bytes)
{
- if (_tcpClient.GetStream().CanWrite)
+ return;
+#if __IOS__
+
+#endif
+ if (heartBeatLogIdList.Count > 3)
{
- _tcpClient.GetStream().Write(bytes, 0, bytes.Length);
+ try
+ {
+ MainPage.Log("蹇冭烦澶氭鏈洖澶嶏紝鏂紑tcp杩炴帴");
+ heartBeatLogIdList.Clear();
+ isConnected = false;
+ Reconect();
+ return;
+ }catch (Exception ex)
+ {
+ MainPage.Log($"閲嶈繛tcp寮傚父:{ex.Message}");
+ }
+ }
+ 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>
private Thread heartBeatThread;
+
private DateTime heartBeatTime;
public void HeartBeat()
{
- if(heartBeatThread == null)
+ return;
+ lock (lockObj)
{
- heartBeatThread = new Thread(() => {
- if(_tcpClient.Connected&&10 *1000 <(System.DateTime.Now - heartBeatTime).TotalMilliseconds)
+ if (heartBeatThread == null)
+ {
+ MainPage.Log($"蹇冭烦鍖呯嚎绋嬪惎鍔�");
+ heartBeatThread = new Thread(() =>
{
- var sendBytes = Control.Ins.ConvertSendBodyData(CommunicationTopic.ct.HeartBeat, "");
- SendMessage(sendBytes);
+ 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();
}
- Thread.Sleep(100);
- });
- heartBeatThread.Start();
+ catch (Exception ex)
+ {
+ MainPage.Log($"鍚姩蹇冭烦绾跨▼锛岄噸鍚嚎绋嬪紓甯�:{ex.Message}");
+ }
+ finally
+ {
+ if (heartBeatThread != null)
+ {
+ heartBeatThread = null;
+ }
+ HeartBeat();
+ }
+ }
}
}
/// <summary>
@@ -160,49 +323,89 @@
/// </summary>
private Thread receiveThread;
+ private object lockObj = new object();
+
//鎺ユ敹娑堟伅
public void ReceiveMessage()
{
- if(receiveThread == null)
+ lock (lockObj)
{
- receiveThread = new Thread(() => {
- while (true)
+ if (receiveThread == null)
+ {
+ receiveThread = new Thread(() =>
{
- if(_tcpClient == null)
+ try
{
- receiveThread.Abort();
- receiveThread = null;
- return;
- }
- if (!_tcpClient.Connected)
+ 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瀹㈡埛绔柇寮�浜嗚繛鎺�...");
- receiveThread.Abort();
- receiveThread = null;
- isConnected = false;
- return;
+ MainPage.Log($"tcp ReceiveMessage error :{ex.Message}");
}
-
- // 瀹氫箟涓�涓�2M鐨勭紦瀛樺尯锛�
- byte[] arrMsgRec = new byte[1024 * 1024 * 2];
- int size = _tcpClient.GetStream().Read(arrMsgRec, 0, arrMsgRec.Length);
- var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, arrMsgRec.Length);
-
-
- if (!string.IsNullOrEmpty(tcpDataString))
- {
- MainPage.Log($"灞�鍩熺綉tcp鏁版嵁鎺ユ敹");
- Control.Ins.ConvertReceiveData(arrMsgRec, null);
-
- }
+ });
+ //receiveThread.IsBackground = true;
+ receiveThread.Start();
+ }
+ else
+ {
+ try
+ {
+ receiveThread?.Abort();
}
-
- });
- //receiveThread.IsBackground = true;
- receiveThread.Start();
+ catch { }
+ finally
+ {
+ receiveThread = null;
+ ReceiveMessage();
+ }
+ }
}
}
-
}
}
\ No newline at end of file
--
Gitblit v1.8.0