/// <summary> /// 缓存分包 /// </summary> /// <param name="token"></param> /// <param name="index"></param> /// <returns></returns> public static AsyncUdpUserToken PackCacheUDP(AsyncUdpUserToken token, ref int index) { if (cacheUDP == null) { cacheUDP = new CacheManager(); cacheUDP.BufferSize = UdpPackSize; cacheUDP.MaxBufferCount = MaxUseBytes / UdpPackSize * 2; } if (tokenPool == null) { tokenPool = new UserTokenPool(); } if (token.Length == 0) { token.Length = token.Data.Length; } // byte[] buf = null; AsyncUdpUserToken userToken = tokenPool.Pop(); userToken.Remote = token.Remote; userToken.IPAddress = token.IPAddress; userToken.IsFixCache = false; userToken.Socket = token.Socket; if (index + UdpPackSize <= token.Length) { //分包 if (!cacheUDP.GetBuffer(out buf)) { buf = new byte[UdpPackSize];//与 } Array.Copy(token.Data, token.Offset + index, buf, 0, UdpPackSize); index += UdpPackSize; userToken.Cache = cacheUDP; userToken.TokenPool = tokenPool; } else { int len = token.Length - index; buf = new byte[len]; Array.Copy(token.Data, token.Offset + index, buf, 0, len); index += len; userToken.TokenPool = tokenPool; } return(userToken); }
/// <summary> /// 处理接收的数据 /// </summary> /// <param name="buf"></param> /// <param name="Offset"></param> /// <param name="len"></param> /// <param name="isCache"></param> private void DoEventRecvice(byte[] buf, EndPoint remote, int Offset = 0, int len = 0, bool isCache = false) { AsyncUdpUserToken token = tokenPool.Pop(); token.Data = buf; token.Offset = Offset; token.Length = len; token.IsFixCache = IsFixCache; token.Remote = remote; token.Socket = socket; if (isCache) { token.Cache = cacheManager; } if (OnDataReceived != null) { Task.Factory.StartNew(() => { OnDataReceived(this, token); }); } }