public static void Test_isSuccessful_True_On_Success(string endpoint, int port)
        {
            //arrange
            ResolveServiceEndpointResponse model = new ResolveServiceEndpointResponse(new ResolvedEndpoint(endpoint, port));

            //assert
            Assert.True(model.isSuccessful);
        }
        public static void Test_isSuccessful_False_On_Failed_ResponseCodes(ResolveServiceEndpointResponseCode resultCode)
        {
            //arrange
            ResolveServiceEndpointResponse model = new ResolveServiceEndpointResponse(resultCode);

            //assert
            Assert.False(model.isSuccessful);
        }
        public async Task Test_ZoneServer_GetEndpoint_ReturnsFail_On_Empty()
        {
            //arrange
            IServiceProvider     provider   = BuildServiceProvider <ZoneServerController>("Test", 1);
            ZoneServerController controller = provider.GetService <ZoneServerController>();

            //assert
            ResolveServiceEndpointResponse result = GetActionResultObject <ResolveServiceEndpointResponse>(await controller.GetServerEndpoint(1));

            //assert
            Assert.False(result.isSuccessful);
        }
示例#4
0
        //TODO: We need to handle failure cases, maybe with a window popup and bringing back to the titlescreen.
        /// <inheritdoc />
        public async Task OnGameInitialized()
        {
            //When we reach this scene, the pre lobby burst scene
            //we need to actually connect to the zone/lobby.
            //it verry well could be a zone. Maybe we were in a party and are reconnecting to it
            //no matter what though, we need to get information about our
            //character session and then the zone it should be connecting to
            //then we can connect.

            //First we need to know what zone this session should be going to
            CharacterSessionDataResponse sessionData = await CharacterDataService.GetCharacterSessionData(CharacterDataRepo.CharacterId, AuthTokenRepo.RetrieveWithType())
                                                       .ConfigureAwait(true);

            //TODO: Handle this better
            if (!sessionData.isSuccessful)
            {
                Logger.Error($"Failed to query session data for Character: {CharacterDataRepo.CharacterId}. Cannot connect to instance server.");
                return;
            }

            ResolveServiceEndpointResponse zoneEndpointResponse = await ZoneService.GetServerEndpoint(sessionData.ZoneId);

            if (!zoneEndpointResponse.isSuccessful)
            {
                Logger.Error($"Failed to query endpoint for Zone: {sessionData.ZoneId} which Character: {CharacterDataRepo.CharacterId} is in. Cannot connect to instance server.");
                return;
            }

            //TODO: Don't hardcode gameserver connection details
            //As soon as we start we should attempt to connect to the login server.
            bool result = await Client.ConnectAsync(IPAddress.Parse(zoneEndpointResponse.Endpoint.EndpointAddress), zoneEndpointResponse.Endpoint.EndpointPort)
                          .ConfigureAwait(true);

            if (!result)
            {
                throw new InvalidOperationException($"Failed to connect to Server: {zoneEndpointResponse.Endpoint.EndpointAddress} Port: {zoneEndpointResponse.Endpoint.EndpointPort}");
            }

            if (Logger.IsDebugEnabled)
            {
                Logger.Debug($"Connected client. isConnected: {Client.isConnected}");
            }

            //Basically we just take the network client and tell the client manager to start dealing with it
            //since it's connecting the manager should start pumping the messages out of it.
            await NetworkClientManager.StartHandlingNetworkClient(Client)
            .ConfigureAwait(true);

            //We should broadcast that the connection has been established to any interested subscribers
            OnNetworkConnectionEstablished?.Invoke(this, EventArgs.Empty);
        }
        //TODO: Put this in a base class or something
        private async Task <string> QueryForRemoteServiceEndpoint(IServiceDiscoveryService serviceDiscovery, string serviceType)
        {
            ResolveServiceEndpointResponse endpointResponse = await serviceDiscovery.DiscoverService(new ResolveServiceEndpointRequest(ClientRegionLocale.US, serviceType));

            if (!endpointResponse.isSuccessful)
            {
                throw new InvalidOperationException($"Failed to query for Service: {serviceType} Result: {endpointResponse.ResultCode}");
            }

            Debug.Log($"Recieved service discovery response: {endpointResponse.Endpoint.EndpointAddress}:{endpointResponse.Endpoint.EndpointPort} for Type: {serviceType}");

            //TODO: Do we need extra slash?
            return($"{endpointResponse.Endpoint.EndpointAddress}:{endpointResponse.Endpoint.EndpointPort}/");
        }
        public static void Test_Can_JSON_Serialize_To_NonNull_Non_Whitespace(string endpoint, int port)
        {
            //arrange
            ResolveServiceEndpointResponse authModel = new ResolveServiceEndpointResponse(new ResolvedEndpoint(endpoint, port));

            //act
            string serializedModel = JsonConvert.SerializeObject(authModel);

            //assert
            Assert.NotNull(serializedModel);
            Assert.IsNotEmpty(serializedModel);
            Assert.True(serializedModel.Contains(endpoint));
            Assert.True(serializedModel.Contains(port.ToString()));
        }
        public static void Test_Can_JSON_Serialize_Then_Deserialize_With_Preserved_Values(ResolveServiceEndpointResponseCode resultCode)
        {
            //arrange
            ResolveServiceEndpointResponse authModel = new ResolveServiceEndpointResponse(resultCode);

            //act
            ResolveServiceEndpointResponse deserializedModel =
                JsonConvert.DeserializeObject <ResolveServiceEndpointResponse>(JsonConvert.SerializeObject(authModel));

            //assert
            Assert.NotNull(deserializedModel);
            Assert.Null(deserializedModel.Endpoint);
            Assert.True(Enum.IsDefined(typeof(ResolveServiceEndpointResponseCode), deserializedModel.ResultCode), $"Enum value: {deserializedModel.ResultCode} was not valid.");
        }
        public static void Test_Can_JSON_Serialize_To_NonNull_Non_Whitespace(ResolveServiceEndpointResponseCode resultCode)
        {
            //arrange
            ResolveServiceEndpointResponse authModel = new ResolveServiceEndpointResponse(resultCode);

            //act
            string serializedModel = JsonConvert.SerializeObject(authModel);

            //assert
            Assert.NotNull(serializedModel);
            Assert.True(!serializedModel.Contains(nameof(authModel.isSuccessful)), $"JSON modle contains what should be unlisted field {nameof(authModel.isSuccessful)}. JSON: {serializedModel}");
            Assert.IsNotEmpty(serializedModel);
            Assert.True(serializedModel.Contains(((int)resultCode).ToString()));
        }
        public static void Test_Can_JSON_Serialize_Then_Deserialize_With_Preserved_Values(string endpoint, int port)
        {
            //arrange
            ResolveServiceEndpointResponse authModel = new ResolveServiceEndpointResponse(new ResolvedEndpoint(endpoint, port));

            //act
            ResolveServiceEndpointResponse deserializedModel =
                JsonConvert.DeserializeObject <ResolveServiceEndpointResponse>(JsonConvert.SerializeObject(authModel));

            //assert
            Assert.NotNull(deserializedModel);
            Assert.NotNull(deserializedModel.Endpoint);
            Assert.NotNull(deserializedModel.Endpoint.EndpointAddress);
            Assert.AreEqual(endpoint, deserializedModel.Endpoint.EndpointAddress);
            Assert.AreEqual(port, deserializedModel.Endpoint.EndpointPort);
        }
        public async Task Test_ZoneServer_GetEndpoint_Succeeds_On_Known_Id(string endpoint, int port)
        {
            //arrange
            IServiceProvider      provider   = BuildServiceProvider <ZoneServerController>("Test", 1);
            ZoneServerController  controller = provider.GetService <ZoneServerController>();
            IZoneServerRepository repo       = provider.GetService <IZoneServerRepository>();
            await repo.TryCreateAsync(new ZoneInstanceEntryModel(endpoint, (short)port, 1));

            //assert
            ResolveServiceEndpointResponse result = GetActionResultObject <ResolveServiceEndpointResponse>(await controller.GetServerEndpoint(1));

            //assert
            Assert.True(result.isSuccessful);
            Assert.AreEqual(ResolveServiceEndpointResponseCode.Success, result.ResultCode);
            Assert.AreEqual(endpoint, result.Endpoint.EndpointAddress);
            Assert.AreEqual(port, result.Endpoint.EndpointPort);
        }
        public async Task Test_ZoneServer_GetEndpoint_ReturnsFail_On_NoExistingZoneId(int zoneId)
        {
            //arrange
            IServiceProvider      provider   = BuildServiceProvider <ZoneServerController>("Test", 1);
            ZoneServerController  controller = provider.GetService <ZoneServerController>();
            IZoneServerRepository repo       = provider.GetService <IZoneServerRepository>();
            await repo.TryCreateAsync(new ZoneInstanceEntryModel("127.0.0.1", 1080, 1));

            await repo.TryCreateAsync(new ZoneInstanceEntryModel("127.0.0.1", 1080, 1));

            await repo.TryCreateAsync(new ZoneInstanceEntryModel("127.0.0.1", 1080, 1));

            //assert
            ResolveServiceEndpointResponse result = GetActionResultObject <ResolveServiceEndpointResponse>(await controller.GetServerEndpoint(25));

            //assert
            Assert.False(result.isSuccessful);
        }
        /// <inheritdoc />
        public async Task OnGameInitialized()
        {
            //We need to connect the hub to the social backend
            ResolveServiceEndpointResponse endpointResponse = await ServiceDiscoveryService.DiscoverService(new ResolveServiceEndpointRequest(ClientRegionLocale.US, "SocialService"))
                                                              .ConfigureAwait(false);

            if (!endpointResponse.isSuccessful)
            {
                throw new InvalidOperationException($"Failed to query for SocialService. Reason: {endpointResponse.ResultCode}");
            }

            string hubConnectionString = $@"{endpointResponse.Endpoint.EndpointAddress}:{endpointResponse.Endpoint.EndpointPort}/realtime/textchat";

            if (Logger.IsInfoEnabled)
            {
                Logger.Info($"Social HubConnection String: {hubConnectionString}");
            }

            //TODO: Handle failed service disc query
            HubConnection connection = new HubConnectionBuilder()
                                       .WithUrl(hubConnectionString, options =>
            {
                options.Headers.Add(SocialNetworkConstants.CharacterIdRequestHeaderName, PlayerDetails.LocalPlayerGuid.EntityId.ToString());
                options.AccessTokenProvider = () => Task.FromResult(AuthTokenProvider.Retrieve());
            })
                                       .AddJsonProtocol()
                                       .Build();

            foreach (var i in InitializableSocialServices)
            {
                i.Connection = connection;
            }

            //Just start the service when the game initializes
            //This will make it so that the signalR clients will start to recieve messages.
            await connection.StartAsync()
            .ConfigureAwait(false);
        }