Files
speckle.xunit.runner.wpf/xunit.runner.data/ClientWriter.cs
T
Dustin Campbell d4122f3a0c Correct namespaces to be pascal-cased
Conflicts:
	xunit.runner.wpf/ViewModel/MainViewModel.cs
2015-12-06 10:56:21 -08:00

79 lines
1.7 KiB
C#

using System;
using System.IO;
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));
}
public void Write(string str)
{
WriteCore(() => _writer.Write(str));
}
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
}
}