Files
speckle.xunit.runner.wpf/xunit.runner.data/ClientWriter.cs
T
Jared Parsons 9d8850c98f Remove exceptions is standard workflow
Added markers to the IPC stream so the client knows when to stop
reading.  The previous behavior was to just read until the pipe was
empty and swallow the exception.
2015-08-19 17:00:49 -07:00

78 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xunit.runner.data
{
public sealed class ClientWriter : IDisposable
{
private readonly BinaryWriter _writer;
private bool _closed;
public bool IsConnected => !_closed;
public ClientWriter(Stream stream)
{
_writer = new BinaryWriter(stream, Constants.Encoding, leaveOpen: true);
}
public void Close()
{
if (_closed)
{
return;
}
_closed = true;
_writer.Dispose();
}
public void Write(TestDataKind kind)
{
WriteCore(() => _writer.Write((int)kind));
}
public void Write(TestCaseData testCaseData)
{
WriteCore(() => testCaseData.WriteTo(_writer));
}
public void Write(TestResultData testCaseResultData)
{
WriteCore(() => testCaseResultData.WriteTo(_writer));
}
private void WriteCore(Action action)
{
if (_closed)
{
return;
}
try
{
action();
}
catch (Exception ex)
{
// Happens during rude shut down of the client. Log to the screen and close
// the connection.
Console.WriteLine(ex.Message);
Close();
}
}
#region IDisposable
void IDisposable.Dispose()
{
Close();
}
#endregion
}
}