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);
            }
        }
Esempio n. 2
0
        private void EndRead(IAsyncResult callback)
        {
            Logger.Info("Ending Read");
            int bytes = 0;

            try
            {
                //Attempt to read the bytes, catching for IO exceptions or dispose exceptions
                bytes = _stream.EndRead(callback);
            }
            catch (IOException)
            {
                Logger.Warning("Attempted to end reading from a closed pipe");
                return;
            }
            catch (ObjectDisposedException)
            {
                Logger.Warning("Attemped to end reading from a disposed pipe");
                return;
            }

            //How much did we read?
            Logger.Info("Read {0} bytes", bytes);

            //Did we read anything? If we did we should enqueue it.
            if (bytes > 0)
            {
                //Load it into a memory stream and read the frame
                using (MemoryStream stream = new MemoryStream(_buffer, 0, bytes))
                {
                    PipeFrame frame = new PipeFrame();
                    if (frame.ReadStream(stream))
                    {
                        Logger.Info("Read a frame: {0}", frame.Opcode);

                        //Enqueue the stream
                        lock (_framequeuelock)
                            _framequeue.Enqueue(frame);
                    }
                    else
                    {
                        //TODO: Enqueue a pipe close event here as we failed to read something.
                        Logger.Error("Pipe failed to read from the data received by the stream.");
                    }
                }
            }

            //We are still connected, so continue to read
            if (IsConnected)
            {
                Logger.Info("Starting another read");
                BeginRead();
            }
        }
        /// <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);
            }
        }
        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)
            {
                //A error actively occured. If it is 0 we just read no bytes.
                if (bytesRead < 0)
                {
                    Logger.Error("Native pipe failed to read. Err: {0}", bytesRead);
                }

                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);
            }
        }
        /// <summary>
        /// Ends a read. Can be executed in another thread.
        /// </summary>
        /// <param name="callback"></param>
        private void TEndRead(IAsyncResult callback)
        {
            Console.WriteLine("Ending Read");
            int bytes = 0;

            try
            {
                //Attempt to read the bytes, catching for IO exceptions or dispose exceptions
                lock (l_stream)
                {
                    //Make sure the stream is still valid
                    if (_stream == null || !_stream.IsConnected)
                    {
                        return;
                    }

                    //Read our btyes
                    bytes = _stream.EndRead(callback);
                }
            }
            catch (IOException)
            {
                Console.WriteLine("Attempted to end reading from a closed pipe");
                return;
            }
            catch (NullReferenceException)
            {
                Console.WriteLine("Attempted to read from a null pipe");
                return;
            }
            catch (ObjectDisposedException)
            {
                Console.WriteLine("Attemped to end reading from a disposed pipe");
                return;
            }
            catch (Exception e)
            {
                Console.WriteLine($"An exception occured while ending a read of a stream: {e.Message}");
                Console.WriteLine(e.StackTrace);
                return;
            }

            //How much did we read?
            Console.WriteLine($"Read {bytes} bytes");

            //Did we read anything? If we did we should enqueue it.
            if (bytes > 0)
            {
                //Load it into a memory stream and read the frame
                using (MemoryStream memory = new MemoryStream(_buffer, 0, bytes))
                {
                    try
                    {
                        PipeFrame frame = new PipeFrame();
                        if (frame.ReadStream(memory))
                        {
                            Console.WriteLine($"Read a frame: {frame.Opcode}");

                            //Enqueue the stream
                            lock (_framequeuelock)
                                _framequeue.Enqueue(frame);
                        }
                        else
                        {
                            //TODO: Enqueue a pipe close event here as we failed to read something.
                            Console.WriteLine("Pipe failed to read from the data received by the stream.");
                            Close();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("A exception has occured while trying to parse the pipe data: " + e.Message);
                        Close();
                    }
                }
            }

            //We are still connected, so continue to read
            if (!_isClosed && IsConnected)
            {
                Console.WriteLine("Starting another read");
                TBeginRead();
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Ends a read. Can be executed in another thread.
        /// </summary>
        /// <param name="callback"></param>
        private void EndReadStream(IAsyncResult callback)
        {
            Logger.Trace("Ending Read");
            int bytes = 0;

            try
            {
                //Attempt to read the bytes, catching for IO exceptions or dispose exceptions
                lock (l_stream)
                {
                    //Make sure the stream is still valid
                    if (_stream == null || !_stream.IsConnected)
                    {
                        return;
                    }

                    //Read our btyes
                    bytes = _stream.EndRead(callback);
                }
            }
            catch (IOException)
            {
                Logger.Warning("Attempted to end reading from a closed pipe");
                return;
            }
            catch (NullReferenceException)
            {
                Logger.Warning("Attempted to read from a null pipe");
                return;
            }
            catch (ObjectDisposedException)
            {
                Logger.Warning("Attemped to end reading from a disposed pipe");
                return;
            }
            catch (Exception e)
            {
                Logger.Error("An exception occured while ending a read of a stream: {0}", e.Message);
                Logger.Error(e.StackTrace);
                return;
            }

            //How much did we read?
            Logger.Trace("Read {0} bytes", bytes);

            //Did we read anything? If we did we should enqueue it.
            if (bytes > 0)
            {
                //Load it into a memory stream and read the frame
                using (MemoryStream memory = new MemoryStream(_buffer, 0, bytes))
                {
                    try
                    {
                        PipeFrame frame = new PipeFrame();
                        if (frame.ReadStream(memory))
                        {
                            Logger.Trace("Read a frame: {0}", frame.Opcode);

                            //Enqueue the stream
                            lock (_framequeuelock)
                                _framequeue.Enqueue(frame);
                        }
                        else
                        {
                            //TODO: Enqueue a pipe close event here as we failed to read something.
                            Logger.Error("Pipe failed to read from the data received by the stream.");
                            Close();
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error("A exception has occured while trying to parse the pipe data: " + e.Message);
                        Close();
                    }
                }
            }
            else
            {
                //If we read 0 bytes, its probably a broken pipe. However, I have only confirmed this is the case for MacOSX.
                // I have added this check here just so the Windows builds are not effected and continue to work as expected.
                if (IsUnix())
                {
                    Logger.Error("Empty frame was read on " + Environment.OSVersion.ToString() + ", aborting.");
                    Close();
                }
                else
                {
                    Logger.Warning("Empty frame was read. Please send report to Lachee.");
                }
            }

            //We are still connected, so continue to read
            if (!_isClosed && IsConnected)
            {
                Logger.Trace("Starting another read");
                BeginReadStream();
            }
        }
        private void tEndRead(IAsyncResult callback)
        {
            this.Logger.Info("Ending Read");
            int count = 0;

            try
            {
                lock (this.l_stream)
                {
                    if (this._stream == null || !this._stream.IsConnected)
                    {
                        return;
                    }
                    count = this._stream.EndRead(callback);
                }
            }
            catch (IOException ex)
            {
                this.Logger.Warning("Attempted to end reading from a closed pipe");
                return;
            }
            catch (NullReferenceException ex)
            {
                this.Logger.Warning("Attempted to read from a null pipe");
                return;
            }
            catch (ObjectDisposedException ex)
            {
                this.Logger.Warning("Attemped to end reading from a disposed pipe");
                return;
            }
            catch (Exception ex)
            {
                this.Logger.Error("An exception occured while ending a read of a stream: {0}", (object)ex.Message);
                this.Logger.Error(ex.StackTrace);
                return;
            }
            this.Logger.Info("Read {0} bytes", (object)count);
            if (count > 0)
            {
                using (MemoryStream memoryStream = new MemoryStream(this._buffer, 0, count))
                {
                    try
                    {
                        PipeFrame pipeFrame = new PipeFrame();
                        if (pipeFrame.ReadStream((Stream)memoryStream))
                        {
                            this.Logger.Info("Read a frame: {0}", (object)pipeFrame.Opcode);
                            lock (this._framequeuelock)
                                this._framequeue.Enqueue(pipeFrame);
                        }
                        else
                        {
                            this.Logger.Error("Pipe failed to read from the data received by the stream.");
                            this.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Logger.Error("A exception has occured while trying to parse the pipe data: " + ex.Message);
                        this.Close();
                    }
                }
            }
            if (this._isClosed || !this.IsConnected)
            {
                return;
            }
            this.Logger.Info("Starting another read");
            this.tBeginRead();
        }