/// <summary> /// Attempts to write a frame /// </summary> /// <param name="frame"></param> /// <returns></returns> public bool WriteFrame(PipeFrame frame) { if (!IsConnected) { throw new Exception("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(); //Copy the bytes into a new marshaled block int size = Marshal.SizeOf(bytes[0]) * bytes.Length; IntPtr pnt = Marshal.AllocHGlobal(size); try { //Send the marshaled block Marshal.Copy(bytes, 0, pnt, bytes.Length); return(NativePipeExtern.WriteFrame(pnt, bytes.Length)); } finally { //Finally, before exiting the try catch, free the memory we assigned. Marshal.FreeHGlobal(pnt); } } }
public bool WriteFrame(PipeFrame frame) { if (_isDisposed) { throw new ObjectDisposedException("_stream"); } //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 //This can only happen on the main thread so it should be fine. Logger.Trace("Writing frame"); 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); }