示例#1
0
        public static byte[] PackMsgToArray <T>(ref T msg, int msgSize, WritePropsDeleg <T> writeProps)
            where T : struct
        {
            using PooledBuffer slice = PooledBuffer.Get(msgSize);
            int index = 0;

            writeProps(ref msg, slice.Data, ref index);
            return(slice.ToArray());
        }
示例#2
0
        public static PooledBuffer PackMsgToBuffer <T>(ref T msg, int msgSize, WritePropsDeleg <T> writeProps)
            where T : struct
        {
            PooledBuffer data  = PooledBuffer.Get(msgSize);
            int          index = 0;

            writeProps(ref msg, data.Data, ref index);
            return(data);
        }
示例#3
0
        public static void PackMsgToStream <T>(ref T msg, Stream destinationStream, int msgSize, WritePropsDeleg <T> writeProps)
            where T : struct
        {
            using PooledBuffer slice = PooledBuffer.Get(msgSize);
            int index = 0;

            writeProps(ref msg, slice.Data, ref index);
            slice.WriteInto(destinationStream);
        }
示例#4
0
        private static void ReclaimSlice(PooledBuffer slice)
        {
            switch (slice.Data.Length)
            {
            case 16:     //most likely
                lock (SixteenByteCache)
                    SixteenByteCache.Push(slice);
                return;

            case 2:
                lock (TwoByteCache)
                    TwoByteCache.Push(slice);
                return;

            case 4:
                lock (FourByteCache)
                    FourByteCache.Push(slice);
                return;

            default:
                lock (SlowCache)
                {
                    if (SlowCache.Count > 0)
                    {
                        LinkedListNode <PooledBuffer> node = SlowCache.First;
                        int size = slice.Data.Length;

                        while (node.Next != null)
                        {
                            if (node.Value.Data.Length > size)
                            {
                                node = node.Next;
                                continue;
                            }

                            break;
                        }

                        if (node.Value.Data.Length >= size)
                        {
                            SlowCache.AddAfter(node, slice);
                        }
                        else
                        {
                            SlowCache.AddBefore(node, slice);
                        }
                    }
                    else
                    {
                        SlowCache.AddLast(slice);
                    }
                }

                return;
            }
        }
示例#5
0
 public static void UnpackMsg <T>(ref T msg, Stream sourceStream, ReadPropsFromStreamDeleg <T> readProps)
     where T : struct
 {
     using PooledBuffer slice = PooledBuffer.Get(GpBufferSize);
     readProps(ref msg, sourceStream, slice.Data);
 }