Exemple #1
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);
        }
        /// <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 #3
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);
            }
        }
        /// <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));
            }
        }
Exemple #5
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 (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("A unkown error has occured while reading a pipe: " + e.Message);
                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))
                {
                    try
                    {
                        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.");
                        }
                    }
                    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 (IsConnected)
            {
                Logger.Info("Starting another read");
                BeginRead();
            }
        }