コード例 #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);
            }
        }
コード例 #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 (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();
            }
        }