Commit 01975f24 张东亮

1

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