| 313 | } |
| 314 | |
| 315 | void GSDKInternal::decodeHeartbeatResponse(const std::string& responseJson) |
| 316 | { |
| 317 | Json::CharReaderBuilder jsonReaderFactory; |
| 318 | std::unique_ptr<Json::CharReader> jsonReader(jsonReaderFactory.newCharReader()); |
| 319 | Json::Value heartbeatResponse; |
| 320 | JSONCPP_STRING jsonParseErrors; |
| 321 | bool parsedSuccessfully = jsonReader->parse(responseJson.c_str(), responseJson.c_str() + responseJson.length(), &heartbeatResponse, &jsonParseErrors); |
| 322 | |
| 323 | if (!parsedSuccessfully) { |
| 324 | GSDK::logMessage("Failed to parse heartbeat"); |
| 325 | GSDK::logMessage(jsonParseErrors); |
| 326 | GSDK::logMessage("Message: " + responseJson); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | try { |
| 331 | if (heartbeatResponse.isMember("sessionConfig")) |
| 332 | { |
| 333 | std::lock_guard<std::mutex> lock(m_configMutex); |
| 334 | Json::Value sessionConfig = heartbeatResponse["sessionConfig"]; |
| 335 | for (Json::ValueIterator i = sessionConfig.begin(); i != sessionConfig.end(); ++i) |
| 336 | { |
| 337 | if ((*i).isString()) |
| 338 | { |
| 339 | m_configSettings[i.key().asCString()] = (*i).asCString(); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // Update initial players only if this is the first time populating it. |
| 344 | if (m_initialPlayers.empty() && sessionConfig.isMember("initialPlayers")) |
| 345 | { |
| 346 | Json::Value players = sessionConfig["initialPlayers"]; |
| 347 | |
| 348 | for (Json::ArrayIndex i = 0; i < players.size(); ++i) |
| 349 | { |
| 350 | m_initialPlayers.push_back(players[i].asCString()); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (sessionConfig.isMember("metadata")) |
| 355 | { |
| 356 | Json::Value sessionMetadata = sessionConfig["metadata"]; |
| 357 | for (Json::ValueIterator i = sessionMetadata.begin(); i != sessionMetadata.end(); ++i) |
| 358 | { |
| 359 | if ((*i).isString()) |
| 360 | { |
| 361 | m_configSettings[i.key().asCString()] = (*i).asCString(); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if (heartbeatResponse.isMember("nextScheduledMaintenanceUtc")) |
| 368 | { |
| 369 | tm nextMaintenance = parseDate(heartbeatResponse["nextScheduledMaintenanceUtc"].asCString()); |
| 370 | time_t nextMaintenanceTime = cGSDKUtils::tm2timet_utc(&nextMaintenance); |
| 371 | time_t cachedMaintenanceTime = cGSDKUtils::tm2timet_utc(&m_cachedScheduledMaintenance); |
| 372 | double diff = difftime(nextMaintenanceTime, cachedMaintenanceTime); |