示例#1
0
        public async Task ReceiveDataPortForward()
        {
            using (MockWebSocket ws = new MockWebSocket())
                using (StreamDemuxer demuxer = new StreamDemuxer(ws, StreamType.PortForward))
                {
                    demuxer.Start();

                    List <byte> receivedBuffer = new List <byte>();
                    byte        channelIndex   = 12;
                    var         stream         = demuxer.GetStream(channelIndex, channelIndex);

                    // Receive 600 bytes in 3 messages. Exclude 1 channel index byte per message, and 2 port bytes in the first message.
                    // expect 600 - 3 - 2 = 595 bytes payload.
                    int expectedCount = 595;

                    var t = Task.Run(async() =>
                    {
                        await ws.InvokeReceiveAsync(
                            new ArraySegment <byte>(GenerateRandomBuffer(100, channelIndex, 0xB1, true)),
                            WebSocketMessageType.Binary, true).ConfigureAwait(false);
                        await ws.InvokeReceiveAsync(
                            new ArraySegment <byte>(GenerateRandomBuffer(200, channelIndex, 0xB2, false)),
                            WebSocketMessageType.Binary, true).ConfigureAwait(false);
                        await ws.InvokeReceiveAsync(
                            new ArraySegment <byte>(GenerateRandomBuffer(300, channelIndex, 0xB3, false)),
                            WebSocketMessageType.Binary, true).ConfigureAwait(false);

                        await WaitForAsync(() => receivedBuffer.Count == expectedCount).ConfigureAwait(false);
                        await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "normal", CancellationToken.None).ConfigureAwait(false);
                    });
                    var buffer = new byte[50];
                    while (true)
                    {
                        var cRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

                        if (cRead == 0)
                        {
                            break;
                        }

                        for (int i = 0; i < cRead; i++)
                        {
                            receivedBuffer.Add(buffer[i]);
                        }
                    }

                    await t.ConfigureAwait(false);

                    Assert.True(
                        receivedBuffer.Count == expectedCount,
                        $"Demuxer error: expect to receive {expectedCount} bytes, but actually got {receivedBuffer.Count} bytes.");
                    Assert.True(receivedBuffer[0] == 0xB1, "The first payload incorrect!");
                    Assert.True(receivedBuffer[96] == 0xB1, "The first payload incorrect!");
                    Assert.True(receivedBuffer[97] == 0xB2, "The second payload incorrect!");
                    Assert.True(receivedBuffer[295] == 0xB2, "The second payload incorrect!");
                    Assert.True(receivedBuffer[296] == 0xB3, "The third payload incorrect!");
                    Assert.True(receivedBuffer[594] == 0xB3, "The third payload incorrect!");
                }
        }
示例#2
0
        public async Task ReceiveDataRemoteCommand()
        {
            using (MockWebSocket ws = new MockWebSocket())
            {
                StreamDemuxer demuxer = new StreamDemuxer(ws);
                Task.Run(() => demuxer.Start());

                List <byte> receivedBuffer = new List <byte>();
                byte        channelIndex   = 12;
                var         stream         = demuxer.GetStream(channelIndex, channelIndex);

                // Receive 600 bytes in 3 messages. Exclude 1 channel index byte per message, expect 597 bytes payload.
                int expectedCount = 597;

                var t = Task.Run(async() =>
                {
                    await ws.InvokeReceiveAsync(new ArraySegment <byte>(GenerateRandomBuffer(100, channelIndex, 0xAA, false)), WebSocketMessageType.Binary, true);
                    await ws.InvokeReceiveAsync(new ArraySegment <byte>(GenerateRandomBuffer(200, channelIndex, 0xAB, false)), WebSocketMessageType.Binary, true);
                    await ws.InvokeReceiveAsync(new ArraySegment <byte>(GenerateRandomBuffer(300, channelIndex, 0xAC, false)), WebSocketMessageType.Binary, true);

                    await WaitForAsync(() => receivedBuffer.Count == expectedCount);
                    await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "normal", CancellationToken.None);
                });
                var buffer = new byte[50];
                while (true)
                {
                    var cRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                    if (cRead == 0)
                    {
                        break;
                    }
                    for (int i = 0; i < cRead; i++)
                    {
                        receivedBuffer.Add(buffer[i]);
                    }
                }
                await t;

                Assert.True(receivedBuffer.Count == expectedCount, $"Demuxer error: expect to receive {expectedCount} bytes, but actually got {receivedBuffer.Count} bytes.");
                Assert.True(receivedBuffer[0] == 0xAA, "The first payload incorrect!");
                Assert.True(receivedBuffer[98] == 0xAA, "The first payload incorrect!");
                Assert.True(receivedBuffer[99] == 0xAB, "The second payload incorrect!");
                Assert.True(receivedBuffer[297] == 0xAB, "The second payload incorrect!");
                Assert.True(receivedBuffer[298] == 0xAC, "The third payload incorrect!");
                Assert.True(receivedBuffer[596] == 0xAC, "The third payload incorrect!");
            }
        }
示例#3
0
        public async Task ReceiveDataPortForwardMultipleStream()
        {
            using (MockWebSocket ws = new MockWebSocket())
                using (StreamDemuxer demuxer = new StreamDemuxer(ws, StreamType.PortForward))
                {
                    demuxer.Start();

                    List <byte> receivedBuffer1 = new List <byte>();
                    byte        channelIndex1   = 1;
                    var         stream1         = demuxer.GetStream(channelIndex1, channelIndex1);
                    List <byte> receivedBuffer2 = new List <byte>();
                    byte        channelIndex2   = 2;
                    var         stream2         = demuxer.GetStream(channelIndex2, channelIndex2);

                    // stream 1: receive 100 + 300 = 400 bytes, exclude 1 channel index per message, exclude port bytes in the first message,
                    // expect 400 - 1 x 2 - 2 = 396 bytes.
                    int expectedCount1 = 396;

                    // stream 2: receive 200 bytes, exclude 1 channel index per message, exclude port bytes in the first message,
                    // expect 200 - 1 - 2 = 197 bytes.
                    int expectedCount2 = 197;

                    var t1 = Task.Run(async() =>
                    {
                        // Simulate WebSocket received remote data to multiple streams
                        await ws.InvokeReceiveAsync(
                            new ArraySegment <byte>(GenerateRandomBuffer(100, channelIndex1, 0xE1, true)),
                            WebSocketMessageType.Binary, true).ConfigureAwait(false);
                        await ws.InvokeReceiveAsync(
                            new ArraySegment <byte>(GenerateRandomBuffer(200, channelIndex2, 0xE2, true)),
                            WebSocketMessageType.Binary, true).ConfigureAwait(false);
                        await ws.InvokeReceiveAsync(
                            new ArraySegment <byte>(GenerateRandomBuffer(300, channelIndex1, 0xE3, false)),
                            WebSocketMessageType.Binary, true).ConfigureAwait(false);

                        await WaitForAsync(() => receivedBuffer1.Count == expectedCount1).ConfigureAwait(false);
                        await WaitForAsync(() => receivedBuffer2.Count == expectedCount2).ConfigureAwait(false);
                        await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "normal", CancellationToken.None).ConfigureAwait(false);
                    });
                    var t2 = Task.Run(async() =>
                    {
                        var buffer = new byte[50];
                        while (true)
                        {
                            var cRead = await stream1.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
                            if (cRead == 0)
                            {
                                break;
                            }

                            for (int i = 0; i < cRead; i++)
                            {
                                receivedBuffer1.Add(buffer[i]);
                            }
                        }
                    });
                    var t3 = Task.Run(async() =>
                    {
                        var buffer = new byte[50];
                        while (true)
                        {
                            var cRead = await stream2.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
                            if (cRead == 0)
                            {
                                break;
                            }

                            for (int i = 0; i < cRead; i++)
                            {
                                receivedBuffer2.Add(buffer[i]);
                            }
                        }
                    });
                    await Task.WhenAll(t1, t2, t3).ConfigureAwait(false);

                    Assert.True(
                        receivedBuffer1.Count == expectedCount1,
                        $"Demuxer error: expect to receive {expectedCount1} bytes, but actually got {receivedBuffer1.Count} bytes.");
                    Assert.True(
                        receivedBuffer2.Count == expectedCount2,
                        $"Demuxer error: expect to receive {expectedCount2} bytes, but actually got {receivedBuffer2.Count} bytes.");
                    Assert.True(receivedBuffer1[0] == 0xE1, "The first payload incorrect!");
                    Assert.True(receivedBuffer1[96] == 0xE1, "The first payload incorrect!");
                    Assert.True(receivedBuffer1[97] == 0xE3, "The second payload incorrect!");
                    Assert.True(receivedBuffer1[395] == 0xE3, "The second payload incorrect!");
                    Assert.True(receivedBuffer2[0] == 0xE2, "The first payload incorrect!");
                    Assert.True(receivedBuffer2[196] == 0xE2, "The first payload incorrect!");
                }
        }