in src/ServiceProfiler.EventPipe.Otel/Microsoft.ApplicationInsights.Profiler.Shared/Services/IPC/DuplexNamedPipeService.cs [94:137]
public async Task ConnectAsync(string pipeName, CancellationToken cancellationToken)
{
_logger.LogTrace("{methodName} on namedpipe: {pipeName}", nameof(ConnectAsync), pipeName);
if (string.IsNullOrEmpty(pipeName))
{
throw new ArgumentException($"'{nameof(pipeName)}' cannot be null or empty.", nameof(pipeName));
}
try
{
using CancellationTokenSource timeoutCancellationTokenSource = new CancellationTokenSource(_options.ConnectionTimeout);
using CancellationTokenSource linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellationTokenSource.Token);
// Becomes token with timeout
cancellationToken = linkedCancellationTokenSource.Token;
await _threadSafeLock.WaitAsync(cancellationToken).ConfigureAwait(false);
switch (_currentMode)
{
case NamedPipeRole.Client:
throw new InvalidOperationException("A connection is already established.");
case NamedPipeRole.Server:
throw new InvalidOperationException("Can't connect to another server from a server.");
case NamedPipeRole.NotSpecified:
default:
break;
}
PipeName = pipeName;
_currentMode = NamedPipeRole.Client;
_logger.LogDebug("Named pipe stream initialization done. Role: {role}, PipeName: {pipeName}", _currentMode, PipeName);
NamedPipeClientStream clientStream = new NamedPipeClientStream(serverName: ".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
_pipeStream = clientStream;
_logger.LogTrace("NamedPipe {role} trying to connect ...", _currentMode);
await clientStream.ConnectAsync(cancellationToken).ConfigureAwait(false);
_logger.LogTrace("NamedPipe {role} connected.", _currentMode);
}
finally
{
_threadSafeLock.Release();
}
}