public async Task Start()

in src/AWS.Deploy.ServerMode.Client/ServerModeSession.cs [115:177]


        public async Task Start(CancellationToken cancellationToken)
        {
            var deployToolRoot = "dotnet aws";
            if (!string.IsNullOrEmpty(_deployToolPath))
            {
                if (!PathUtilities.IsDeployToolPathValid(_deployToolPath))
                    throw new InvalidAssemblyReferenceException("The specified assembly location is invalid.");

                deployToolRoot = _deployToolPath;
            }

            var currentProcessId = Process.GetCurrentProcess().Id;

            for (var port = _startPort; port <= _endPort; port++)
            {
                // This ensures that deploy tool CLI doesn't try on the in-use port
                // because server availability task will return success response for
                // an in-use port
                if (IsPortInUse(port))
                {
                    continue;
                }

                _aes = Aes.Create();
                _aes.GenerateKey();

                var keyInfo = new EncryptionKeyInfo(
                    EncryptionKeyInfo.VERSION_1_0,
                    Convert.ToBase64String(_aes.Key));

                var keyInfoStdin = Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(keyInfo)));

                var command = $"\"{deployToolRoot}\" server-mode --port {port} --parent-pid {currentProcessId}";
                var startServerTask = _commandLineWrapper.Run(command, keyInfoStdin);

                _baseUrl = $"http://localhost:{port}";
                var isServerAvailableTask = IsServerAvailable(cancellationToken);

                if (isServerAvailableTask == await Task.WhenAny(startServerTask, isServerAvailableTask).ConfigureAwait(false))
                {
                    // The server timed out, this isn't a transient error, therefore, we throw
                    if (!isServerAvailableTask.Result)
                    {
                        throw new InternalServerModeException($"\"{command}\" failed for unknown reason.");
                    }

                    // Server has started, it is safe to return
                    return;
                }

                // For -100 errors, we want to check all the ports in the configured port range
                // If the error code other than -100, this is an unexpected exit code.
                if (startServerTask.Result.ExitCode != TCP_PORT_ERROR)
                {
                    throw new InternalServerModeException(
                        string.IsNullOrEmpty(startServerTask.Result.StandardError) ?
                        $"\"{command}\" failed for unknown reason." :
                        startServerTask.Result.StandardError);
                }
            }

            throw new PortUnavailableException($"Free port unavailable in {_startPort}-{_endPort} range.");
        }