Commit 444cd4e0 张东亮

1

1 个父辈 6e0fc3cc
正在显示 52 个修改的文件 包含 61 行增加60 行删除
此文件类型无法预览

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30523.141
# Visual Studio Version 17
VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AgvClientTest", "AgvClientTest\AgvClientTest.csproj", "{E1C0827A-FA12-49A9-AC71-6D3E6518754A}"
EndProject
......
......@@ -17,7 +17,7 @@ namespace Agv
/// <summary>
/// 节点信息解析方式:默认解析单个
/// </summary>
public static bool EnDecodeSingleNode = true;
public static bool EnDecodeSingleNode = false;
public static bool CheckHeadChar(byte[] head, out int starIdx)
{
starIdx = -1;
......@@ -115,7 +115,7 @@ namespace Agv
try
{
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);
return node;
......@@ -136,7 +136,7 @@ namespace Agv
try
{
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);
return node;
......
......@@ -29,7 +29,7 @@ namespace Agv
}
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
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.csproj.AssemblyReference.cache
E:\Neotel\Projects\Gitee\AGV-Com\AgvClientTest\obj\Debug\AgvClientTest.csproj.SuggestedBindingRedirects.cache
......@@ -104,9 +104,6 @@ namespace Agv
}
if (dictClient.Count > 0) dictClient.Clear();
if (nodeMap.Count > 0)
nodeMap.Clear();
if (_server != null)
{
_server.Close();
......@@ -258,18 +255,20 @@ namespace Agv
IPEndPoint endPoint = (IPEndPoint)sokConnection.RemoteEndPoint;
string ip = endPoint.Address.ToString();
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))
{
log.info(string.Format($"{ip}重连 断开上一次连接:{client1.Endpoint}"));
Offline(client1);
Thread.Sleep(2000);
log.debug(string.Format("断开"));
dictClient[ip] = client;
}
Thread thr = new Thread(ListenNet);
Client client = new Client(sokConnection, thr, ip, endPoint.ToString());
thr.IsBackground = true;
dictClient.Add(ip, client);
else
dictClient.Add(ip, client);
thr.Start(client);
log.info(string.Format("客户端[{0}]连接服务端[{1}]成功", endPoint.ToString(), ServerInfo));
Thread.Sleep(300);
}
catch (SocketException)
{
......@@ -302,18 +301,21 @@ namespace Agv
if (dic.TryGetValue(endpoint, out surplusBuffer))
{
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];
Buffer.BlockCopy(bytes, startIdx, tmp, 0, bytesRead - startIdx);
Buffer.BlockCopy(curBuffer, startIdx, tmp, 0, bytesRead - startIdx);
//更新会话ID 的最新字节
dic.TryUpdate(endpoint, tmp, surplusBuffer);
log.debug($"更新缓存【{endpoint}】:【{Common.HexBuff(tmp)}】");
surplusBuffer = curBuffer;//同步
}
else
{
byte[] xbtye = null;
dic.TryRemove(endpoint, out xbtye);
log.debug($"删除缓存【{endpoint}】:【{Common.HexBuff(xbtye)}】");
}
}
else
......@@ -324,6 +326,7 @@ namespace Agv
byte[] tmp = new byte[bytesRead - startIdx];
Buffer.BlockCopy(bytes, startIdx, tmp, 0, bytesRead - startIdx);
dic.TryAdd(endpoint, tmp);
log.debug($"添加缓存【{endpoint}】:【{Common.HexBuff(tmp)}】");
surplusBuffer = tmp;//同步
}
else
......@@ -345,7 +348,7 @@ namespace Agv
Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead);
dic.TryUpdate(endpoint, byteSub, surplusBuffer);
surplusBuffer = byteSub;
log.debug($"更新缓存【{endpoint}】:【{Common.HexBuff(byteSub)}】");
totalLen = 0;
break;
......@@ -365,6 +368,7 @@ namespace Agv
byte[] byteSub = new byte[totalLen - haveRead];
Buffer.BlockCopy(surplusBuffer, haveRead, byteSub, 0, totalLen - haveRead);
dic.TryUpdate(endpoint, byteSub, surplusBuffer);
log.debug($"更新缓存【{endpoint}】:【{Common.HexBuff(byteSub)}】");
surplusBuffer = byteSub;
break;
}
......@@ -374,14 +378,21 @@ namespace Agv
//String strc = Encoding.UTF8.GetString(surplusBuffer, haveRead + headSize, bodySize);
byte[] resBytes = new byte[bodySize];
Buffer.BlockCopy(surplusBuffer, haveRead + Common.headSize, resBytes, 0, bodySize);
log.debug(string.Format("[Receive data from:{0}] -> {1}", endpoint, Common.HexBuff(resBytes)));
DecodeNode(client, resBytes);
log.debug(string.Format("[Parse data from:{0}] -> {1}", endpoint, Common.HexBuff(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;
if (Common.headSize + bodySize == bytesRead)//如果当前接收的数据包长度正好等于缓冲区长度,则待拼接的不规则数据长度归0
{
byte[] xbtye = null;
dic.TryRemove(endpoint, out xbtye);
log.debug($"删除缓存【{endpoint}】:【{Common.HexBuff(xbtye)}】");
surplusBuffer = null;//设置空 回到原始状态
totalLen = 0;//清0
}
......@@ -394,7 +405,7 @@ namespace Agv
}
#endregion
private void DecodeNode(Client client, byte[] resultBytes)
private bool DecodeNode(Client client, byte[] resultBytes)
{
//解码单个节点
if (Common.EnDecodeSingleNode)
......@@ -402,7 +413,8 @@ namespace Agv
Node node = Common.Decode(resultBytes);
if (node == null)
{
log.error("命令解析失败: " + Common.HexBuff(resultBytes));
//log.error("命令解析失败: " + Common.HexBuff(resultBytes));
return false;
}
else
{
......@@ -410,7 +422,7 @@ namespace Agv
int idx = client.nodeName.FindIndex(s => s == node.Name);
if (idx == -1) client.nodeName.Add(node.Name);
UpdateNode(node);
return true;
}
}
else
......@@ -419,7 +431,8 @@ namespace Agv
List<Node> nodes = Common.DecodeNodes(resultBytes);
if (nodes == null)
{
log.error("命令解析失败: " + Common.HexBuff(resultBytes));
//log.error("命令解析失败: " + Common.HexBuff(resultBytes));
return false;
}
else
{
......@@ -430,6 +443,7 @@ namespace Agv
if (idx == -1) client.nodeName.Add(node.Name);
}
UpdateNodes(nodes);
return true;
}
}
}
......@@ -465,6 +479,7 @@ namespace Agv
{
byte[] buff = new byte[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);
}
}
......@@ -523,7 +538,6 @@ namespace Agv
try
{
client.Loop = false;
dictClient.Remove(client.IP);
if (client.Socket != null)
{
client.Socket.Close();
......@@ -541,17 +555,14 @@ namespace Agv
nodeOnline[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.ListenNet.Abort();
log.info(string.Format("关闭对客户端[{0}]的监听线程", client.Endpoint));
log.info(string.Format("关闭对客户端[{0}]的监听线程1", client.Endpoint));
}
catch (ThreadAbortException)
{
log.error(string.Format("关闭对客户端[{0}]的监听线程", client.Endpoint));
log.error(string.Format("关闭对客户端[{0}]的监听线程1", client.Endpoint));
}
}
private void Offline(string clientKey)
......@@ -577,7 +588,7 @@ namespace Agv
client.nodeName.Clear();
client.ListenNet.Abort();
client.Socket = null;
dictClient.Remove(client.IP);
log.info(string.Format("关闭对客户端[{0}]的监听线程2", client.Endpoint));
}
private void KeepClientLive()
......@@ -591,7 +602,8 @@ namespace Agv
Thread.Sleep(2000);
foreach (string client in dictClient.Keys)
{
Send(client,buff);
SendLive(client, buff);
Thread.Sleep(1000);
}
}
catch (Exception ex)
......@@ -612,19 +624,32 @@ namespace Agv
try
{
dictClient[clientKey].Socket.Send(buff);
log.debug(string.Format("服务端[{0}]向客户端[{1}]发送消息:[{2}]", ServerInfo, clientKey, Common.HexBuff(buff)));
return true;
}
catch (Exception ex)
{
log.error("发送失败" + i + "次:" + ex.Message);
log.error($"给{clientKey}发送失败" + i + "次:" + ex.Message);
}
Thread.Sleep(100);
}
Offline(clientKey);
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
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!