Example #1
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);
            }
        }
Example #2
0
        /// <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));
            }
        }
Example #3
0
        /// <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.
            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);
            }
        }
Example #4
0
        /// <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)
            {
                Logger.Error("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)
            {
                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);
        }
Example #5
0
        /// <summary>
        /// Ends a read. Can be executed in another thread.
        /// </summary>
        /// <param name="callback"></param>
        private void tEndRead(IAsyncResult callback)
        {
            Logger.Info("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.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 memory = new MemoryStream(_buffer, 0, bytes))
                {
                    try
                    {
                        PipeFrame frame = new PipeFrame();
                        if (frame.ReadStream(memory))
                        {
                            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.");
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Error("A exception has occured while trying to parse the pipe data: " + e.Message);
                    }
                }
            }

            //We are still connected, so continue to read
            if (!_isClosed && IsConnected)
            {
                Logger.Info("Starting another read");
                tBeginRead();
            }
        }