public async Task <Stream> ConnectTcpStreamAsync(CancellationToken token) { Stream tcpClientStream = null; _logger?.LogDebug($"Connecting new tcp endpoint \"{_tcpClientAddress}\"."); bool retry = false; IpcTcpSocketEndPoint clientTcpEndPoint = new IpcTcpSocketEndPoint(_tcpClientAddress); Socket clientSocket = null; using var connectTimeoutTokenSource = new CancellationTokenSource(); using var connectTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, connectTimeoutTokenSource.Token); connectTimeoutTokenSource.CancelAfter(TcpClientTimeoutMs); do { clientSocket = new Socket(SocketType.Stream, ProtocolType.Tcp); try { await ConnectAsyncInternal(clientSocket, clientTcpEndPoint, token).ConfigureAwait(false); retry = false; } catch (Exception) { clientSocket?.Dispose(); if (connectTimeoutTokenSource.IsCancellationRequested) { _logger?.LogDebug("No tcp stream connected, timing out."); if (_auto_shutdown) { throw new RuntimeTimeoutException(TcpClientTimeoutMs); } throw new TimeoutException(); } // If we are not doing auto shutdown when runtime is unavailable, fail right away, this will // break any accepted IPC connections, making sure client is notified and could reconnect. // If we do have auto shutdown enabled, retry until succeed or time out. if (!_auto_shutdown) { _logger?.LogTrace($"Failed connecting {_tcpClientAddress}."); throw; } _logger?.LogTrace($"Failed connecting {_tcpClientAddress}, wait {TcpClientRetryTimeoutMs} ms before retrying."); // If we get an error (without hitting timeout above), most likely due to unavailable listener. // Delay execution to prevent to rapid retry attempts. await Task.Delay(TcpClientRetryTimeoutMs, token).ConfigureAwait(false); retry = true; } }while (retry); tcpClientStream = new ExposedSocketNetworkStream(clientSocket, ownsSocket: true); _logger?.LogDebug("Successfully connected tcp stream."); return(tcpClientStream); }
public async Task <Stream> ConnectIpcStreamAsync(CancellationToken token) { Stream ipcClientStream = null; _logger?.LogDebug($"Connecting new ipc endpoint \"{_ipcClientPath}\"."); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var namedPipe = new NamedPipeClientStream( ".", _ipcClientPath, PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.Impersonation); try { await namedPipe.ConnectAsync(IpcClientTimeoutMs, token).ConfigureAwait(false); } catch (Exception ex) { namedPipe?.Dispose(); if (ex is TimeoutException) { _logger?.LogDebug("No ipc stream connected, timing out."); } throw; } ipcClientStream = namedPipe; } else { bool retry = false; IpcUnixDomainSocket unixDomainSocket; using var connectTimeoutTokenSource = new CancellationTokenSource(); using var connectTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, connectTimeoutTokenSource.Token); connectTimeoutTokenSource.CancelAfter(IpcClientTimeoutMs); do { unixDomainSocket = new IpcUnixDomainSocket(); try { await unixDomainSocket.ConnectAsync(new IpcUnixDomainSocketEndPoint(_ipcClientPath), token).ConfigureAwait(false); retry = false; } catch (Exception) { unixDomainSocket?.Dispose(); if (connectTimeoutTokenSource.IsCancellationRequested) { _logger?.LogDebug("No ipc stream connected, timing out."); throw new TimeoutException(); } _logger?.LogTrace($"Failed connecting {_ipcClientPath}, wait {IpcClientRetryTimeoutMs} ms before retrying."); // If we get an error (without hitting timeout above), most likely due to unavailable listener. // Delay execution to prevent to rapid retry attempts. await Task.Delay(IpcClientRetryTimeoutMs, token).ConfigureAwait(false); retry = true; } }while (retry); ipcClientStream = new ExposedSocketNetworkStream(unixDomainSocket, ownsSocket: true); } if (ipcClientStream != null) { _logger?.LogDebug("Successfully connected ipc stream."); } return(ipcClientStream); }
protected async Task <Stream> ConnectIpcStreamAsync(CancellationToken token) { Stream ipcClientStream = null; Logger.LogDebug($"Connecting new ipc endpoint \"{_ipcClientPath}\"."); if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { var namedPipe = new NamedPipeClientStream( ".", _ipcClientPath, PipeDirection.InOut, PipeOptions.Asynchronous, TokenImpersonationLevel.Impersonation); try { await namedPipe.ConnectAsync(IpcClientTimeoutMs, token).ConfigureAwait(false); } catch (Exception ex) { namedPipe?.Dispose(); if (ex is TimeoutException) { Logger.LogDebug("No ipc stream connected, timing out."); } throw; } ipcClientStream = namedPipe; } else { bool retry = false; IpcUnixDomainSocket unixDomainSocket; do { unixDomainSocket = new IpcUnixDomainSocket(); using var connectTimeoutTokenSource = new CancellationTokenSource(); using var connectTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, connectTimeoutTokenSource.Token); try { connectTimeoutTokenSource.CancelAfter(IpcClientTimeoutMs); await unixDomainSocket.ConnectAsync(new IpcUnixDomainSocketEndPoint(_ipcClientPath), token).ConfigureAwait(false); retry = false; } catch (Exception) { unixDomainSocket?.Dispose(); if (connectTimeoutTokenSource.IsCancellationRequested) { Logger.LogDebug("No ipc stream connected, timing out."); throw new TimeoutException(); } Logger.LogTrace($"Failed connecting {_ipcClientPath}, wait {IpcClientRetryTimeoutMs} ms before retrying."); // If we get an error (without hitting timeout above), most likely due to unavailable listener. // Delay execution to prevent to rapid retry attempts. await Task.Delay(IpcClientRetryTimeoutMs, token).ConfigureAwait(false); if (IpcClientTimeoutMs != Timeout.Infinite) { throw; } retry = true; } }while (retry); ipcClientStream = new ExposedSocketNetworkStream(unixDomainSocket, ownsSocket: true); } try { // ReversedDiagnosticsServer consumes advertise message, needs to be replayed back to ipc client stream. Use router process ID as representation. await IpcAdvertise.SerializeAsync(ipcClientStream, RuntimeInstanceId, (ulong)Process.GetCurrentProcess().Id, token).ConfigureAwait(false); } catch (Exception) { Logger.LogDebug("Failed sending advertise message."); ipcClientStream?.Dispose(); throw; } if (ipcClientStream != null) { Logger.LogDebug("Successfully connected ipc stream."); } return(ipcClientStream); }