Exemple #1
0
        public async Task <int> SendAsync(FrameStream frames, CancellationToken token)
        {
            var bytesSent = 0;

            foreach (var frame in frames.GetBuffers())
            {
                var frameBytes = new byte[4];
                MessageFramingUtil.EncodeLength((int)frame.Length, frameBytes, 0);
                await _stream.WriteAsync(frameBytes, 0, 4, token);

                bytesSent += 4;
                await _stream.WriteAsync(frame.GetBuffer(), 0, (int)frame.Length, token);

                bytesSent += (int)frame.Length;
            }
            return(bytesSent);
        }
Exemple #2
0
        public async Task <FrameStream> ReceiveAsync(CancellationToken token)
        {
            var result         = new FrameStream();
            var frameSizeBytes = new byte[4];

            await ReadBytesAsync(_stream, frameSizeBytes, 0, 4, token);

            var frameSize = MessageFramingUtil.DecodeLength(frameSizeBytes, 0);

            if (frameSize == 0)
            {
                return(FrameStream.EMPTY);
            }

            var frame = new MemoryStream(new byte[frameSize], 0, frameSize, true, true);

            await ReadBytesAsync(_stream, frame.GetBuffer(), 0, frameSize, token);

            frame.SetLength(frameSize);
            result.AppendFrame(frame);
            return(result);
        }