public async Task SendHeader() { await semaphoreSlim.WaitAsync(); try { using (var ms = new PooledMemoryStream()) { var nbw = new NetworkBinaryWriter(ms); nbw.Write(Amqp); byte one = (byte)1; if (Endpoint.Protocol.Revision != 0) { nbw.Write((byte)0); nbw.Write((byte)Endpoint.Protocol.MajorVersion); nbw.Write((byte)Endpoint.Protocol.MinorVersion); nbw.Write((byte)Endpoint.Protocol.Revision); } else { nbw.Write(one); nbw.Write(one); nbw.Write((byte)Endpoint.Protocol.MajorVersion); nbw.Write((byte)Endpoint.Protocol.MinorVersion); } var bufferSegment = ms.ToUnsafeArraySegment(); await m_netStream.WriteAsync(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count); } } finally { semaphoreSlim.Release(); } }
public void TestWriteData() { using (var ms = new PooledMemoryStream()) { var testData = new byte[4096]; new Random().NextBytes(testData); ms.Write(testData, 0, testData.Length); var result = ms.ToUnsafeArraySegment(); Assert.Equal(0, result.Offset); Assert.Equal(4096, result.Count); Assert.Equal(new ArraySegment <byte>(testData), result); } }
public void TestWriteDataToExistingBuffer() { var buffer = ArrayPool <byte> .Shared.Rent(4096); using (var ms = new PooledMemoryStream(buffer, 20, ArrayPool <byte> .Shared)) { var testData = new byte[2048]; new Random().NextBytes(testData); ms.Write(testData, 0, testData.Length); var result = ms.ToUnsafeArraySegment(); Assert.Equal(20, result.Offset); Assert.Equal(2048, result.Count); Assert.Equal(new ArraySegment <byte>(testData), result); } }
public async Task WriteFrame(OutboundFrame frame) { await semaphoreSlim.WaitAsync(); try { using (var ms = new PooledMemoryStream()) { var nbw = new NetworkBinaryWriter(ms); frame.WriteTo(nbw); var bufferSegment = ms.ToUnsafeArraySegment(); await m_netStream.WriteAsync(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count); } } finally { semaphoreSlim.Release(); } }
public async Task WriteFrameSet(IList <OutboundFrame> frames) { await semaphoreSlim.WaitAsync(); try { using (var ms = new PooledMemoryStream()) { var nbw = new NetworkBinaryWriter(ms); for (var i = 0; i < frames.Count; ++i) { frames[i].WriteTo(nbw); } var bufferSegment = ms.ToUnsafeArraySegment(); await m_netStream.WriteAsync(bufferSegment.Array, bufferSegment.Offset, bufferSegment.Count); } } finally { semaphoreSlim.Release(); } }