Number of bytes transmitted by TcpClient through NetworkStream BinaryReader / BinaryWriter

I use the network protocol built around TcpClient, using BinaryReaderto read bytes from the base NetworkStream(and vice versa, using BinaryWriterto write).

The protocol transmits strings in UTF-8 encoding and calls it reader.ReadString()for reading from the stream (using writer.Write(someStr)for writing).

Is there an easy way to determine the number of bytes read (or written) in a NetworkStream without having to jump through hoops to calculate the actual byte lengths of the transmitted strings?

Note that it BinaryWriter.Write()writes an integer with 7-bit encoding before the actual bytes of the string, which makes any manual calculation extra complicated.

Also note that it NetworkStreamdoes not support the property Position, as it complains of impossibility Seek.

In addition, I would like to avoid introducing intermediaries who must copy / scan data into the read / write process so as not to affect the performance of the entire system.

Is there a simple, high-level way to count bytes passing through a network interface without having to manually consider the encoding and string length?

+4
source share
2 answers

You can insert a custom stream between the network stream and a reader that counts bytes.

. , .

+1

, , , ( , ):

using System;
using System.IO;

namespace Streams
{
    /// <summary>
    /// A wrapper around a <see cref="Stream"/> that keeps track of the number of bytes read and written.
    /// </summary>
    public class ByteCountingStream : Stream
    {
        private readonly Stream inner;

        private long totalBytesWritten;
        private long totalBytesRead;


        public ByteCountingStream(Stream inner)
        {
            this.inner = inner;
        }

        public override void Flush()
        {
            inner.Flush();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(long value)
        {
            throw new NotImplementedException();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int readBytes = inner.Read(buffer, offset, count);
            totalBytesRead += readBytes;
            return readBytes;
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            inner.Write(buffer, offset, count);
            totalBytesWritten += count;
        }

        public override bool CanRead => true;
        public override bool CanSeek => false;
        public override bool CanWrite => true;

        public override long Length
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public override long Position { get; set; }

        public long TotalBytesWritten => totalBytesWritten;
        public long TotalBytesRead => totalBytesRead;
    }
}

, .

0

Source: https://habr.com/ru/post/1619961/


All Articles