static SocketConnection() { // validated styles for known OSes if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { // zero-length receive works fine _bufferStyle = BufferStyle.UseZeroLengthBuffer; } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { // zero-length receive is unreliable _bufferStyle = BufferStyle.UseSmallBuffer; } else { // default to "figure it out based on what happens" _bufferStyle = BufferStyle.Unknown; } if (_bufferStyle != BufferStyle.UseZeroLengthBuffer) { // we're going to need to use small buffers for awaiting input _smallBuffers = new MicroBufferPool(SmallBufferSize, ushort.MaxValue); } }
public void BufferPoolBasicUsage() { var pool = new MicroBufferPool(8, 4); ArraySegment <byte>[] segments = new ArraySegment <byte> [5]; Assert.Equal(0, pool.InUse); Assert.Equal(4, pool.Available); Assert.True(pool.TryTake(out segments[0])); Assert.True(pool.TryTake(out segments[1])); Assert.Equal(2, pool.InUse); Assert.Equal(2, pool.Available); Assert.True(pool.TryTake(out segments[2])); Assert.True(pool.TryTake(out segments[3])); Assert.False(pool.TryTake(out segments[4])); Assert.Equal(4, pool.InUse); Assert.Equal(0, pool.Available); for (int i = 0; i < 4; i++) { Assert.Equal(i * 8, segments[i].Offset); Assert.Equal(8, segments[i].Count); } pool.Recycle(segments[3]); pool.Recycle(segments[1]); Assert.Equal(2, pool.InUse); Assert.Equal(2, pool.Available); Assert.True(pool.TryTake(out segments[1])); Assert.True(pool.TryTake(out segments[3])); Assert.False(pool.TryTake(out segments[4])); Assert.Equal(4, pool.InUse); Assert.Equal(0, pool.Available); Assert.Equal(24, segments[1].Offset); Assert.Equal(8, segments[3].Offset); for (int i = 0; i < 4; i++) { pool.Recycle(segments[i]); } Assert.Equal(0, pool.InUse); Assert.Equal(4, pool.Available); }