Ejemplo n.º 1
0
        private async Task <bool> SendFrameAsync(Frame frame)
        {
            try
            {
                Stream stream = this.networkStream.Stream;

                await networkStream.WriteLock.WaitAsync();

                try
                {
                    await frame.WriteAsync(stream);
                }
                finally
                {
                    networkStream.WriteLock.Release();
                }

                await stream.FlushAsync();

                return(true);
            }
            catch (Exception ex) when(ex is IOException || ex is ObjectDisposedException || ex is SocketException)
            {
                logger.Site().Error(ex, "{0} While writing a Frame to the network: {1}", this, ex.Message);
                return(false);
            }
        }
Ejemplo n.º 2
0
        private async Task <bool> SendFrameAsync(Frame frame)
        {
            try
            {
                Stream networkStream = netSocket.NetworkStream;

                await netSocket.WriteLock.WaitAsync();

                try
                {
                    await frame.WriteAsync(networkStream);
                }
                finally
                {
                    netSocket.WriteLock.Release();
                }

                await networkStream.FlushAsync();

                return(true);
            }
            catch (Exception ex) when(ex is IOException || ex is ObjectDisposedException || ex is SocketException)
            {
                Log.Error(ex, "{0}.{1}: While writing a Frame to the network: {2}", this, nameof(SendFrameAsync),
                          ex.Message);
                return(false);
            }
        }
Ejemplo n.º 3
0
        public async Task Frame_RoundTrip_Works()
        {
            var expectedFramelets = new[]
            {
                new Framelet(FrameletType.EpoxyConfig, AnyContents),
                new Framelet(FrameletType.LayerData, AnyContents),
                new Framelet(FrameletType.EpoxyConfig, AnyContents),
            };

            var frame = new Frame(LoggerTests.BlackHole);
            foreach (var framelet in expectedFramelets)
            {
                frame.Add(framelet);
            }

            var memStream = new MemoryStream();
            await frame.WriteAsync(memStream);

            memStream.Seek(0, SeekOrigin.Begin);
            var resultFrame = await Frame.ReadAsync(memStream, CancellationToken.None, LoggerTests.BlackHole);

            CollectionAssert.AreEqual(expectedFramelets, resultFrame.Framelets, DeepFrameletComparer.Instance);
        }
Ejemplo n.º 4
0
        public async Task Frame_WriteAsync_OneFramelet_ContentsExpected()
        {
            var frame = new Frame(LoggerTests.BlackHole);
            frame.Add(new Framelet(FrameletType.EpoxyConfig, AnyContents));

            var memStream = new MemoryStream();
            await frame.WriteAsync(memStream);

            var expectedBytes = new[]
            {
                0x01, 0x00, // frame count
                0x43, 0x47, // EpoxyConfig framelet type
                0x04, 0x00, 0x00, 0x00, // framelet length
                0x62, 0x6F, 0x6E, 0x64 // AnyContents bytes
            };
            CollectionAssert.AreEqual(expectedBytes, memStream.ToArray());
        }
Ejemplo n.º 5
0
 public void Frame_WriteAsync_EmptyFrame_Throws()
 {
     var frame = new Frame(LoggerTests.BlackHole);
     Assert.Throws<InvalidOperationException>(async () => await frame.WriteAsync(new MemoryStream()));
 }