// create a new kcp control object, 'conv' must equal in two endpoint // from the same connection. public KCP(UInt32 conv_, Action <byte[], int> output_) { conv = conv_; snd_wnd = IKCP_WND_SND; rcv_wnd = IKCP_WND_RCV; rmt_wnd = IKCP_WND_RCV; mtu = IKCP_MTU_DEF; mss = mtu - IKCP_OVERHEAD; rx_rto = IKCP_RTO_DEF; rx_minrto = IKCP_RTO_MIN; interval = IKCP_INTERVAL; ts_flush = IKCP_INTERVAL; ssthresh = IKCP_THRESH_INIT; dead_link = IKCP_DEADLINK; buffer = new byte[(mtu + IKCP_OVERHEAD) * 3]; output = output_; //创建报文包对象池 m_oSegmentPool = new ThreadQueue <Segment>(); m_oSegmentPool.Create(); //内存池 m_oMemPool = new MemPool(); m_oMemPool.Create(); // }
//分配对象节点 public QueueNode <ByteData> Aloc(int nSize) { QueueNode <ByteData> node = null; int nBoundSize = GetBoundSize(nSize); if (nBoundSize > 0) { ThreadQueue <ByteData> queueData = null; if (m_dicMemPool.TryGetValue(nBoundSize, out queueData)) { node = queueData.Pop(); if (null != node) { return(node); } } } else { nBoundSize = nSize; } node = new QueueNode <ByteData>(); node.item = new ByteData(); node.next = null; node.item.data = new byte[nBoundSize]; node.item.nLen = 0; return(node); }
//回收对象节点 public void Recycle(ref QueueNode <ByteData> node) { int nBoundSize = node.item.data.Length; ThreadQueue <ByteData> queueData = null; if (m_dicMemPool.TryGetValue(nBoundSize, out queueData)) { if (queueData.Size() < m_nQueueMaxSize) { queueData.Push(node); } } }
//创建 public void Create() { m_dicMemPool = new Dictionary <int, ThreadQueue <ByteData> >(); ThreadQueue <ByteData> threadQueue = null; for (int nPower = MIN_SIZE; nPower <= MAX_SIZE; nPower = nPower << 1) { threadQueue = new ThreadQueue <ByteData>(); threadQueue.Create(); m_dicMemPool.Add(nPower, threadQueue); } }
//创建 public bool Create() { m_oMemPool = new MemPool(); m_oMemPool.Create(); m_oDataQueue = new ThreadQueue <ByteData>(); m_oDataQueue.Create(); m_oRecycleQueue = new ThreadQueue <ByteData>(); m_oRecycleQueue.Create(); return(true); }
//释放 public void Release() { if (null != m_oSegmentPool) { m_oSegmentPool.Release(); m_oSegmentPool = null; } if (null != m_oMemPool) { m_oMemPool.Release(); m_oMemPool = null; } }
//销毁 public void Release() { if (null != m_oMemPool) { m_oMemPool.Release(); m_oMemPool = null; } if (null != m_oDataQueue) { m_oDataQueue.Release(); m_oDataQueue = null; } if (null != m_oRecycleQueue) { m_oRecycleQueue.Release(); m_oRecycleQueue = null; } }