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()); }
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); }
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); }
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; } }
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); }