public bool ReadFrame(out PipeFrame frame)
        {
            if (!this.IsConnected)
            {
                throw new InvalidPipeException("Cannot read Native Stream as pipe is not connected");
            }
            int count = NativePipe.ReadFrame(this._buffer, this._buffer.Length);

            if (count <= 0)
            {
                this._lasterr = NativePipe.PipeReadError.ReadEmptyMessage;
                if (count < 0)
                {
                    this._lasterr = (NativePipe.PipeReadError)count;
                    this.Logger.Error("Native pipe failed to read: {0}", (object)this._lasterr.ToString());
                    this.Close();
                }
                frame = new PipeFrame();
                return(false);
            }
            using (MemoryStream memoryStream = new MemoryStream(this._buffer, 0, count))
            {
                frame = new PipeFrame();
                if (frame.ReadStream((Stream)memoryStream) && frame.Length != 0U)
                {
                    return(true);
                }
                this.Logger.Error("Pipe failed to read from the data received by the stream.");
                return(false);
            }
        }
示例#2
0
        /// <summary>
        /// Attempts to read a frame
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public bool ReadFrame(out PipeFrame frame)
        {
            //Make sure we are connected
            if (!IsConnected)
            {
                throw new InvalidPipeException("Cannot read Native Stream as pipe is not connected");
            }

            //Try and read the frame from the native pipe
            int bytesRead = NativePipe.ReadFrame(_buffer, _buffer.Length);

            if (bytesRead <= 0)
            {
                //Update the error message
                _lasterr = NativePipe.PipeReadError.ReadEmptyMessage;

                //A error actively occured. If it is 0 we just read no bytes.
                if (bytesRead < 0)
                {
                    //We have a pretty bad error, we will log it for prosperity.
                    _lasterr = (NativePipe.PipeReadError)bytesRead;
                    Logger.Error("Native pipe failed to read: {0}", _lasterr.ToString());

                    //Close this pipe
                    this.Close();
                }

                //Return a empty frame and return false (read failure).
                frame = default(PipeFrame);
                return(false);
            }

            //Parse the pipe
            using (MemoryStream stream = new MemoryStream(_buffer, 0, bytesRead))
            {
                //Try to parse the stream
                frame = new PipeFrame();
                if (frame.ReadStream(stream) && frame.Length != 0)
                {
                    return(true);
                }

                //We failed
                Logger.Error("Pipe failed to read from the data received by the stream.");
                return(false);
            }
        }