AWSGameLiftJoinSessionActivity.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
  3. * its licensors.
  4. *
  5. * For complete copyright and license terms please see the LICENSE at the root of this
  6. * distribution (the "License"). All use of this software is governed by the License,
  7. * or, if provided, by the license below or the license accompanying this file. Do not
  8. * remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. *
  11. */
  12. #include <AzCore/Interface/Interface.h>
  13. #include <AzFramework/Session/ISessionHandlingRequests.h>
  14. #include <Activity/AWSGameLiftJoinSessionActivity.h>
  15. #include <AWSGameLiftSessionConstants.h>
  16. namespace AWSGameLift
  17. {
  18. namespace JoinSessionActivity
  19. {
  20. Aws::GameLift::Model::CreatePlayerSessionRequest BuildAWSGameLiftCreatePlayerSessionRequest(
  21. const AWSGameLiftJoinSessionRequest& joinSessionRequest)
  22. {
  23. Aws::GameLift::Model::CreatePlayerSessionRequest request;
  24. // Optional attributes
  25. if (!joinSessionRequest.m_playerData.empty())
  26. {
  27. request.SetPlayerData(joinSessionRequest.m_playerData.c_str());
  28. }
  29. // Required attributes
  30. request.SetPlayerId(joinSessionRequest.m_playerId.c_str());
  31. request.SetGameSessionId(joinSessionRequest.m_sessionId.c_str());
  32. return request;
  33. }
  34. AzFramework::SessionConnectionConfig BuildSessionConnectionConfig(
  35. const Aws::GameLift::Model::CreatePlayerSessionOutcome& createPlayerSessionOutcome)
  36. {
  37. AzFramework::SessionConnectionConfig sessionConnectionConfig;
  38. auto createPlayerSessionResult = createPlayerSessionOutcome.GetResult();
  39. // TODO: AWSNativeSDK needs to be updated to support this attribute, and it is a must have for TLS certificate enabled fleet
  40. //sessionConnectionConfig.m_dnsName = createPlayerSessionResult.GetPlayerSession().GetDnsName().c_str();
  41. sessionConnectionConfig.m_ipAddress = createPlayerSessionResult.GetPlayerSession().GetIpAddress().c_str();
  42. sessionConnectionConfig.m_playerSessionId = createPlayerSessionResult.GetPlayerSession().GetPlayerSessionId().c_str();
  43. sessionConnectionConfig.m_port = createPlayerSessionResult.GetPlayerSession().GetPort();
  44. return sessionConnectionConfig;
  45. }
  46. Aws::GameLift::Model::CreatePlayerSessionOutcome CreatePlayerSession(
  47. const Aws::GameLift::GameLiftClient& gameliftClient,
  48. const AWSGameLiftJoinSessionRequest& joinSessionRequest)
  49. {
  50. AZ_TracePrintf(AWSGameLiftJoinSessionActivityName,
  51. "Requesting CreatePlayerSession for player %s against Amazon GameLift service ...",
  52. joinSessionRequest.m_playerId.c_str());
  53. Aws::GameLift::Model::CreatePlayerSessionRequest request =
  54. BuildAWSGameLiftCreatePlayerSessionRequest(joinSessionRequest);
  55. auto createPlayerSessionOutcome = gameliftClient.CreatePlayerSession(request);
  56. if (!createPlayerSessionOutcome.IsSuccess())
  57. {
  58. AZ_Error(AWSGameLiftJoinSessionActivityName, false, AWSGameLiftErrorMessageTemplate,
  59. createPlayerSessionOutcome.GetError().GetExceptionName().c_str(),
  60. createPlayerSessionOutcome.GetError().GetMessage().c_str());
  61. }
  62. return createPlayerSessionOutcome;
  63. }
  64. bool RequestPlayerJoinSession(const Aws::GameLift::Model::CreatePlayerSessionOutcome& createPlayerSessionOutcome)
  65. {
  66. bool result = false;
  67. if (createPlayerSessionOutcome.IsSuccess())
  68. {
  69. auto clientRequestHandler = AZ::Interface<AzFramework::ISessionHandlingClientRequests>::Get();
  70. if (clientRequestHandler)
  71. {
  72. AZ_TracePrintf(AWSGameLiftJoinSessionActivityName, "Requesting player to connect to game session ...");
  73. AzFramework::SessionConnectionConfig sessionConnectionConfig =
  74. BuildSessionConnectionConfig(createPlayerSessionOutcome);
  75. result = clientRequestHandler->RequestPlayerJoinSession(sessionConnectionConfig);
  76. }
  77. else
  78. {
  79. AZ_Error(AWSGameLiftJoinSessionActivityName, false, AWSGameLiftJoinSessionMissingRequestHandlerErrorMessage);
  80. }
  81. }
  82. return result;
  83. }
  84. bool ValidateJoinSessionRequest(const AzFramework::JoinSessionRequest& joinSessionRequest)
  85. {
  86. auto gameliftJoinSessionRequest = azrtti_cast<const AWSGameLiftJoinSessionRequest*>(&joinSessionRequest);
  87. if (gameliftJoinSessionRequest &&
  88. !gameliftJoinSessionRequest->m_playerId.empty() &&
  89. !gameliftJoinSessionRequest->m_sessionId.empty())
  90. {
  91. return true;
  92. }
  93. else
  94. {
  95. AZ_Error(AWSGameLiftJoinSessionActivityName, false, AWSGameLiftJoinSessionRequestInvalidErrorMessage);
  96. return false;
  97. }
  98. }
  99. } // namespace JoinSessionActivity
  100. } // namespace AWSGameLift