Exemplo n.º 1
0
        public async Task <JsonResult> TryEnterGameServer([FromServices] IGameSessionRepository gameSessionRepository)
        {
            if (!ModelState.IsValid)
            {
                return(Json(new ServerEntryResponse(ServerEntryResponseCode.FailedConnectionActivelyRefused)));
            }

            int userId = int.Parse(HaloLiveUserManager.GetUserId(User));             //TODO: Should we try parse? Or change the signature for this?

            //If there is a session and we're already logged in reject the requesting user
            //It is possible for there to be an existing session yet not logged in
            //that would mean an existing session was unclaimined and hasn't been cleaned up yet or someone is connecting in the middle of the session transfer
            bool hasSessionAlready = await gameSessionRepository.HasSession(userId);

            if (hasSessionAlready)
            {
                return(Json(new ServerEntryResponse(ServerEntryResponseCode.FailedAlreadyLoggedIn)));
            }

            //This could result in a data race so we need to check the result after we create. Don't assume this won't fail or be exploited
            SessionCreationResult result = await gameSessionRepository.TryCreateSession(userId, Request.HttpContext.Connection.RemoteIpAddress.ToString());

            if (!result.isSessionCreated)
            {
                return(Json(new ServerEntryResponse(ServerEntryResponseCode.GeneralServerError)));
            }

            //TODO: Handle gameserver/zoneserver redirection based on session information.
            return(Json(new ServerEntryResponse(result.SessionGuid, new ResolvedEndpoint("127.0.0.1", 8051))));            //TODO: Obviously we want to look in the DB and provide a real token.
        }