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); } }
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); } }
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> /// 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); } }
/// <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)); } }
private bool AttemptConnection(int pipe) { if (this.IsConnected) { this.Logger.Error("Cannot connect as the pipe is already connected"); throw new InvalidPipeException("Cannot connect as the pipe is already connected"); } string pipename = string.Format("\\\\?\\pipe\\discord-ipc-{0}", (object)pipe); this.Logger.Info("Attempting to connect to " + pipename); uint num = NativePipe.Open(pipename); if (num == 0U && this.IsConnected) { this.Logger.Info("Succesfully connected to " + pipename); this._connectedPipe = pipe; return(true); } this.Logger.Error("Failed to connect to native pipe. Err: {0}", (object)num); return(false); }
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> /// Closes the pipe /// </summary> public void Close() { NativePipe.Close(); }