PLAYERONLINECNT
table.<?xml version="1.0" encoding="GBK" standalone="yes" ?><metalib name="tcaplus_tb" tagsetversion="1" version="1"><struct name="PLAYERONLINECNT" version="1" primarykey="TimeStamp,GameSvrID" splittablekey="TimeStamp"><entry name="TimeStamp" type="uint32" desc="Measured in minutes" /><entry name="GameSvrID" type="string" size="64" /><entry name="GameAppID" type="string" size="64" desc="gameapp id" /><entry name="OnlineCntIOS" type="uint32" defaultvalue="0" desc="The number of iOS online users" /><entry name="OnlineCntAndroid" type="uint32" defaultvalue="0" desc="The number of Android online users" /><entry name="BinaryLen" type="smalluint" defaultvalue="1" desc="Data length of the data source; skip the source check when the length is 0."/><entry name="binary" type="tinyint" desc="Binary" count= "1000" refer="BinaryLen" /><entry name="binary2" type="tinyint" desc="Binary 2" count= "1000" refer="BinaryLen" /><entry name="strstr" type="string" size="64" desc="String"/><index name="index_id" column="TimeStamp"/></struct></metalib>
// Access address of the target clusterstatic const char DIR_URL_ARRAY[][TCAPLUS_MAX_STRING_LENGTH] ={"tcp://10.191.***.99:9999","tcp://10.191.***.88:9999"};// The number of target cluster addressesstatic const int32_t DIR_URL_COUNT = 2;// Cluster ID of the target businessstatic const int32_t APP_ID = 3;// Table group ID of the target businessstatic const int32_t ZONE_ID = 1;// Target business passwordstatic const char * SIGNATURE = "*******";// Table name "PLAYERONLINECNT" of the target businessstatic const char * TABLE_NAME = "PLAYERONLINECNT";
// TCaplus service log classTcaplusService::TLogger* g_pstTlogger;LPTLOGCATEGORYINST g_pstLogHandler;LPTLOGCTX g_pstLogCtx;int32_t InitLog(){// Absolute path of the log configuration fileconst char* sLogConfFile = "tlogconf.xml";// Log class nameconst char* sCategoryName = "mytest";// Initialize the log handle using the configuration fileg_pstLogCtx = tlog_init_from_file(sLogConfFile);if (NULL == g_pstLogCtx){fprintf(stderr, "tlog_init_from_file failed.\\n");return -1;}// Get the log classg_pstLogHandler = tlog_get_category(g_pstLogCtx, sCategoryName);if (NULL == g_pstLogHandler){fprintf(stderr, "tlog_get_category(mytest) failed.\\n");return -2;}// Initialize the log handleg_pstTlogger = new TcaplusService::TLogger(g_pstLogHandler);if (NULL == g_pstTlogger){fprintf(stderr, "TcaplusService::TLogger failed.\\n");return -3;}return 0;}
// The client main class of the TCaplus service APITcaplusService::TcaplusServer g_stTcapSvr;// Meta information for the tableextern unsigned char g_szMetalib_tcaplus_tb[];LPTDRMETA g_szTableMeta = NULL;int32_t InitServiceAPI(){// Initializeint32_t iRet = g_stTcapSvr.Init(g_pstTlogger, /*module_id*/0, /*app id*/APP_ID, /*zone id*/ZONE_ID, /*signature*/SIGNATURE);if (0 != iRet){tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.Init failed, iRet: %d.", iRet);return iRet;}// Add the directory serverfor (int32_t i = 0; i< DIR_URL_COUNT; i++){iRet = g_stTcapSvr.AddDirServerAddress(DIR_URL_ARRAY[i]);if (0 != iRet){tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.AddDirServerAddress(%s) failed, iRet: %d.", DIR_URL_ARRAY[i], iRet);return iRet;}}// Get the meta description of the tableg_szTableMeta = tdr_get_meta_by_name((LPTDRMETALIB)g_szMetalib_tcaplus_tb, TABLE_NAME);if(NULL == g_szTableMeta){tlog_error(g_pstLogHandler, 0, 0,"tdr_get_meta_by_name(%s) failed.", TABLE_NAME);return -1;}// Register the data table (connect to the directory server, verify the password, and get the table routing) with 10-second timeout periodiRet = g_stTcapSvr.RegistTable(TABLE_NAME, g_szTableMeta, /*timeout_ms*/10000);if(0 != iRet){tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.RegistTable(%s) failed, iRet: %d.", TABLE_NAME, iRet);return iRet;}// Connect to all TcaplusDB proxy servers corresponding to the tableiRet = g_stTcapSvr.ConnectAll(/*timeout_ms*/10000, 0);if(0 != iRet){tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.ConnectAll failed, iRet: %d.", iRet);return iRet;}return 0;}
int32_t SendUpdateRequest(){// Request object classTcaplusService::TcaplusServiceRequest* pstRequest = g_stTcapSvr.GetRequest(TABLE_NAME);if (NULL == pstRequest){tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.GetRequest(%s) failed.", TABLE_NAME);return -1;}// Initialize the request objectint iRet = pstRequest->Init(TCAPLUS_API_UPDATE_REQ, NULL, 0, 0, 0, 0);if(0 != iRet){tlog_error(g_pstLogHandler, 0, 0, "pstRequest->Init(TCAPLUS_API_UPDATE_REQ) failed, iRet: %d.", iRet);return iRet;}// Add a record to the table according to the requestTcaplusService::TcaplusServiceRecord* pstRecord = pstRequest->AddRecord();if (NULL == pstRecord){tlog_error(g_pstLogHandler, 0, 0, "pstRequest->AddRecord() failed.");return -1;}PLAYERONLINECNT stPLAYERONLINECNT;memset(&stPLAYERONLINECNT, 0, sizeof(stPLAYERONLINECNT));// Specify the information of the key to be updatedstPLAYERONLINECNT.dwTimeStamp = 1;snprintf(stPLAYERONLINECNT.szGameSvrID, sizeof(stPLAYERONLINECNT.szGameSvrID), "%s", "mysvrid");// Specify the information of the value to be updatedsnprintf(stPLAYERONLINECNT.szGameAppID, sizeof(stPLAYERONLINECNT.szGameAppID), "%s", "myappid2");stPLAYERONLINECNT.dwOnlineCntIOS = 2;stPLAYERONLINECNT.dwOnlineCntAndroid = 2;// Set the record based on the TDR descriptioniRet = pstRecord->SetData(&stPLAYERONLINECNT, sizeof(stPLAYERONLINECNT));if(0 != iRet){tlog_error(g_pstLogHandler, 0, 0, "pstRecord->SetData() failed, iRet: %d.", iRet);return iRet;}// Send the request packetiRet= g_stTcapSvr.SendRequest(pstRequest);if(0 != iRet){tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.SendRequest failed, iRet: %d.", iRet);return iRet;}return 0;}
int RecvResp(TcaplusServiceResponse*& response){// Block the reception of response packets 5,000 times with each block lasting for 1 msunsigned int sleep_us = 1000;unsigned int sleep_count = 5000;do{usleep(sleep_us);response = NULL;int ret = g_stTcapSvr.RecvResponse(response);if (ret < 0){tlog_error(g_pstLogHandler, 0, 0, "tcaplus_server.RecvResponse failed. ret:%d", ret);return ret;}// Receive a response packetif (1 == ret){break;}} while ((--sleep_count) > 0);// Timeout period: 5 secondsif (0 == sleep_count){tlog_error(g_pstLogHandler, 0, 0, "tcaplus_server.RecvResponse wait timeout.");return -1;}return 0;}
int main(void) {// Initialize the logint ret = InitLog();if ( ret != 0){printf("init log failed\\n");return -1;}// Initialize the API clientret = InitServiceAPI();if ( ret != 0){printf("init InitServiceAPI failed\\n");return -1;}// Send the requestret = SendUpdateRequest();if (0 != ret){printf("SendUpdateRequest failed\\n");return -1;}// Receive the responseTcaplusServiceResponse* response = NULL;ret = RecvResp(response);if (0 != ret){printf("RecvResp failed\\n");return -1;}// Get the result, where "0" indicates successint32_t result = response->GetResult();if (0 != result){printf("the result is %d\\n", result);return -1;}return 0;}
Was this page helpful?