提交 a6f86326 编写于 作者: J Jared Parsons 提交者: GitHub

Merge pull request #16019 from jaredpar/fix-code

Combine duplicate methods
......@@ -175,30 +175,9 @@ internal NamedPipeClientConnection(ICompilerServerHost compilerServerHost, strin
///
/// This will return true if the pipe was disconnected.
/// </summary>
protected override async Task CreateMonitorDisconnectTask(CancellationToken cancellationToken)
protected override Task CreateMonitorDisconnectTask(CancellationToken cancellationToken)
{
var buffer = Array.Empty<byte>();
while (!cancellationToken.IsCancellationRequested && _pipeStream.IsConnected)
{
// Wait a second before trying again
await Task.Delay(1000, cancellationToken).ConfigureAwait(false);
try
{
CompilerServerLogger.Log($"Pipe {LoggingIdentifier}: Before poking pipe.");
await _pipeStream.ReadAsync(buffer, 0, 0, cancellationToken).ConfigureAwait(false);
CompilerServerLogger.Log($"Pipe {LoggingIdentifier}: After poking pipe.");
}
catch (Exception e)
{
// It is okay for this call to fail. Errors will be reflected in the
// IsConnected property which will be read on the next iteration of the
// loop
var msg = string.Format($"Pipe {LoggingIdentifier}: Error poking pipe.");
CompilerServerLogger.LogException(e, msg);
}
}
return BuildServerConnection.CreateMonitorDisconnectTask(_pipeStream, LoggingIdentifier, cancellationToken);
}
protected override void ValidateBuildRequest(BuildRequest request)
......
......@@ -228,7 +228,7 @@ internal static string GetRuntimeDirectoryOpt()
Log("Begin reading response");
var responseTask = BuildResponse.ReadAsync(pipeStream, serverCts.Token);
var monitorTask = CreateMonitorDisconnectTask(pipeStream, serverCts.Token);
var monitorTask = CreateMonitorDisconnectTask(pipeStream, "client", serverCts.Token);
await Task.WhenAny(responseTask, monitorTask).ConfigureAwait(false);
Log("End reading response");
......@@ -263,17 +263,13 @@ internal static string GetRuntimeDirectoryOpt()
/// The IsConnected property on named pipes does not detect when the client has disconnected
/// if we don't attempt any new I/O after the client disconnects. We start an async I/O here
/// which serves to check the pipe for disconnection.
///
/// This will return true if the pipe was disconnected.
/// </summary>
private static async Task CreateMonitorDisconnectTask(
NamedPipeClientStream pipeStream,
CancellationToken cancellationToken)
internal static async Task CreateMonitorDisconnectTask(
PipeStream pipeStream,
string identifier = null,
CancellationToken cancellationToken = default(CancellationToken))
{
// Ignore this warning because the desktop projects don't target 4.6 yet
#pragma warning disable CA1825 // Avoid zero-length array allocations.
var buffer = new byte[0];
#pragma warning restore CA1825 // Avoid zero-length array allocations.
var buffer = Array.Empty<byte>();
while (!cancellationToken.IsCancellationRequested && pipeStream.IsConnected)
{
......@@ -282,17 +278,18 @@ internal static string GetRuntimeDirectoryOpt()
try
{
Log("Before poking pipe.");
Log($"Before poking pipe {identifier}.");
await pipeStream.ReadAsync(buffer, 0, 0, cancellationToken).ConfigureAwait(false);
Log("After poking pipe.");
Log($"After poking pipe {identifier}.");
}
catch (OperationCanceledException)
{
}
// Ignore cancellation
catch (OperationCanceledException) { }
catch (Exception e)
{
// It is okay for this call to fail. Errors will be reflected in the
// IsConnected property which will be read on the next iteration of the
LogException(e, "Error poking pipe");
LogException(e, $"Error poking pipe {identifier}.");
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册