Commit 01975f24 张东亮

1

1 个父辈 444cd4e0
......@@ -18,10 +18,22 @@ namespace Agv
/// 节点信息解析方式:默认解析单个
/// </summary>
public static bool EnDecodeSingleNode = false;
public static bool CheckHeadChar(byte[] head)
{
if (head != null && head.Length >= headPack.Length)
{
for (int i = 0; i < headPack.Length; i++)
{
if ((head[i] != headPack[i]))
return false;
}
}
return true;
}
public static bool CheckHeadChar(byte[] head, out int starIdx)
{
starIdx = -1;
for (int i = 0; i < head.Length - headPack.Length+1; i++)
for (int i = 0; i < head.Length - headPack.Length + 1; i++)
{
int j = 0;
for (j = 0; j < headPack.Length; j++)
......
a2c542a81d97a3eb8ae8ea5cece663e91e44e532
14658b2508922eea2b84ff26a73fe8d074e0f067
......@@ -8,3 +8,13 @@ E:\Neotel\Projects\Gitee\AGV-Com\Agv\obj\Debug\Agv.csproj.CopyComplete
E:\Neotel\Projects\Gitee\AGV-Com\Agv\obj\Debug\Agv.dll
E:\Neotel\Projects\Gitee\AGV-Com\Agv\obj\Debug\Agv.pdb
E:\Neotel\Projects\Gitee\AGV-Com\Agv\obj\Debug\Agv.csproj.AssemblyReference.cache
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\bin\Debug\Agv.dll
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\bin\Debug\Agv.pdb
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\bin\Debug\log4net.dll
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\bin\Debug\Newtonsoft.Json.dll
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\bin\Debug\log4net.xml
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\obj\Debug\Agv.csproj.AssemblyReference.cache
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\obj\Debug\Agv.csproj.CoreCompileInputs.cache
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\obj\Debug\Agv.csproj.CopyComplete
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\obj\Debug\Agv.dll
E:\Codes\Neotel\SO775-AGV\AGV-Com\Agv\obj\Debug\Agv.pdb
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Net;
using System.Net.Sockets;
......@@ -300,39 +301,39 @@ namespace Agv
byte[] surplusBuffer = null;
if (dic.TryGetValue(endpoint, out surplusBuffer))
{
log.debug($"拼接上次剩余包【{endpoint}】:【{Common.HexBuff(surplusBuffer)}】+【{Common.HexBuff(bytes)}】");
byte[] curBuffer = surplusBuffer.Concat(bytes).ToArray();//拼接上一次剩余的包
bytesRead = curBuffer.Length;
if (Common.CheckHeadChar(curBuffer, out int startIdx))//检查是否存在指定包头识别字符
{
byte[] tmp = new byte[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)}】");
}
//更新会话ID 的最新字节
dic.TryUpdate(endpoint, curBuffer, surplusBuffer);
surplusBuffer = curBuffer;//同步
}
else
{
//添加会话ID的bytes
dic.TryAdd(endpoint, bytes);
log.debug($"新数据【{endpoint}】:【{Common.HexBuff(bytes)}】");
surplusBuffer = bytes;//同步
}
//判断包头起始符
if (!Common.CheckHeadChar(surplusBuffer, out int startIdx))
{
dic.TryRemove(endpoint, out _);
log.debug($"未找到包头,删除缓存【{endpoint}】:【{Common.HexBuff(surplusBuffer)}】");
surplusBuffer = null;//设置空 回到原始状态
return;
}
else
{
if (Common.CheckHeadChar(bytes, out int startIdx))//检查是否存在指定包头识别字符
if(startIdx>0)
{
//添加会话ID的bytes
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;//同步
log.debug($"找到包头,但不在起始位置【{startIdx}】【{endpoint}】:【{Common.HexBuff(surplusBuffer)}】");
byte[] subByte= new byte[surplusBuffer.Length-startIdx+1];
Buffer.BlockCopy(surplusBuffer, startIdx, subByte, 0, subByte.Length);//从缓冲区里读取包头的字节
dic.TryUpdate(endpoint, subByte, surplusBuffer);
surplusBuffer = subByte;//同步
log.debug($"找到包头,不在起始位置处理完成【{endpoint}】:【{Common.HexBuff(surplusBuffer)}】");
}
else
return;
}
//已经完成读取每个数据包长度
int haveRead = 0;
//这里totalLen的长度有可能大于缓冲区大小的(因为 这里的surplusBuffer 是系统缓冲区+不完整的数据包)
......@@ -348,19 +349,16 @@ 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;
log.debug($"剩余包长度不够包头,保存【{endpoint}】:【{Common.HexBuff(byteSub)}】");
break;
}
//如果够了一个完整包,则读取包头的数据
byte[] headByte = new byte[Common.headSize];
Buffer.BlockCopy(surplusBuffer, haveRead, headByte, 0, Common.headSize);//从缓冲区里读取包头的字节
//判断包头起始符
int bodySize = BitConverter.ToUInt16(headByte, 2);//从包头里面分析出包体的长度
//这里的 haveRead=等于N个数据包的长度 从0开始;0,1,2,3....N
int bodySize = BitConverter.ToInt32(headByte, 2);//从包头里面分析出包体的长度
//这里的 haveRead=等于N个数据包的长度 从0开始;0,1,2,3....N
//如果自定义缓冲区拆解N个包后的长度 大于 总长度,说最后一段数据不够一个完整的包了,拆出来保存
if (haveRead + Common.headSize + bodySize > totalLen)
......@@ -368,8 +366,8 @@ 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;
log.debug($"解析剩余数据不足一个完整包,保存剩余【{endpoint}】:【{Common.HexBuff(byteSub)}】");
break;
}
else
......@@ -378,23 +376,25 @@ 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("[Parse data from:{0}] -> {1}", endpoint, Common.HexBuff(resBytes)));
if (!DecodeNode(client, resBytes))//解析失败,重新接收
log.debug(string.Format("[解析数据:{0}]:{1}", endpoint, Common.HexBuff(resBytes)));
if (!DecodeNode(client, resBytes))//解析失败,清空缓存
{
//byte[] xbtye = null;
//dic.TryRemove(endpoint, out xbtye);
// log.error($"清除缓存【{endpoint}】:{Common.HexBuff(xbtye)}");
log.error($"解析失败【{endpoint}】:【{Common.HexBuff(resBytes)}】");
dic.TryRemove(endpoint, out byte[] xbtye);
log.debug($"删除剩余缓存【{endpoint}】:【{Common.HexBuff(xbtye)}】");
surplusBuffer = null;//设置空 回到原始状态
totalLen = 0;//清0
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)}】");
dic.TryRemove(endpoint, out byte[] xbtye);
log.debug($"接收的数据长度{bytesRead}一次解析完成,删除缓存【{endpoint}】:【{Common.HexBuff(bytes)}】【{Common.HexBuff(xbtye)}】");
surplusBuffer = null;//设置空 回到原始状态
totalLen = 0;//清0
return;
}
}
......@@ -485,7 +485,7 @@ namespace Agv
}
catch (Exception e)
{
log.error($"ListenNet error,{client.IP}", e);
}
}
}
......
e196c9379e13470b4ab5ab3b2ee6f05d3c7c771f
82320ed0b6da403e89a8d1157d5e457da0cfd1cb
......@@ -10,3 +10,15 @@ E:\Neotel\Projects\Gitee\AGV-Com\Server\obj\Debug\Server.csproj.CopyComplete
E:\Neotel\Projects\Gitee\AGV-Com\Server\obj\Debug\Agv.Server.dll
E:\Neotel\Projects\Gitee\AGV-Com\Server\obj\Debug\Agv.Server.pdb
E:\Neotel\Projects\Gitee\AGV-Com\Server\obj\Debug\Server.csproj.AssemblyReference.cache
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\bin\Debug\Agv.Server.dll
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\bin\Debug\Agv.Server.pdb
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\bin\Debug\Agv.dll
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\bin\Debug\Newtonsoft.Json.dll
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\bin\Debug\log4net.dll
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\bin\Debug\Agv.pdb
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\bin\Debug\log4net.xml
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\obj\Debug\Server.csproj.AssemblyReference.cache
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\obj\Debug\Server.csproj.CoreCompileInputs.cache
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\obj\Debug\Server.csproj.CopyComplete
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\obj\Debug\Agv.Server.dll
E:\Codes\Neotel\SO775-AGV\AGV-Com\Server\obj\Debug\Agv.Server.pdb
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!