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);
            }
        }
Exemple #2
0
        public bool WriteFrame(PipeFrame frame)
        {
            //Write the frame. We are assuming proper duplex connection here
            if (!IsConnected)
            {
                Logger.Error("Failed to write frame because the stream is closed");
                return(false);
            }

            try
            {
                //Write the pipe
                frame.WriteStream(_stream);
                return(true);
            }
            catch (IOException io)
            {
                Logger.Error("Failed to write frame because of a IO Exception: {0}", io.Message);
            }
            catch (ObjectDisposedException)
            {
                Logger.Warning("Failed to write frame as the stream was already disposed");
            }
            catch (InvalidOperationException)
            {
                Logger.Warning("Failed to write frame because of a invalid operation");
            }

            //We must have failed the try catch
            return(false);
        }
 public bool WriteFrame(PipeFrame frame)
 {
     if (this._isDisposed)
     {
         throw new ObjectDisposedException("_stream");
     }
     if (!this._isClosed)
     {
         if (this.IsConnected)
         {
             try
             {
                 frame.WriteStream((Stream)this._stream);
                 return(true);
             }
             catch (IOException ex)
             {
                 this.Logger.Error("Failed to write frame because of a IO Exception: {0}", (object)ex.Message);
             }
             catch (ObjectDisposedException ex)
             {
                 this.Logger.Warning("Failed to write frame as the stream was already disposed");
             }
             catch (InvalidOperationException ex)
             {
                 this.Logger.Warning("Failed to write frame because of a invalid operation");
             }
             return(false);
         }
     }
     this.Logger.Error("Failed to write frame because the stream is closed");
     return(false);
 }
Exemple #4
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();
            }
        }
 public bool WriteFrame(PipeFrame frame)
 {
     if (!this.IsConnected)
     {
         throw new InvalidPipeException("Cannot write Native Stream as pipe is not connected");
     }
     using (MemoryStream memoryStream = new MemoryStream())
     {
         frame.WriteStream((Stream)memoryStream);
         byte[] array = memoryStream.ToArray();
         return(NativePipe.WriteFrame(array, array.Length));
     }
 }
        /// <summary>
        /// Reads a frame, returning false if none are available
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public bool ReadFrame(out PipeFrame frame)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("_stream");
            }

            //Check the queue, returning the pipe if we have anything available. Otherwise null.
            var dequeued = _framequeue.TryDequeue(out frame);

            if (!dequeued)
            {
                frame = default(PipeFrame);
            }
            return(dequeued);
        }
        /// <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);
            }
        }
Exemple #8
0
        public bool ReadFrame(out PipeFrame frame)
        {
            //Check the queue, returning the pipe if we have anything available. Otherwise null.
            lock (_framequeuelock)
            {
                if (_framequeue.Count == 0)
                {
                    //We found nothing, so just default and return null
                    frame = default(PipeFrame);
                    return(false);
                }

                //Return the dequed frame
                frame = _framequeue.Dequeue();
                return(true);
            }
        }
 public bool ReadFrame(out PipeFrame frame)
 {
     if (this._isDisposed)
     {
         throw new ObjectDisposedException("_stream");
     }
     lock (this._framequeuelock)
     {
         if (this._framequeue.Count == 0)
         {
             frame = new PipeFrame();
             return(false);
         }
         frame = this._framequeue.Dequeue();
         return(true);
     }
 }
        /// <summary>
        /// Attempts to write a frame
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public bool WriteFrame(PipeFrame frame)
        {
            if (!IsConnected)
            {
                throw new InvalidPipeException("Cannot write Native Stream as pipe is not connected");
            }

            //Create a memory stream so we can write it to the pipe
            using (MemoryStream stream = new MemoryStream())
            {
                //Write the stream and the send it to the pipe
                frame.WriteStream(stream);

                //Get the bytes and send it
                byte[] bytes = stream.ToArray();
                return(NativePipe.WriteFrame(bytes, bytes.Length));
            }
        }
        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>
        /// Writes a frame to the pipe
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        public bool WriteFrame(PipeFrame frame)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("_stream");
            }

            //Write the frame. We are assuming proper duplex connection here
            if (_isClosed || !IsConnected)
            {
                Console.WriteLine("Failed to write frame because the stream is closed");
                return(false);
            }

            try
            {
                //Write the pipe
                //This can only happen on the main thread so it should be fine.
                frame.WriteStream(_stream);
                return(true);
            }
            catch (IOException io)
            {
                Console.WriteLine($"Failed to write frame because of a IO Exception: {io.Message}");
            }
            catch (ObjectDisposedException)
            {
                Console.WriteLine("Failed to write frame as the stream was already disposed");
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine("Failed to write frame because of a invalid operation");
            }

            //We must have failed the try catch
            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();
            }
        }
Exemple #14
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();
        }