Commit 444cd4e0 张东亮

1

1 个父辈 6e0fc3cc
正在显示 52 个修改的文件 包含 60 行增加59 行删除
此文件类型无法预览
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16 # Visual Studio Version 17
VisualStudioVersion = 16.0.30523.141 VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AgvClientTest", "AgvClientTest\AgvClientTest.csproj", "{E1C0827A-FA12-49A9-AC71-6D3E6518754A}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AgvClientTest", "AgvClientTest\AgvClientTest.csproj", "{E1C0827A-FA12-49A9-AC71-6D3E6518754A}"
EndProject EndProject
......
...@@ -17,7 +17,7 @@ namespace Agv ...@@ -17,7 +17,7 @@ namespace Agv
/// <summary> /// <summary>
/// 节点信息解析方式:默认解析单个 /// 节点信息解析方式:默认解析单个
/// </summary> /// </summary>
public static bool EnDecodeSingleNode = true; public static bool EnDecodeSingleNode = false;
public static bool CheckHeadChar(byte[] head, out int starIdx) public static bool CheckHeadChar(byte[] head, out int starIdx)
{ {
starIdx = -1; starIdx = -1;
...@@ -115,7 +115,7 @@ namespace Agv ...@@ -115,7 +115,7 @@ namespace Agv
try try
{ {
string json = System.Text.Encoding.UTF8.GetString(buff); string json = System.Text.Encoding.UTF8.GetString(buff);
log.debug(string.Format("Decode[{0}]", json)); log.debug(string.Format("Decode【{0}】", json));
Node node = JsonHelper.DeserializeJsonToObject<Node>(json); Node node = JsonHelper.DeserializeJsonToObject<Node>(json);
return node; return node;
...@@ -136,7 +136,7 @@ namespace Agv ...@@ -136,7 +136,7 @@ namespace Agv
try try
{ {
string json = System.Text.Encoding.UTF8.GetString(buff); string json = System.Text.Encoding.UTF8.GetString(buff);
log.debug(string.Format("DecodeNodes[{0}]", json)); log.debug(string.Format("DecodeNodes【{0}】", json));
List<Node> node = JsonHelper.DeserializeJsonToList<Node>(json); List<Node> node = JsonHelper.DeserializeJsonToList<Node>(json);
return node; return node;
......
...@@ -29,7 +29,7 @@ namespace Agv ...@@ -29,7 +29,7 @@ namespace Agv
} }
public static void warn(string warn) public static void warn(string warn)
{ {
LOGGER.Warn(warn); //LOGGER.Warn(warn);
} }
} }
} }
此文件类型无法预览
此文件类型无法预览
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<log4net>
<appender name="Agv" type="log4net.Appender.RollingFileAppender">
<file value="logs/Agv.log"/>
<param name="Encoding" value="UTF-8"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyy-MM-dd"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date][%t][%c:%L]%-5p %m%n"/>
</layout>
</appender>
<root>
<level value="debug"/>
<appender-ref ref="Agv"/>
</root>
</log4net>
</configuration>
此文件的差异太大,无法显示。
...@@ -58,3 +58,4 @@ E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.csproj.Co ...@@ -58,3 +58,4 @@ E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.csproj.Co
E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.exe E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.exe
E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.pdb E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.pdb
E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.csproj.AssemblyReference.cache E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.csproj.AssemblyReference.cache
E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.csproj.SuggestedBindingRedirects.cache
...@@ -104,9 +104,6 @@ namespace Agv ...@@ -104,9 +104,6 @@ namespace Agv
} }
if (dictClient.Count > 0) dictClient.Clear(); if (dictClient.Count > 0) dictClient.Clear();
if (nodeMap.Count > 0)
nodeMap.Clear();
if (_server != null) if (_server != null)
{ {
_server.Close(); _server.Close();
...@@ -258,18 +255,20 @@ namespace Agv ...@@ -258,18 +255,20 @@ namespace Agv
IPEndPoint endPoint = (IPEndPoint)sokConnection.RemoteEndPoint; IPEndPoint endPoint = (IPEndPoint)sokConnection.RemoteEndPoint;
string ip = endPoint.Address.ToString(); string ip = endPoint.Address.ToString();
int port = endPoint.Port; int port = endPoint.Port;
Thread thr = new Thread(ListenNet);
Client client = new Client(sokConnection, thr, ip, endPoint.ToString());
thr.IsBackground = true;
if (dictClient.TryGetValue(ip, out Client client1)) if (dictClient.TryGetValue(ip, out Client client1))
{ {
log.info(string.Format($"{ip}重连 断开上一次连接:{client1.Endpoint}"));
Offline(client1); Offline(client1);
Thread.Sleep(2000); dictClient[ip] = client;
log.debug(string.Format("断开"));
} }
Thread thr = new Thread(ListenNet); else
Client client = new Client(sokConnection, thr, ip, endPoint.ToString());
thr.IsBackground = true;
dictClient.Add(ip, client); dictClient.Add(ip, client);
thr.Start(client); thr.Start(client);
log.info(string.Format("客户端[{0}]连接服务端[{1}]成功", endPoint.ToString(), ServerInfo)); log.info(string.Format("客户端[{0}]连接服务端[{1}]成功", endPoint.ToString(), ServerInfo));
Thread.Sleep(300);
} }
catch (SocketException) catch (SocketException)
{ {
...@@ -302,18 +301,21 @@ namespace Agv ...@@ -302,18 +301,21 @@ namespace Agv
if (dic.TryGetValue(endpoint, out surplusBuffer)) if (dic.TryGetValue(endpoint, out surplusBuffer))
{ {
byte[] curBuffer = surplusBuffer.Concat(bytes).ToArray();//拼接上一次剩余的包 byte[] curBuffer = surplusBuffer.Concat(bytes).ToArray();//拼接上一次剩余的包
if (Common.CheckHeadChar(bytes, out int startIdx))//检查是否存在指定包头识别字符 bytesRead = curBuffer.Length;
if (Common.CheckHeadChar(curBuffer, out int startIdx))//检查是否存在指定包头识别字符
{ {
byte[] tmp = new byte[bytesRead - startIdx]; byte[] tmp = new byte[bytesRead - startIdx];
Buffer.BlockCopy(bytes, startIdx, tmp, 0, bytesRead - startIdx); Buffer.BlockCopy(curBuffer, startIdx, tmp, 0, bytesRead - startIdx);
//更新会话ID 的最新字节 //更新会话ID 的最新字节
dic.TryUpdate(endpoint, tmp, surplusBuffer); dic.TryUpdate(endpoint, tmp, surplusBuffer);
log.debug($"更新缓存【{endpoint}】:【{Common.HexBuff(tmp)}】");
surplusBuffer = curBuffer;//同步 surplusBuffer = curBuffer;//同步
} }
else else
{ {
byte[] xbtye = null; byte[] xbtye = null;
dic.TryRemove(endpoint, out xbtye); dic.TryRemove(endpoint, out xbtye);
log.debug($"删除缓存【{endpoint}】:【{Common.HexBuff(xbtye)}】");
} }
} }
else else
...@@ -324,6 +326,7 @@ namespace Agv ...@@ -324,6 +326,7 @@ namespace Agv
byte[] tmp = new byte[bytesRead - startIdx]; byte[] tmp = new byte[bytesRead - startIdx];
Buffer.BlockCopy(bytes, startIdx, tmp, 0, bytesRead - startIdx); Buffer.BlockCopy(bytes, startIdx, tmp, 0, bytesRead - startIdx);
dic.TryAdd(endpoint, tmp); dic.TryAdd(endpoint, tmp);
log.debug($"添加缓存【{endpoint}】:【{Common.HexBuff(tmp)}】");
surplusBuffer = tmp;//同步 surplusBuffer = tmp;//同步
} }
else else
...@@ -345,7 +348,7 @@ namespace Agv ...@@ -345,7 +348,7 @@ namespace Agv
Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead); Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead);
dic.TryUpdate(endpoint, byteSub, surplusBuffer); dic.TryUpdate(endpoint, byteSub, surplusBuffer);
surplusBuffer = byteSub; surplusBuffer = byteSub;
log.debug($"更新缓存【{endpoint}】:【{Common.HexBuff(byteSub)}】");
totalLen = 0; totalLen = 0;
break; break;
...@@ -365,6 +368,7 @@ namespace Agv ...@@ -365,6 +368,7 @@ namespace Agv
byte[] byteSub = new byte[totalLen - haveRead]; byte[] byteSub = new byte[totalLen - haveRead];
Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead); Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead);
dic.TryUpdate(endpoint, byteSub, surplusBuffer); dic.TryUpdate(endpoint, byteSub, surplusBuffer);
log.debug($"更新缓存【{endpoint}】:【{Common.HexBuff(byteSub)}】");
surplusBuffer = byteSub; surplusBuffer = byteSub;
break; break;
} }
...@@ -374,14 +378,21 @@ namespace Agv ...@@ -374,14 +378,21 @@ namespace Agv
//String strc = Encoding.UTF8.GetString(surplusBuffer, haveRead + headSize, bodySize); //String strc = Encoding.UTF8.GetString(surplusBuffer, haveRead + headSize, bodySize);
byte[] resBytes = new byte[bodySize]; byte[] resBytes = new byte[bodySize];
Buffer.BlockCopy(surplusBuffer, haveRead + Common.headSize, resBytes, 0, bodySize); Buffer.BlockCopy(surplusBuffer, haveRead + Common.headSize, resBytes, 0, bodySize);
log.debug(string.Format("[Receive data from:{0}] -> {1}", endpoint, Common.HexBuff(resBytes))); log.debug(string.Format("[Parse data from:{0}] -> {1}", endpoint, Common.HexBuff(resBytes)));
DecodeNode(client, resBytes); if (!DecodeNode(client, resBytes))//解析失败,重新接收
{
//byte[] xbtye = null;
//dic.TryRemove(endpoint, out xbtye);
// log.error($"清除缓存【{endpoint}】:{Common.HexBuff(xbtye)}");
return;
}
//依次累加当前的数据包的长度 //依次累加当前的数据包的长度
haveRead = haveRead + Common.headSize + bodySize; haveRead = haveRead + Common.headSize + bodySize;
if (Common.headSize + bodySize == bytesRead)//如果当前接收的数据包长度正好等于缓冲区长度,则待拼接的不规则数据长度归0 if (Common.headSize + bodySize == bytesRead)//如果当前接收的数据包长度正好等于缓冲区长度,则待拼接的不规则数据长度归0
{ {
byte[] xbtye = null; byte[] xbtye = null;
dic.TryRemove(endpoint, out xbtye); dic.TryRemove(endpoint, out xbtye);
log.debug($"删除缓存【{endpoint}】:【{Common.HexBuff(xbtye)}】");
surplusBuffer = null;//设置空 回到原始状态 surplusBuffer = null;//设置空 回到原始状态
totalLen = 0;//清0 totalLen = 0;//清0
} }
...@@ -394,7 +405,7 @@ namespace Agv ...@@ -394,7 +405,7 @@ namespace Agv
} }
#endregion #endregion
private void DecodeNode(Client client, byte[] resultBytes) private bool DecodeNode(Client client, byte[] resultBytes)
{ {
//解码单个节点 //解码单个节点
if (Common.EnDecodeSingleNode) if (Common.EnDecodeSingleNode)
...@@ -402,7 +413,8 @@ namespace Agv ...@@ -402,7 +413,8 @@ namespace Agv
Node node = Common.Decode(resultBytes); Node node = Common.Decode(resultBytes);
if (node == null) if (node == null)
{ {
log.error("命令解析失败: " + Common.HexBuff(resultBytes)); //log.error("命令解析失败: " + Common.HexBuff(resultBytes));
return false;
} }
else else
{ {
...@@ -410,7 +422,7 @@ namespace Agv ...@@ -410,7 +422,7 @@ namespace Agv
int idx = client.nodeName.FindIndex(s => s == node.Name); int idx = client.nodeName.FindIndex(s => s == node.Name);
if (idx == -1) client.nodeName.Add(node.Name); if (idx == -1) client.nodeName.Add(node.Name);
UpdateNode(node); UpdateNode(node);
return true;
} }
} }
else else
...@@ -419,7 +431,8 @@ namespace Agv ...@@ -419,7 +431,8 @@ namespace Agv
List<Node> nodes = Common.DecodeNodes(resultBytes); List<Node> nodes = Common.DecodeNodes(resultBytes);
if (nodes == null) if (nodes == null)
{ {
log.error("命令解析失败: " + Common.HexBuff(resultBytes)); //log.error("命令解析失败: " + Common.HexBuff(resultBytes));
return false;
} }
else else
{ {
...@@ -430,6 +443,7 @@ namespace Agv ...@@ -430,6 +443,7 @@ namespace Agv
if (idx == -1) client.nodeName.Add(node.Name); if (idx == -1) client.nodeName.Add(node.Name);
} }
UpdateNodes(nodes); UpdateNodes(nodes);
return true;
} }
} }
} }
...@@ -465,6 +479,7 @@ namespace Agv ...@@ -465,6 +479,7 @@ namespace Agv
{ {
byte[] buff = new byte[length]; byte[] buff = new byte[length];
Array.Copy(arrMsgRec, 0, buff, 0, length); Array.Copy(arrMsgRec, 0, buff, 0, length);
log.debug(string.Format("[Receive data from:{0}] -> {1}", client.Endpoint, Common.HexBuff(buff)));
StickyBagHandle(client, buff); StickyBagHandle(client, buff);
} }
} }
...@@ -523,7 +538,6 @@ namespace Agv ...@@ -523,7 +538,6 @@ namespace Agv
try try
{ {
client.Loop = false; client.Loop = false;
dictClient.Remove(client.IP);
if (client.Socket != null) if (client.Socket != null)
{ {
client.Socket.Close(); client.Socket.Close();
...@@ -541,17 +555,14 @@ namespace Agv ...@@ -541,17 +555,14 @@ namespace Agv
nodeOnline[client.nodeName[i]] = false; nodeOnline[client.nodeName[i]] = false;
NodeOnline?.Invoke(client.nodeName[i], false); NodeOnline?.Invoke(client.nodeName[i], false);
} }
if (nodeMap.ContainsKey(client.nodeName[i]))
nodeMap.Remove(client.nodeName[i]);
} }
client.nodeName.Clear(); client.nodeName.Clear();
client.ListenNet.Abort(); client.ListenNet.Abort();
log.info(string.Format("关闭对客户端[{0}]的监听线程", client.Endpoint)); log.info(string.Format("关闭对客户端[{0}]的监听线程1", client.Endpoint));
} }
catch (ThreadAbortException) catch (ThreadAbortException)
{ {
log.error(string.Format("关闭对客户端[{0}]的监听线程", client.Endpoint)); log.error(string.Format("关闭对客户端[{0}]的监听线程1", client.Endpoint));
} }
} }
private void Offline(string clientKey) private void Offline(string clientKey)
...@@ -577,7 +588,7 @@ namespace Agv ...@@ -577,7 +588,7 @@ namespace Agv
client.nodeName.Clear(); client.nodeName.Clear();
client.ListenNet.Abort(); client.ListenNet.Abort();
client.Socket = null; client.Socket = null;
dictClient.Remove(client.IP); log.info(string.Format("关闭对客户端[{0}]的监听线程2", client.Endpoint));
} }
private void KeepClientLive() private void KeepClientLive()
...@@ -591,7 +602,8 @@ namespace Agv ...@@ -591,7 +602,8 @@ namespace Agv
Thread.Sleep(2000); Thread.Sleep(2000);
foreach (string client in dictClient.Keys) foreach (string client in dictClient.Keys)
{ {
Send(client,buff); SendLive(client, buff);
Thread.Sleep(1000);
} }
} }
catch (Exception ex) catch (Exception ex)
...@@ -612,19 +624,32 @@ namespace Agv ...@@ -612,19 +624,32 @@ namespace Agv
try try
{ {
dictClient[clientKey].Socket.Send(buff); dictClient[clientKey].Socket.Send(buff);
log.debug(string.Format("服务端[{0}]向客户端[{1}]发送消息:[{2}]", ServerInfo, clientKey, Common.HexBuff(buff)));
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
log.error("发送失败" + i + "次:" + ex.Message); log.error($"给{clientKey}发送失败" + i + "次:" + ex.Message);
} }
Thread.Sleep(100); Thread.Sleep(100);
} }
Offline(clientKey); Offline(clientKey);
return false; return false;
} }
private bool SendLive(string clientKey, byte[] buff)
{
try
{
if (dictClient[clientKey].Loop)
dictClient[clientKey].Socket.Send(buff);
return true;
}
catch (Exception ex)
{
log.error($"给{clientKey}发送心跳包失败" + ex.Message);
}
Offline(clientKey);
return false;
}
private class Client private class Client
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!