文章内容有错,请直接关闭~~~不要看了。丢人。

         private static Dictionary<string, Packet> cache = new Dictionary<string, Packet>();
private static Dictionary<string, object> lockers = new Dictionary<string, object>();
public List<pkReturn> Package(ChannelReceiveEventArgs e)
{ byte[] data = new byte[e.Data.Count];
Buffer.BlockCopy(e.Data.Array, , data, , e.Data.Count);
int leng = e.Data.Count; if (!lockers.ContainsKey(e.Channel.ID)) lockers[e.Channel.ID] = new object(); //创建锁
lock (lockers[e.Channel.ID])
{
if (data != null)
{
string id = e.Channel.ID;
try
{
if (cache.ContainsKey(id))
{//如果客户端ID存在,就把接收到的字节,和之前的字节合起来
cache[id].BufferTemp = coder.UnionBytes(cache[id].BufferTemp, data);
}
else
{//如果客户端ID不存在集合内,就新建一个放入集合
Packet pk = new Packet();
cache[id] = pk;
cache[id].BufferTemp = new byte[leng];
Buffer.BlockCopy(data, , cache[id].BufferTemp, , data.Length);//拷贝数据
}
//循环处理包
List<pkReturn> lists = new List<pkReturn>();
cache[id].BufferCom = null;
do
{
pkReturn model = new pkReturn();
if (cache[id].BufferTemp.Length >= ) //1-4字节包大小 4-6两字节指令
{
byte[] sizetemp = new byte[];//大小
Buffer.BlockCopy(cache[id].BufferTemp, , sizetemp, , );
int size = (int)BitConverter.ToUInt32(sizetemp, );
size = size + ;
byte[] cmd = new byte[];//指令
Buffer.BlockCopy(cache[id].BufferTemp, , cmd, , );
if (size < && size > )
{
model.PkSize = size;
model.Cmd = (int)BitConverter.ToUInt32(cmd, );
}
else
{
e.Channel.Dispose();
Remove(id);//小于0 有问题 要清理掉客户端
lists.Clear();
break;
}
//解析包
byte[] tempbytes = new byte[size];
if (size < cache[id].BufferTemp.Length)
{ Buffer.BlockCopy(cache[id].BufferTemp, , tempbytes, , size);//拷贝一个完整包 byte[] temp = new byte[cache[id].BufferTemp.Length - size];//保存剩下的字节
Buffer.BlockCopy(cache[id].BufferTemp, size, temp, , cache[id].BufferTemp.Length - size);
cache[id].BufferTemp = temp; model.Bytes = coder.UnifyDecrypt(tempbytes);//对完整包解密
lists.Add(model);
}
else if (size == cache[id].BufferTemp.Length)
{
Buffer.BlockCopy(cache[id].BufferTemp, , tempbytes, , size);//根据获得大小拷贝字节
model.Bytes = coder.UnifyDecrypt(tempbytes);//对完整包解密
lists.Add(model);
cache[id].BufferTemp = null;//清空数据缓冲区
break;
}
else if (size > cache[id].BufferTemp.Length)
{//如果包头标记大小大于缓冲区大小,跳出
break;
}
else
{
Console.WriteLine("任何条件都不符合");
} }
else
{
break;
}
} while (true);
return lists;
}
catch (Exception ex)
{
Remove(id);
e.Channel.Dispose();
Console.WriteLine("分包解析函数异常:" + ex.Message + " 定位:" + ex.StackTrace);
throw new ArgumentException("分包解析函数异常 定位:"+ex.Message);
}
}
else
{
Console.WriteLine("data 为null");
throw new ArgumentException("分包解析函数异常 data 为null");
//return null;
}
}
}

最新文章

  1. Handler系列之原理分析
  2. NSString相关操作
  3. 一次线上http接口调用不通相关的解决过程
  4. 【MySQL】pt-query-digest数据处理并关联业务
  5. unicode-range 字体混搭(转)
  6. Missing number
  7. SPSS数据分析—单因素及多因素方差分析
  8. python string.py 源码分析 三:maketrans
  9. 闭包 (循环事件获取不到i) 和 各种解决循环获取不到i的解决方法
  10. UVa 10780 (质因数分解) Again Prime? No Time.
  11. Java http请求和调用
  12. java基础(Fundamental)
  13. python matplotlib 图表局部放大
  14. phpcms v9 sql注入脚本
  15. [Luogu2991][USACO10OPEN]水滑梯Water Slides
  16. ubuntu启动报/root/.profile mesg:ttyname failed错误的解决办法
  17. k8s 集群部署问题整理
  18. Proxmox VE中出现TASK ERROR: command &#39;apt-get update&#39; failed: exit code 100的解决方法
  19. java笔记----JVM内存
  20. Codeforces1023E Down or Right 【贪心】

热门文章

  1. 【原创】大数据基础之Logstash(3)应用之http(in和out)
  2. C语言-社保工资查询系统
  3. layui 常见的表单元素
  4. Codeforces 1114F Please, another Queries on Array? [线段树,欧拉函数]
  5. AES加解密程序的实现
  6. nodejs和ionic小助手
  7. thinkphp (tcms)
  8. 纯webpack打包项目
  9. Java 调用翻译软件实现英文文档翻译
  10. Confluence 6 编辑站点欢迎消息使用模板编辑器的小提示