public override async Task <byte[]> ReadBlockSizeAsync(PipeNetworkStream stream, int count)
#endif
        {
            List <byte> bytes        = new List <byte>();
            int         lengthReaded = 0;

            while (lengthReaded < count)
            {
                int countToRead = count;
                if (lengthReaded + countToRead > count)
                {
                    countToRead = count - lengthReaded;
                }
                //#if (!PORTABLE)
                //                Console.WriteLine("countToRead: " + countToRead);
                //#endif
                byte[] readBytes = new byte[countToRead];
#if (NET35 || NET40)
                int readCount = stream.Read(readBytes, countToRead);
#else
                int readCount = await stream.ReadAsync(readBytes, countToRead);
#endif
                if (readCount <= 0)
                {
                    throw new Exception("read zero buffer! client disconnected: " + readCount);
                }
                lengthReaded += readCount;
                bytes.AddRange(readBytes.ToList().GetRange(0, readCount));
            }
            return(bytes.ToArray());
        }
        public override Task WriteToStreamAsync(PipeNetworkStream stream, byte[] data)
#endif
        {
#if (NET35 || NET40)
            stream.Write(data, 0, data.Length);
#else
            return(stream.WriteAsync(data, 0, data.Length));
#endif
        }
        public override async Task <byte[]> ReadBlockToEndAsync(PipeNetworkStream stream, CompressMode compress, int maximum)
#endif
        {
#if (NET35 || NET40)
            byte[] lenBytes = ReadBlockSize(stream, 4);
            int    len      = BitConverter.ToInt32(lenBytes, 0);
            return(ReadBlockSize(stream, len));
#else
            byte[] lenBytes = await ReadBlockSizeAsync(stream, 4);

            int len = BitConverter.ToInt32(lenBytes, 0);
            return(await ReadBlockSizeAsync(stream, len));
#endif
        }
        public virtual byte[] ReadBlockToEnd(PipeNetworkStream stream, CompressMode compress, int maximum)
        {
            //first 4 bytes are size of block
            byte[] dataLenByte = ReadBlockSize(stream, 4);
            //convert bytes to int
            int dataLength = BitConverter.ToInt32(dataLenByte, 0);

            if (dataLength > maximum)
            {
                throw new Exception("dataLength is upper than maximum :" + dataLength);
            }
            //read a block
            byte[] dataBytes = ReadBlockSize(stream, dataLength);
            return(dataBytes);
        }
        public virtual byte[] ReadBlockSize(PipeNetworkStream stream, int count)
        {
            List <byte> bytes        = new List <byte>();
            int         lengthReaded = 0;

            while (lengthReaded < count)
            {
                int countToRead = count;
                if (lengthReaded + countToRead > count)
                {
                    countToRead = count - lengthReaded;
                }
                byte[] readBytes = new byte[countToRead];
                int    readCount = stream.Read(readBytes, countToRead);
                if (readCount <= 0)
                {
                    throw new Exception("read zero buffer! client disconnected: " + readCount);
                }
                lengthReaded += readCount;
                bytes.AddRange(readBytes.ToList().GetRange(0, readCount));
            }
            return(bytes.ToArray());
        }
 /// <summary>
 /// read one byte from server
 /// </summary>
 /// <param name="stream">stream to read</param>
 /// <param name="compress">compress mode</param>
 /// <param name="maximum">maximum read</param>
 /// <param name="isWebSocket">if reading socket is websocket</param>
 /// <returns></returns>
 public virtual byte ReadOneByte(PipeNetworkStream stream)
 {
     return(stream.ReadOneByte());
 }
 public void WriteBlockToStream(PipeNetworkStream stream, byte[] data)
 {
     byte[] size = BitConverter.GetBytes(data.Length);
     stream.Write(size, 0, data.Length);
     stream.Write(data, 0, data.Length);
 }
 public Task WriteBlockToStreamAsync(PipeNetworkStream stream, byte[] data)
 {
     byte[] size = BitConverter.GetBytes(data.Length);
     return(stream.WriteAsync(size, 0, size.Length).ContinueWith((t) => stream.WriteAsync(data, 0, data.Length)));
 }
 public virtual Task WriteToStreamAsync(PipeNetworkStream stream, byte[] data)
 {
     return(stream.WriteAsync(data, 0, data.Length));
 }
 public virtual void WriteToStream(PipeNetworkStream stream, byte[] data)
 {
     stream.Write(data, 0, data.Length);
 }
 public override Task <byte> ReadOneByteAsync(PipeNetworkStream stream)
 {
     return(stream.ReadOneByteAsync());
 }
 public override byte ReadOneByte(PipeNetworkStream stream)
 {
     return(stream.ReadOneByte());
 }
 public override byte[] ReadBlockToEnd(PipeNetworkStream stream, CompressMode compress, int maximum)
 public override byte[] ReadBlockSize(PipeNetworkStream stream, int count)
 public virtual Task <byte> ReadOneByteAsync(PipeNetworkStream stream)
 {
     return(stream.ReadOneByteAsync());
 }
예제 #16
0
 public PipeNormalStream(PipeNetworkStream pipeNetworkStream)
 {
     _pipeNetworkStream = pipeNetworkStream;
 }
 public override void WriteToStream(PipeNetworkStream stream, byte[] data)