public AsyncUdpUserToken(CacheManager cache = null, UserTokenPool pool = null) { this.cache = cache; this.tokenPool = pool; this.CreateTime = DateTime.Now; this.DataTime = DateTime.Now; }
public UDPSocket() { BufferSize = 65535; TotalBufSize = 1024 * MByte; IsFixCache = false; pool = new SocketAsyncEventArgsPool(); bufferManager = new BufferManager(); cacheManager = new CacheManager(); tokenPool = new UserTokenPool(); TokenMaxLeftNum = Environment.ProcessorCount * 10; if (TokenMaxLeftNum == 0) { TokenMaxLeftNum = 100; } }
/// <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); }
public UDPTaskSocket() { BufferSize = 65535; TotalBufSize = 1024 * MByte; IsFixCache = false; cacheManager = new CacheManager(); tokenPool = new UserTokenPool(); uDPSendPool = new UDPSendPool(); queue = new ConcurrentQueue <AsyncUDPSendBuffer>(); TokenMaxLeftNum = Environment.ProcessorCount * 10; if (TokenMaxLeftNum == 0) { TokenMaxLeftNum = 100; } int CPU = (int)(Environment.ProcessorCount * 1.5); semaphore = new Semaphore(CPU, CPU); }
/// <summary> /// 缓存分包;如果不到缓存长度就创建byte[],通过isUse /// </summary> /// <param name="token"></param> /// <param name="index"></param> /// <returns></returns> public static AsyncUdpUserToken PackCacheUDPHead(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 (token.DataPackage == null) { token.DataPackage = new UDPDataPackage(); token.DataPackage.packageID = Interlocked.Increment(ref dataPackageid); token.DataPackage.packageSeq = -1; token.DataPackage.packageSum = token.Length; token.DataPackage.data = token.Data; token.DataPackage.DataLen = token.Length; token.DataPackage.Offset = token.Offset; token.PackageNum = token.Length / UdpPackSize + 1; token.DataPackage.PackageNum = token.PackageNum; } if (index + UdpPackSize <= token.Length) { //分包 if (!cacheUDP.GetBuffer(out buf)) { buf = new byte[UdpPackSize]; //与 } token.Offset = token.Offset + index; //移动偏移量 token.DataPackage.Pack(buf, 0, UdpPackSize); userToken.Data = token.DataPackage.data; userToken.Length = 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 + UDPDataPackage.HeadLen]; //头 token.Offset = token.Offset + index; //移动偏移量 token.DataPackage.Pack(buf, 0, buf.Length); //这样做恰好合适,内部分包不判断 //Array.Copy(token.Data, token.Offset + index, buf, 0, len); userToken.Data = buf; userToken.Length = buf.Length; index += len; userToken.TokenPool = tokenPool; } return(userToken); }