public async Task <Connection> CreateAsync(ConnectionParameters parameters, CancellationToken?token = null)
        {
            parameters.Network.UseExternalIp = true;

            using (var locator = new Locator(LocatorHost, LocatorParameters))
            {
                var deploymentList = await GetDeploymentList(locator);

                var deploymentName = SelectDeploymentName(deploymentList);
                if (string.IsNullOrEmpty(deploymentName))
                {
                    throw new ConnectionFailedException("No deployment name chosen",
                                                        ConnectionErrorReason.DeploymentNotFound);
                }

                using (var connectionFuture = locator.ConnectAsync(deploymentName, parameters, _ => true))
                {
                    return(await Utils.TryToConnectAsync(connectionFuture, token).ConfigureAwait(false));
                }
            }
        }
        private static async Task <DeploymentList> GetDeploymentList(Locator locator)
        {
            using (var deploymentsFuture = locator.GetDeploymentListAsync())
            {
                var deploymentList = await Task.Run(() => deploymentsFuture.Get()).ConfigureAwait(false);

                // Guard against null refs. This shouldn't be triggered.
                if (!deploymentList.HasValue)
                {
                    throw new ConnectionFailedException("Deployment list future returned null.",
                                                        ConnectionErrorReason.DeploymentNotFound);
                }

                if (deploymentList.Value.Error != null)
                {
                    throw new ConnectionFailedException(deploymentList.Value.Error,
                                                        ConnectionErrorReason.DeploymentNotFound);
                }

                return(deploymentList.Value);
            }
        }
Example #3
0
        public static async Task <WorkerConnection> ConnectAsync(ILocatorOptions options, ConnectionParameters connectionParameters, CancellationToken cancellation = default)
        {
            var pit         = GetDevelopmentPlayerIdentityToken(options.SpatialOsHost, options.SpatialOsPort, options.UseInsecureConnection, options.DevToken, options.PlayerId, options.DisplayName);
            var loginTokens = GetDevelopmentLoginTokens(options.SpatialOsHost, options.SpatialOsPort, options.UseInsecureConnection, connectionParameters.WorkerType, pit);
            var loginToken  = loginTokens.First().LoginToken;

            var locatorParameters = new LocatorParameters
            {
                PlayerIdentity = new PlayerIdentityCredentials
                {
                    LoginToken          = loginToken,
                    PlayerIdentityToken = pit
                },
                UseInsecureConnection = options.UseInsecureConnection,
                Logging         = connectionParameters.ProtocolLogging,
                EnableLogging   = connectionParameters.EnableProtocolLoggingAtStartup,
                CredentialsType = LocatorCredentialsType.PlayerIdentity,
                ProjectName     = options.ProjectName
            };

            using var locator = new Locator(options.SpatialOsHost, options.SpatialOsPort, locatorParameters);
            using var future  = locator.ConnectAsync(connectionParameters);
            var connection = await future.ToTask(cancellation).ConfigureAwait(false);

            if (connection == null)
            {
                throw new Exception("Connection is null");
            }

            if (connection.GetConnectionStatusCode() != ConnectionStatusCode.Success)
            {
                throw new Exception($"{connection.GetConnectionStatusCode()}: {connection.GetConnectionStatusCodeDetailString()}");
            }

            return(new WorkerConnection(connection));
        }