예제 #1
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}");
            }
        }
예제 #2
0
 public IpcTcpSocketServerTransport(string address, int backlog, IIpcServerTransportCallbackInternal transportCallback = null)
     : base(transportCallback)
 {
     _endPoint = new IpcTcpSocketEndPoint(address);
     _backlog  = backlog != MaxAllowedConnections ? backlog : 100;
     _socket   = CreateNewSocketServer();
 }
 public TcpClientRouterFactory(string tcpClient, int runtimeTimeoutMs, ILogger logger)
 {
     _logger           = logger;
     _tcpClientAddress = IpcTcpSocketEndPoint.NormalizeTcpIpEndPoint(string.IsNullOrEmpty(tcpClient) ? "127.0.0.1:" + string.Format("{0}", 56000 + (Process.GetCurrentProcess().Id % 1000)) : tcpClient);
     _auto_shutdown    = runtimeTimeoutMs != Timeout.Infinite;
     if (runtimeTimeoutMs != Timeout.Infinite)
     {
         TcpClientTimeoutMs = runtimeTimeoutMs;
     }
 }
예제 #4
0
        public static bool isLoopbackOnly(string address)
        {
            bool isLooback = false;

            try
            {
                var value = new IpcTcpSocketEndPoint(address);
                isLooback = IPAddress.IsLoopback(value.EndPoint.Address);
            }
            catch { }

            return(isLooback);
        }
        protected TcpServerRouterFactory(string tcpServer, int runtimeTimeoutMs, ILogger logger)
            : base(logger)
        {
            _tcpServerAddress = IpcTcpSocketEndPoint.NormalizeTcpIpEndPoint(string.IsNullOrEmpty(tcpServer) ? "127.0.0.1:0" : tcpServer);

            _auto_shutdown = runtimeTimeoutMs != Timeout.Infinite;
            if (runtimeTimeoutMs != Timeout.Infinite)
            {
                RuntimeTimeoutMs = runtimeTimeoutMs;
            }

            _tcpServer             = new ReversedDiagnosticsServer(_tcpServerAddress, enableTcpIpProtocol: true);
            _tcpServerEndpointInfo = new IpcEndpointInfo();
        }
예제 #6
0
 public static IpcServerTransport Create(string address, int maxConnections, bool enableTcpIpProtocol, IIpcServerTransportCallbackInternal transportCallback = null)
 {
     if (!enableTcpIpProtocol || !IpcTcpSocketEndPoint.IsTcpIpEndPoint(address))
     {
         if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
         {
             return(new IpcWindowsNamedPipeServerTransport(address, maxConnections, transportCallback));
         }
         else
         {
             return(new IpcUnixDomainSocketServerTransport(address, maxConnections, transportCallback));
         }
     }
     else
     {
         return(new IpcTcpSocketServerTransport(address, maxConnections, transportCallback));
     }
 }
        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);
        }