internal static Task WriteAsync(this StreamWriter writer, NamedPipeCommand command) { return writer.WriteAsync((char)command); }
internal static Task WriteAsync(this StreamWriter writer, NamedPipeCommand command) { return(writer.WriteAsync((char)command)); }
private async Task <T> SendReceiveCommandAsync <T>(Func <StreamReader, StreamWriter, Task <T> > func, NamedPipeCommand command, CancellationToken cancellationToken, T defaultValue = default(T)) { // Time out after 2 seconds to access named pipe using (var innerCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(2))) { try { // Ensure that the named pipe cancellation token gets cancelled if the main token is using (cancellationToken.Register(innerCancellationTokenSource.Cancel)) using (var pipe = new NamedPipeClientStream(NamedPipePoshGitServer.ServerName, ServerInfo.Name, PipeDirection.InOut, PipeOptions.Asynchronous)) { await pipe.ConnectAsync(innerCancellationTokenSource.Token); using (var writer = new NonClosingStreamWriter(pipe) { AutoFlush = true }) using (var reader = new NonClosingStreamReader(pipe)) { await writer.WriteAsync(command); var response = await reader.ReadCommandAsync(); if (response != NamedPipeCommand.Ready) { return(defaultValue); } return(await func(reader, writer)); } } } catch (OperationCanceledException) { _log.Error("Named pipe communication with server was cancelled"); return(defaultValue); } } }