예제 #1
0
파일: IoServer.cs 프로젝트: cooml/inyzmes
 /// <summary> 清空指定的粘包处理的缓存区
 /// </summary>
 /// <param name="QueueId">队列约定的id(找缓存区)</param>
 /// <param name="IpportStr">(缓冲区索引,目前是ip加端口号)</param>
 private void CleandicBuffer(int QueueId, string IpportStr)
 {
     try
     {
         if (GetDicBuffer(QueueId).ContainsKey(IpportStr))
         {
             DynamicBufferManager dy = new DynamicBufferManager();
             GetDicBuffer(QueueId).TryRemove(IpportStr, out dy);
             dy.Clear();
             dy = null;
         }
     }
     catch
     {
     }
 }
예제 #2
0
        /// <summary>大内存粘包处理
        /// </summary>
        /// <param name="reciveByteArr">未做粘包处理原始字节数组</param>
        /// <param name="dicBufferIndex">粘包的缓存区索引(目前使用终结点)</param>
        /// <param name="dicBufferDic">粘包的缓存区字典</param>
        /// <returns>一个或者多个完整的数据包集合</returns>
        public static List <byte[]> MakeStickingBag(byte[] reciveByteArr, string dicBufferIndex, ConcurrentDictionary <string, DynamicBufferManager> dicBufferDic)
        {
            var completeByteArrList = new List <byte[]>();//存放完整的数据包

            try
            {
                var byteRecive = reciveByteArr;

                //判断粘包缓存区是否存在数据
                if (dicBufferDic.ContainsKey(dicBufferIndex))
                {
                    dicBufferDic[dicBufferIndex].WriteBuffer(byteRecive);

                    var tempByteArr = dicBufferDic[dicBufferIndex].Buffer;

                    int commandLen = BitConverter.ToInt32(tempByteArr, 0);

                    while (dicBufferDic[dicBufferIndex].GetDataCount() >= (commandLen + 4) && dicBufferDic[dicBufferIndex].GetDataCount() > 0)
                    {
                        var tempArr = new byte[commandLen];

                        Array.Copy(tempByteArr, sizeof(int), tempArr, 0, commandLen);

                        dicBufferDic[dicBufferIndex].Clear(commandLen + 4);

                        completeByteArrList.Add(tempArr);

                        if (dicBufferDic[dicBufferIndex].GetDataCount() <= 4)
                        {
                            break;
                        }

                        commandLen = BitConverter.ToInt32(tempByteArr, 0); //取出命令长度
                    }
                }
                else
                {
                    var dy = new DynamicBufferManager(300);//新建一个DynamicBufferManager,如果处理完后有不完整的包就放入到缓存区

                    dy.WriteBuffer(byteRecive);

                    var tempByteArr = dy.Buffer;

                    int commandLen = BitConverter.ToInt32(tempByteArr, 0);

                    while (dy.GetDataCount() >= (commandLen + 4) && dy.GetDataCount() > 0)
                    {
                        var tempArr = new byte[commandLen];

                        Array.Copy(tempByteArr, sizeof(int), tempArr, 0, commandLen);

                        dy.Clear(commandLen + 4);

                        completeByteArrList.Add(tempArr);

                        if (dy.GetDataCount() <= 4)
                        {
                            break;
                        }

                        commandLen = BitConverter.ToInt32(tempByteArr, 0); //取出命令长度
                    }

                    //如果当前的 byte 数组 里面有剩余字节,就加到粘包的缓存区
                    if (dy.GetDataCount() > 0)
                    {
                        dicBufferDic.TryAdd(dicBufferIndex, dy);
                    }
                }

                return(completeByteArrList);
            }
            catch (Exception ex)
            {
                Logger.WriteLog("粘包处理:" + ex.Message);
                return(completeByteArrList);
            }
        }