Пример #1
0
        public bool TryConnect(ConnectionParameters connectionParameters, string developmentAuthToken)
        {
            var playerIdentityToken = GetPlayerIdentityToken(developmentAuthToken);
            var loginTokenDetails   = GetLoginTokenDetails(playerIdentityToken);
            var loginToken          = GetLoginToken(loginTokenDetails);

            var locatorParameters = new Improbable.Worker.Alpha.LocatorParameters
            {
                PlayerIdentity = new PlayerIdentityCredentials
                {
                    LoginToken          = loginToken,
                    PlayerIdentityToken = playerIdentityToken,
                }
            };

            using (var locator = new Improbable.Worker.Alpha.Locator(LocatorHost, LocatorPort, locatorParameters))
                using (var connectionFuture = locator.ConnectAsync(connectionParameters))
                {
                    Connection = connectionFuture.Get();

                    if (Connection.GetConnectionStatusCode() != ConnectionStatusCode.Success)
                    {
                        Log.Print(LogLevel.Error, $"Failed to connect to deployment {deploymentName}. Reason: {Connection.GetConnectionStatusDetailString()}", serviceConnection);
                        return(false);
                    }

                    return(true);
                }
        }
Пример #2
0
        protected override void Run()
        {
            Console.WriteLine("Creating a PlayerIdentityToken");
            var playerIdentityTokenResponse = _playerAuthServiceClient.CreatePlayerIdentityToken(
                new CreatePlayerIdentityTokenRequest
            {
                Provider         = "provider",
                PlayerIdentifier = "player_identifier",
                ProjectName      = ProjectName
            });

            Console.WriteLine("Choosing a deployment");
            var suitableDeployment = _deploymentServiceClient.ListDeployments(new ListDeploymentsRequest
            {
                ProjectName    = ProjectName,
                DeploymentName = DeploymentName
            }).First(d => d.Tag.Contains(ScenarioDeploymentTag));

            Console.WriteLine("Creating a LoginToken for the selected deployment");
            var createLoginTokenResponse = _playerAuthServiceClient.CreateLoginToken(
                new CreateLoginTokenRequest
            {
                PlayerIdentityToken = playerIdentityTokenResponse.PlayerIdentityToken,
                DeploymentId        = suitableDeployment.Id,
                LifetimeDuration    = Duration.FromTimeSpan(new TimeSpan(0, 0, 30, 0)),
                WorkerType          = ScenarioWorkerType
            });

            Console.WriteLine("Connecting to the deployment using the LoginToken and PlayerIdentityToken");
            var locatorParameters = new LocatorParameters
            {
                PlayerIdentity = new PlayerIdentityCredentials
                {
                    PlayerIdentityToken = playerIdentityTokenResponse.PlayerIdentityToken,
                    LoginToken          = createLoginTokenResponse.LoginToken
                }
            };
            var locator = new Locator(LocatorServerAddress, LocatorServerPort, locatorParameters);

            using (var connectionFuture = locator.ConnectAsync(new ConnectionParameters
            {
                WorkerType = ScenarioWorkerType,
                Network = { ConnectionType = NetworkConnectionType.Tcp, UseExternalIp = true }
            }))
            {
                var connFuture = connectionFuture.Get(Convert.ToUInt32(Defaults.ConnectionTimeoutMillis));
                if (!connFuture.HasValue || !connFuture.Value.IsConnected)
                {
                    throw new Exception("No connection or connection not established");
                }
                Console.WriteLine($"Assigned worker ID: {connFuture.Value.GetWorkerId()}");
            }
        }
Пример #3
0
        private static bool TryConnectWorker(Locator locator)
        {
            using (var connectionFuture = locator.ConnectAsync(new ConnectionParameters
            {
                WorkerType = ScenarioWorkerType,
                Network = { ConnectionType = NetworkConnectionType.Tcp, UseExternalIp = true }
            }))
            {
                var connFuture = connectionFuture.Get(Convert.ToUInt32(Defaults.ConnectionTimeoutMillis));
                if (!connFuture.HasValue || !connFuture.Value.IsConnected)
                {
                    return(false);
                }

                Console.WriteLine($"Worker connected and assigned worker ID: {connFuture.Value.GetWorkerId()}");
                return(true);
            }
        }
Пример #4
0
        protected override void Run()
        {
            Console.WriteLine("Creating a PlayerIdentityToken");
            var playerIdentityTokenResponse = _playerAuthServiceClient.CreatePlayerIdentityToken(
                new CreatePlayerIdentityTokenRequest
            {
                Provider         = "provider",
                PlayerIdentifier = "player_identifier",
                ProjectName      = ProjectName
            });

            Console.WriteLine("Verifying PlayerIdentityToken");
            var decodePlayerIdentityTokenResponse = _playerAuthServiceClient.DecodePlayerIdentityToken(
                new DecodePlayerIdentityTokenRequest
            {
                PlayerIdentityToken = playerIdentityTokenResponse.PlayerIdentityToken
            });
            var playerIdentityToken = decodePlayerIdentityTokenResponse.PlayerIdentityToken;

            if (playerIdentityToken.Provider != "provider")
            {
                throw new Exception("Provider not recognised.");
            }
            if (playerIdentityToken.ProjectName != ProjectName)
            {
                throw new Exception("Project not recognised.");
            }
            if (DateTime.Now.CompareTo(playerIdentityToken.ExpiryTime.ToDateTime()) > 0)
            {
                throw new Exception("PlayerIdentityToken expired.");
            }

            Console.WriteLine("Choosing a deployment");
            var listDeploymentsRequest = new ListDeploymentsRequest
            {
                ProjectName = ProjectName,
                Filters     = { new Filter
                                {
                                    TagsPropertyFilter = new TagsPropertyFilter
                                    {
                                        Tag      = "player_auth_tag",
                                        Operator = TagsPropertyFilter.Types.Operator.Equal,
                                    },
                                } }
            };
            var suitableDeployment = _deploymentServiceClient.ListDeployments(listDeploymentsRequest).First();

            Console.WriteLine("Creating a LoginToken for the selected deployment");
            var createLoginTokenResponse = _playerAuthServiceClient.CreateLoginToken(
                new CreateLoginTokenRequest
            {
                PlayerIdentityToken = playerIdentityTokenResponse.PlayerIdentityToken,
                DeploymentId        = suitableDeployment.Id.ToString(),
                LifetimeDuration    = Duration.FromTimeSpan(new TimeSpan(0, 0, 30, 0)),
                WorkerType          = ScenarioWorkerType
            });

            Console.WriteLine("Connecting to the deployment using the LoginToken and PlayerIdentityToken");
            var locatorParameters = new LocatorParameters
            {
                PlayerIdentity = new PlayerIdentityCredentials
                {
                    PlayerIdentityToken = playerIdentityTokenResponse.PlayerIdentityToken,
                    LoginToken          = createLoginTokenResponse.LoginToken
                }
            };
            var locator = new Locator(LocatorServerAddress, LocatorServerPort, locatorParameters);

            using (var connectionFuture = locator.ConnectAsync(new ConnectionParameters
            {
                WorkerType = ScenarioWorkerType,
                Network = { ConnectionType = NetworkConnectionType.Tcp, UseExternalIp = true }
            }))
            {
                var connFuture = connectionFuture.Get(Convert.ToUInt32(Defaults.ConnectionTimeoutMillis));
                if (!connFuture.HasValue || !connFuture.Value.IsConnected)
                {
                    throw new Exception("No connection or connection not established");
                }
                Console.WriteLine($"Assigned worker ID: {connFuture.Value.GetWorkerId()}");
            }
        }