Exemplo n.º 1
0
        public static async Task <Stream> ConnectAsync(IpcEndpointConfig config, CancellationToken token)
        {
            if (config.Transport == IpcEndpointConfig.TransportType.NamedPipe)
            {
                var namedPipe = new NamedPipeClientStream(
                    ".",
                    config.Address,
                    PipeDirection.InOut,
                    PipeOptions.Asynchronous,
                    TokenImpersonationLevel.Impersonation);

                // Pass non-infinite timeout in order to cause internal connection algorithm
                // to check the CancellationToken periodically. Otherwise, if the named pipe
                // is waited using WaitNamedPipe with an infinite timeout, then the
                // CancellationToken cannot be observed.
                await namedPipe.ConnectAsync(int.MaxValue, token).ConfigureAwait(false);

                return(namedPipe);
            }
            else if (config.Transport == IpcEndpointConfig.TransportType.UnixDomainSocket)
            {
                var socket = new IpcUnixDomainSocket();
                await socket.ConnectAsync(new IpcUnixDomainSocketEndPoint(config.Address), token).ConfigureAwait(false);

                return(new ExposedSocketNetworkStream(socket, ownsSocket: true));
            }
            else
            {
                throw new ArgumentException($"Unsupported IpcEndpointConfig transport type {config.Transport}");
            }
        }
Exemplo n.º 2
0
        public static Stream Connect(IpcEndpointConfig config, TimeSpan timeout)
        {
            if (config.Transport == IpcEndpointConfig.TransportType.NamedPipe)
            {
                var namedPipe = new NamedPipeClientStream(
                    ".",
                    config.Address,
                    PipeDirection.InOut,
                    PipeOptions.None,
                    TokenImpersonationLevel.Impersonation);
                namedPipe.Connect((int)timeout.TotalMilliseconds);
                return(namedPipe);
            }
            else if (config.Transport == IpcEndpointConfig.TransportType.UnixDomainSocket)
            {
                var socket = new IpcUnixDomainSocket();
                socket.Connect(new IpcUnixDomainSocketEndPoint(config.Address), timeout);
                return(new ExposedSocketNetworkStream(socket, ownsSocket: true));
            }
#if DIAGNOSTICS_RUNTIME
            else if (config.Transport == IpcEndpointConfig.TransportType.TcpSocket)
            {
                var tcpClient = new TcpClient();
                var endPoint  = new IpcTcpSocketEndPoint(config.Address);
                tcpClient.Connect(endPoint.EndPoint);
                return(tcpClient.GetStream());
            }
#endif
            else
            {
                throw new ArgumentException($"Unsupported IpcEndpointConfig transport type {config.Transport}");
            }
        }
Exemplo n.º 3
0
        public static async Task <Stream> ConnectAsync(IpcEndpointConfig config, CancellationToken token)
        {
            if (config.Transport == IpcEndpointConfig.TransportType.NamedPipe)
            {
                var namedPipe = new NamedPipeClientStream(
                    ".",
                    config.Address,
                    PipeDirection.InOut,
                    PipeOptions.Asynchronous,
                    TokenImpersonationLevel.Impersonation);
                await namedPipe.ConnectAsync(token).ConfigureAwait(false);

                return(namedPipe);
            }
            else if (config.Transport == IpcEndpointConfig.TransportType.UnixDomainSocket)
            {
                var socket = new IpcUnixDomainSocket();
                await socket.ConnectAsync(new IpcUnixDomainSocketEndPoint(config.Address), token).ConfigureAwait(false);

                return(new ExposedSocketNetworkStream(socket, ownsSocket: true));
            }
            else
            {
                throw new ArgumentException($"Unsupported IpcEndpointConfig transport type {config.Transport}");
            }
        }
Exemplo n.º 4
0
        internal override IpcSocket CreateNewSocketServer()
        {
            var socket = new IpcUnixDomainSocket();

            socket.Bind(_endPoint);
            socket.Listen(_backlog);
            socket.LingerState.Enabled = false;
            OnCreateNewServer(null);
            return(socket);
        }
        public override Stream Connect(TimeSpan timeout)
        {
            string address = GetDefaultAddress();

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var namedPipe = new NamedPipeClientStream(
                    ".",
                    address,
                    PipeDirection.InOut,
                    PipeOptions.None,
                    TokenImpersonationLevel.Impersonation);
                namedPipe.Connect((int)timeout.TotalMilliseconds);
                return(namedPipe);
            }
            else
            {
                var socket = new IpcUnixDomainSocket();
                socket.Connect(new IpcUnixDomainSocketEndPoint(Path.Combine(IpcRootPath, address)), timeout);
                return(new ExposedSocketNetworkStream(socket, ownsSocket: true));
            }
        }
        public override async Task <Stream> ConnectAsync(CancellationToken token)
        {
            string address = GetDefaultAddress();

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                var namedPipe = new NamedPipeClientStream(
                    ".",
                    address,
                    PipeDirection.InOut,
                    PipeOptions.None,
                    TokenImpersonationLevel.Impersonation);
                await namedPipe.ConnectAsync(token).ConfigureAwait(false);

                return(namedPipe);
            }
            else
            {
                var socket = new IpcUnixDomainSocket();
                await socket.ConnectAsync(new IpcUnixDomainSocketEndPoint(Path.Combine(IpcRootPath, address)), token).ConfigureAwait(false);

                return(new ExposedSocketNetworkStream(socket, ownsSocket: true));
            }
        }
        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);
        }