bool LoadServerList(char *workDir, struct SERVER_LIST* pServerList) { FILE *fp = NULL; char filepath[MAX_PATH] = {0}; char* buffer = NULL; long lSize = 0; long result = 0; cJSON* pServerListNode = NULL; if(!pServerList) return false; path_combine(filepath, workDir, DEF_SERVER_IP_LIST_FILE);//g_PluginInfo.WorkDir, if(fp=fopen(filepath,"r")) { // obtain file size: fseek (fp , 0 , SEEK_END); lSize = ftell (fp); rewind (fp); // allocate memory to contain the whole file: buffer = (char*) malloc (sizeof(char)*lSize); if (!buffer) { fclose(fp); return false; } // copy the file into the buffer: result = fread (buffer,1,lSize,fp); if (result != lSize) { free(buffer); fclose(fp); return false; } pServerListNode = cJSON_Parse(buffer); if(pServerListNode) { ParseServerList(pServerListNode, pServerList); cJSON_Delete(pServerListNode); } free(buffer); fclose(fp); } return true; }
void LobbyClient::OnHttpResult(int err, const std::string &result, const HttpParam *headers) { HttpClient::Param::const_iterator it; switch( err ) { case 301: // moved permanently assert(STATE_IDLE != _state); ++_redirectCount; it = headers->find("Location"); // TODO: case insensitive search if( headers->end() == it || _redirectCount > LOBBY_MAX_REDIRECT ) { _state = STATE_IDLE; if( eventError ) { INVOKE(eventError)("lobby: redirect failed"); } return; } _lobbyUrl = it->second; _http->Get(_lobbyUrl, _param); break; case 200: // ok assert(STATE_IDLE != _state); switch( _state ) { case STATE_ANNOUNCE: { if( result.substr(0, 2) == "ok" ) { LARGE_INTEGER dueTime; dueTime.QuadPart = -300000000; // 30 seconds SetWaitableTimer(_timer, &dueTime, 0, NULL, NULL, FALSE); } else { _state = STATE_IDLE; TRACE("lobby: invalid server reply - %s", result.c_str()); if( eventError ) { INVOKE(eventError) ("invalid lobby reply"); } } break; } case STATE_LIST: { _state = STATE_IDLE; std::vector<std::string> svlist; if( ParseServerList(svlist, result) ) { if( eventServerListReply ) { INVOKE(eventServerListReply) (svlist); } } else { TRACE("lobby: invalid server reply - %s", result.c_str()); if( eventError ) { INVOKE(eventError) ("invalid lobby reply"); } } break; } case STATE_CANCEL: { CancelWaitableTimer(_timer); _state = STATE_IDLE; if( result.substr(0, 2) != "ok" ) { TRACE("lobby: invalid server reply - %s", result.c_str()); if( eventError ) { INVOKE(eventError) ("invalid lobby reply"); } } Release(); break; } } // end of switch( _state ) break; default: // unknown error _state = STATE_IDLE; std::stringstream s; s << "lobby: error " << err; if( eventError ) { INVOKE(eventError)(s.str()); } } }