Exemplo n.º 1
0
        private bool AttemptConnection(int pipe)
        {
            if (IsConnected)
            {
                Logger.Error("Cannot connect as the pipe is already connected");
                throw new InvalidPipeException("Cannot connect as the pipe is already connected");
            }

            //Prepare the pipe name
            string pipename = string.Format(PIPE_NAME, pipe);

            Logger.Info("Attempting to connect to " + pipename);

            uint err = NativePipe.Open(pipename);

            if (err == 0 && IsConnected)
            {
                Logger.Info("Succesfully connected to " + pipename);
                _connectedPipe = pipe;
                return(true);
            }
            else
            {
                Logger.Error("Failed to connect to native pipe. Err: {0}", err);
                return(false);
            }
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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));
            }
        }
Exemplo n.º 4
0
 /// <summary>
 /// Closes the pipe
 /// </summary>
 public void Close()
 {
     NativePipe.Close();
 }