Example #1
0
        void HandleData(NetIncomingMessage message)
        {
            ushort     id         = ushort.MaxValue;
            ByteBuffer byteBuffer = null;

            try
            {
                id = message.ReadUInt16();
                ushort len = message.ReadUInt16();
                byteBuffer = ByteBufferPool.Alloc(len);
                message.ReadBytes(byteBuffer.Data, 0, len);
                var result = dispatcher.Fire(message.SenderConnection, (MessageID)id, byteBuffer, message);
                if (result != MessageHandleResult.Processing)
                {
                    ByteBufferPool.Dealloc(ref byteBuffer);
                }
            }
            catch (Exception e)
            {
                ByteBufferPool.Dealloc(ref byteBuffer);
                NetLog.Exception("HandleData throws exception", e);

                if (id != ushort.MaxValue)
                {
                    NetLog.Error("Caught exception when processing message " + (MessageID)id);
                }
                else
                {
                    NetLog.Error("Caught exception when processing message");
                }
            }
        }
Example #2
0
    static void ByteBufferPoolTest()
    {
        for (int i = 0; i < ByteBufferPool.sPool.Length; ++i)
        {
            var p = ByteBufferPool.sPool[i];
            Debug.Assert(p.Count == ByteBufferPool.initialArraySize);
            for (int j = 0; j < p.Count; ++j)
            {
                Debug.Assert(p[j].Data.Length == 1 << (i + ByteBufferPool.minSizePOT));
            }
        }

        int min = 1 << ByteBufferPool.minSizePOT;
        List <ByteBuffer> buffers = new List <ByteBuffer>();

        buffers.Add(ByteBufferPool.Alloc(0));
        buffers.Add(ByteBufferPool.Alloc(1));
        buffers.Add(ByteBufferPool.Alloc(min - 1));
        buffers.Add(ByteBufferPool.Alloc(min));
        buffers.Add(ByteBufferPool.Alloc(min + 1));

        var pool_64 = ByteBufferPool.sPool[ByteBufferPool.sIndexLookup[1 << ByteBufferPool.minSizePOT]];

        Debug.Assert(ByteBufferPool.AvailableSlots(pool_64) == 0);
        var pool_128 = ByteBufferPool.sPool[ByteBufferPool.sIndexLookup[1 << (ByteBufferPool.minSizePOT + 1)]];

        Debug.Assert(ByteBufferPool.AvailableSlots(pool_128) == ByteBufferPool.initialArraySize - 1);

        buffers.Add(ByteBufferPool.Alloc(min));
        Debug.Assert(ByteBufferPool.AvailableSlots(pool_64) == ByteBufferPool.initialArraySize - 1);

        for (int i = 0; i < buffers.Count; ++i)
        {
            ByteBuffer bb = buffers[i];
            ByteBufferPool.Dealloc(ref bb);
            Debug.Assert(null == bb);
        }
        Debug.Assert(ByteBufferPool.AvailableSlots(pool_64) == ByteBufferPool.initialArraySize * 2);
        Debug.Assert(ByteBufferPool.AvailableSlots(pool_128) == ByteBufferPool.initialArraySize);

        try
        {
            ByteBufferPool.Alloc((1 << ByteBufferPool.maxSizePOT) + 1);
            Debug.Assert(false);
        } catch (ArgumentOutOfRangeException) { }

        ByteBuffer wrongsize = new ByteBuffer(new byte[123]);

        ByteBufferPool.Dealloc(ref wrongsize);
        Debug.Assert(null != wrongsize);

        ByteBuffer exceed = new ByteBuffer(new byte[1 << ByteBufferPool.minSizePOT]);

        ByteBufferPool.Dealloc(ref exceed);
        Debug.Assert(null != exceed);

        Console.WriteLine("ByteBuffer Pool Checked");
    }
Example #3
0
        void HandleData(NetIncomingMessage message)
        {
            MessageID  id         = (MessageID)message.ReadUInt16();
            ushort     len        = message.ReadUInt16();
            ByteBuffer byteBuffer = ByteBufferPool.Alloc(len);

            try
            {
                message.ReadBytes(byteBuffer.Data, 0, len);
                var result = dispatcher.Fire(message.SenderConnection, id, byteBuffer, message);
                if (result != MessageHandleResult.Processing)
                {
                    ByteBufferPool.Dealloc(ref byteBuffer);
                }
            }
            catch (Exception e)
            {
                ByteBufferPool.Dealloc(ref byteBuffer);
                NetLog.Exception("HandleData throws exception", e);
            }
        }