/// <summary> /// Writes an instance of a MockMessage to a given pipe stream connection /// </summary> /// <param name="connectionId">The connection Id to which to write the message</param> /// <param name="message">The message instance that will be written to the pipe</param> public void WriteMessage(int connectionId, MockMessage message) { try { System.Diagnostics.Trace.WriteLine( "WriteMessage() called", "TransMock.Communication.NamedPipe.StreamingNamedPipeServer"); var formatter = new BinaryFormatter(); using (MemoryStream msgStream = new MemoryStream(4096)) { System.Diagnostics.Debug.WriteLine( "Serializing the message to a stream.", "TransMock.Communication.NamedPipe.StreamingNamedPipeServer"); formatter.Serialize(msgStream, message); // Writing EndOfMessage sequence msgStream.Write( NamedPipeMessageUtils.EndOfMessage, 0, NamedPipeMessageUtils.EndOfMessage.Length); msgStream.Seek(0, SeekOrigin.Begin); this.WriteStream(connectionId, msgStream); } System.Diagnostics.Trace.WriteLine( "WriteMessage() succeeded", "TransMock.Communication.NamedPipe.StreamingNamedPipeServer"); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine( "WriteStream() trhew an exception: " + ex.Message, "TransMock.Communication.NamedPipe.StreamingNamedPipeServer"); throw; } }
/// <summary> /// Wrties a message instance to the underlying named pipe /// </summary> /// <param name="message">The instance of a message that will be written</param> public void WriteMessage(MockMessage message) { try { System.Diagnostics.Debug.WriteLine( "WriteMessage() invoked", "TransMock.Communication.NamedPipes.StreamingNamedPipeClient"); using (MemoryStream msgStream = new MemoryStream(4096)) { System.Diagnostics.Debug.WriteLine( "Constructed MemoryStream where the message will be serialized.", "TransMock.Communication.NamedPipe.StreamingNamedPipeClient"); var formatter = new BinaryFormatter(); formatter.Serialize(msgStream, message); // Writing the TransMock EndOfMessage sequence msgStream.Write( NamedPipeMessageUtils.EndOfMessage, 0, NamedPipeMessageUtils.EndOfMessage.Length); // Rewinding the stream to the beginning msgStream.Seek(0, SeekOrigin.Begin); this.WriteStream(msgStream); } System.Diagnostics.Trace.WriteLine( "WriteMessage() succeeded. Message read by the server", "TransMock.Communication.NamedPipes"); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine( "WriteMessage() threw exception: " + ex.Message, "TransMock.Communication.NamedPipes.StreamingNamedPipeClient"); throw; } }