in CppServerAndClient/Client/Client.cpp [107:199]
int main () {
// Init the AWS SDK
Aws::SDKOptions options;
Aws::InitAPI(options);
Aws::Client::ClientConfiguration clientConfiguration;
clientConfiguration.region = REGION; // region must be set for Amazon Cognito operations
auto cognitoIdentityClient = Aws::MakeShared<CognitoIdentityClient>("CognitoIdentityClient", clientConfiguration);
// Request the Cognito Identity, NOTE: We will always get a new identity as this is not cached! You should cache the identity in your local player data
GetIdRequest getIdRequest;
getIdRequest.WithIdentityPoolId(identityPoolId);
GetIdOutcome idResult = cognitoIdentityClient->GetId(getIdRequest);
GetCredentialsForIdentityRequest getCredentialsRequest;
getCredentialsRequest.WithIdentityId(idResult.GetResult().GetIdentityId());
GetCredentialsForIdentityOutcome getCredentialsResult = cognitoIdentityClient->GetCredentialsForIdentity(getCredentialsRequest);
auto credentials = getCredentialsResult.GetResult().GetCredentials();
std::cout << "Got Cognito Identity: " << getCredentialsResult.GetResult().GetIdentityId() << "\n";
std::cout << "Got Credentials: " << credentials.GetAccessKeyId() << ":" << credentials.GetSecretKey() << "\n";
// REQUEST MATCHMAKING
std::cout << "Signing a matchmaking request...\n";
//Generate the Region latencies string for the API call by pinging the DynamoDB endpoints with HTTP, this information will be used by FlexMatch to match player and by GameLift Queue to place sessions
String latencies = "";
latencies += GetLatencyString(regionString) + "_" + GetLatencyString(secondaryRegionString);
std::cout << "Latencies: " << latencies << std::endl;
// Create a credentials provider with the Cognito credentials to sign the requests
auto credentialsProvider = Aws::MakeShared<Aws::Auth::SimpleAWSCredentialsProvider>("api-gateway-client", credentials.GetAccessKeyId(), credentials.GetSecretKey(), credentials.GetSessionToken());
auto requestSigner = MakeShared<AWSAuthV4Signer>("", credentialsProvider, "execute-api", regionString);
// Create and sign the HTTP request
auto getGameSessionRequest = CreateHttpRequest(URI(backendApiUrl +"requestmatchmaking?latencies=" + latencies),
HttpMethod::HTTP_GET, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
requestSigner->SignRequest(*getGameSessionRequest);
std::cout << "requesting matchmaking...\n\n";
// make the HTTP request
auto httpClient = CreateHttpClient(clientConfiguration);
auto requestMatchmakingResponse = httpClient->MakeRequest(getGameSessionRequest);
std::stringstream ss;
ss << requestMatchmakingResponse->GetResponseBody().rdbuf();
std::cout << ss.str() + "\n\n";
JsonValue matchMakingRequestJson = JsonValue(ss.str());
String ticketId = matchMakingRequestJson.View().GetString("TicketId");
std::cout << "Got Ticket ID: " << ticketId << "\n";
// REQUEST MATCH STATUS UNTIL IT'S DONE
// We will try 10 times with a 2s interval. The Matchmaking will time out after 20s so no point trying after that
int tries = 0;
while(tries < 10)
{
std::cout << "Signing a match status request...\n";
// Create and sign the HTTP request
auto getMatchStatusRequest = CreateHttpRequest(URI(backendApiUrl + "requestmatchstatus?ticketId=" + ticketId),
HttpMethod::HTTP_GET, Aws::Utils::Stream::DefaultResponseStreamFactoryMethod);
requestSigner->SignRequest(*getMatchStatusRequest);
std::cout << "requesting match status...\n\n";
// make the HTTP request
auto requestMatchStatusResponse = httpClient->MakeRequest(getMatchStatusRequest);
ss.str(""); //clear the string stream
ss << requestMatchStatusResponse->GetResponseBody().rdbuf();
std::cout << ss.str() + "\n\n";
JsonValue matchStatusResponseJson = JsonValue(ss.str());
String playerSessionId = matchStatusResponseJson.View().GetString("PlayerSessionId");
int port = matchStatusResponseJson.View().GetInteger("Port");
String ip = matchStatusResponseJson.View().GetString("IpAddress");
std::cout << "Player Session ID: " + playerSessionId + "\n";
if(playerSessionId.compare("NotPlacedYet") == 0)
{
std::cout << "Not placed yet, try again in 2s\n\n";
}
else
{
std::cout << "Placement to session done! Connect to server and send player session ID\n\n";
int success = ConnectToServer(ip, port, playerSessionId);
// We're done, break out
break;
}
tries++;
if(tries < 10)
sleep(2);
}
//Shutdown the AWS SDK
Aws::ShutdownAPI(options);
return 0;
}