コード例 #1
0
 public KcpClient(IKcpSocket client, ISessionListener listen)
 {
     _buffer           = BufferQueue.Allocate(1024 * 32);
     this.socket       = client;
     this._listen      = listen;
     client.onMessage += Socket_onMessage;
 }
コード例 #2
0
 public static Segment Get(int size)
 {
     if (_pool.count > 0)
     {
         var seg = _pool.Get();
         seg.data = BufferQueue.Allocate(size, true);
         return(seg);
     }
     return(new Segment(size));
 }
コード例 #3
0
 /// <summary>
 /// 将一个ByteBuffer的有效字节区写入此缓存区中
 /// </summary>
 /// <param name="buffer">待写入的字节缓存区</param>
 public void Write(BufferQueue buffer)
 {
     if (buffer == null)
     {
         return;
     }
     if (buffer.canRead <= 0)
     {
         return;
     }
     WriteBytes(buffer.ToArray());
 }
コード例 #4
0
 /// <summary>
 /// 复制一个对象,具有与原对象相同的数据,不改变原对象的数据,不包括已读数据
 /// </summary>
 /// <returns></returns>
 public BufferQueue Copy()
 {
     if (_buffer == null)
     {
         return(new BufferQueue(16));
     }
     if (_read < _write)
     {
         byte[] newbytes = new byte[_write - _read];
         Array.Copy(_buffer, _read, newbytes, 0, newbytes.Length);
         BufferQueue buffer = new BufferQueue(newbytes.Length);
         buffer.WriteBytes(newbytes);
         buffer._pool = this._pool;
         return(buffer);
     }
     return(new BufferQueue(16));
 }
コード例 #5
0
        /// <summary>
        /// 深度复制,具有与原对象相同的数据,不改变原对象的数据,包括已读数据
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            if (_buffer == null)
            {
                return(new BufferQueue(16));
            }
            BufferQueue newBuf = new BufferQueue(_buffer)
            {
                _capacity      = this._capacity,
                _read          = this._read,
                _write         = this._write,
                markReadIndex  = this.markReadIndex,
                markWirteIndex = this.markWirteIndex,
                _pool          = this._pool
            };

            return(newBuf);
        }
コード例 #6
0
            internal void Reset()
            {
                conv     = 0;
                cmd      = 0;
                frg      = 0;
                wnd      = 0;
                ts       = 0;
                sn       = 0;
                una      = 0;
                rto      = 0;
                xmit     = 0;
                resendts = 0;
                fastack  = 0;
                acked    = 0;

                data.Clear();
                data.Dispose();
                data = null;
            }
コード例 #7
0
        /// <summary>
        /// 构建一个capacity长度的字节缓存区ByteBuffer对象
        /// </summary>
        /// <param name="capacity">初始容量</param>
        /// <param name="fromPool">
        /// true表示获取一个池化的ByteBuffer对象,池化的对象必须在调用Dispose后才会推入池中,此方法为线程安全的。
        /// 当为true时,从池中获取的对象的实际capacity值。
        /// </param>
        /// <returns>ByteBuffer对象</returns>
        public static BufferQueue Allocate(int capacity, bool fromPool = false)
        {
            if (!fromPool)
            {
                return(new BufferQueue(capacity));
            }
            BufferQueue bbuf;

            if (pool.count == 0)
            {
                bbuf = new BufferQueue(capacity)
                {
                    _pool = true
                };
                return(bbuf);
            }
            bbuf = pool.Get();
            if (!bbuf._pool)
            {
                bbuf._pool = true;
            }
            return(bbuf);
        }
コード例 #8
0
        /// <summary>
        /// 构建一个以bytes为字节缓存区的ByteBuffer对象,一般不推荐使用
        /// </summary>
        /// <param name="bytes">初始字节数组</param>
        /// <param name="fromPool">
        /// true表示获取一个池化的ByteBuffer对象,池化的对象必须在调用Dispose后才会推入池中,此方法为线程安全的。
        /// </param>
        /// <returns>ByteBuffer对象</returns>
        public static BufferQueue Allocate(byte[] bytes, bool fromPool = false)
        {
            if (!fromPool)
            {
                return(new BufferQueue(bytes));
            }
            BufferQueue bbuf;

            if (pool.count == 0)
            {
                bbuf = new BufferQueue(bytes)
                {
                    _pool = true
                };
                return(bbuf);
            }
            bbuf = pool.Get();
            bbuf.WriteBytes(bytes);
            if (!bbuf._pool)
            {
                bbuf._pool = true;
            }
            return(bbuf);
        }
コード例 #9
0
 private Segment(int size)
 {
     data = BufferQueue.Allocate(size, true);
 }