private static async Task WebSocketToPipeWorker(WebSocket socket, IMessagePipeEnd pipe, CancellationToken cancellationToken)
        {
            const int blockSize = 0x10000;
            var       buffer    = new MemoryStream(blockSize);

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                int index = (int)buffer.Length;
                buffer.SetLength(index + blockSize);
                buffer.TryGetBuffer(out ArraySegment <byte> bufferSegment);

                var wsrr = await socket.ReceiveAsync(new ArraySegment <byte>(bufferSegment.Array, index, blockSize), cancellationToken);

                buffer.SetLength(index + wsrr.Count);

                if (wsrr.CloseStatus != null)
                {
                    break;
                }

                if (wsrr.EndOfMessage)
                {
                    pipe.Write(buffer.ToArray());
                    buffer.SetLength(0);
                }
            }
        }
Example #2
0
        private async Task HostToClientWorker(Stream stream, IMessagePipeEnd pipe)
        {
            var sizeBuf = new byte[sizeof(int)];

            while (true)
            {
                if (!await FillFromStreamAsync(stream, sizeBuf))
                {
                    break;
                }
                int size = BitConverter.ToInt32(sizeBuf, 0);

                var message = new byte[size];
                if (!await FillFromStreamAsync(stream, message))
                {
                    break;
                }

                pipe.Write(message);
            }

            pipe.Write(_endMessage);
        }
Example #3
0
        private static async Task WebSocketToPipeWorker(WebSocket socket, IMessagePipeEnd pipe, CancellationToken cancellationToken) {
            const int blockSize = 0x10000;
            var buffer = new MemoryStream(blockSize);

            while (true) {
                cancellationToken.ThrowIfCancellationRequested();

                int index = (int)buffer.Length;
                buffer.SetLength(index + blockSize);

                var wsrr = await socket.ReceiveAsync(new ArraySegment<byte>(buffer.GetBuffer(), index, blockSize), cancellationToken);
                buffer.SetLength(index + wsrr.Count);

                if (wsrr.CloseStatus != null) {
                    break;
                } else if (wsrr.EndOfMessage) {
                    pipe.Write(buffer.ToArray());
                    buffer.SetLength(0);
                }
            }
        }
Example #4
0
        private async Task HostToClientWorker(Stream stream, IMessagePipeEnd pipe) {
            var sizeBuf = new byte[sizeof(int)];
            while (true) {
                if (!await FillFromStreamAsync(stream, sizeBuf)) {
                    break;
                }
                int size = BitConverter.ToInt32(sizeBuf, 0);

                var message = new byte[size];
                if (!await FillFromStreamAsync(stream, message)) {
                    break;
                }

                pipe.Write(message);
            }
        }