9d8850c98f
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.
98 lines
2.0 KiB
C#
98 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.IO.Pipes;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace xunit.runner.worker
|
|
{
|
|
internal abstract class Connection : IDisposable
|
|
{
|
|
private bool _closed;
|
|
|
|
internal abstract Stream Stream { get; }
|
|
|
|
internal abstract void WaitForClientConnect();
|
|
internal abstract void WaitForClientDone();
|
|
|
|
protected virtual void DisposeCore()
|
|
{
|
|
|
|
}
|
|
|
|
private void Dispose()
|
|
{
|
|
if (_closed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_closed = true;
|
|
DisposeCore();
|
|
}
|
|
|
|
#region IDisposable
|
|
|
|
void IDisposable.Dispose()
|
|
{
|
|
Dispose();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
internal sealed class NamedPipeConnection : Connection
|
|
{
|
|
private readonly NamedPipeServerStream _stream;
|
|
|
|
internal override Stream Stream => _stream;
|
|
|
|
internal NamedPipeConnection(string pipeName)
|
|
{
|
|
_stream = new NamedPipeServerStream(pipeName);
|
|
}
|
|
|
|
protected override void DisposeCore()
|
|
{
|
|
_stream.Close();
|
|
}
|
|
|
|
internal override void WaitForClientConnect()
|
|
{
|
|
_stream.WaitForConnection();
|
|
}
|
|
|
|
internal override void WaitForClientDone()
|
|
{
|
|
try
|
|
{
|
|
_stream.ReadByte();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// If there is an error reading from the client then clearly they are done
|
|
Console.WriteLine($"Error reading client done byte {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
internal sealed class TestConnection : Connection
|
|
{
|
|
private readonly MemoryStream _stream = new MemoryStream();
|
|
|
|
internal override Stream Stream => _stream;
|
|
|
|
internal override void WaitForClientConnect()
|
|
{
|
|
|
|
}
|
|
|
|
internal override void WaitForClientDone()
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|