//检查一个rpc调用是否合法 bool CWorldCrossserver::CSCheckClientRpc(CPluto& u) { CMailBox* mb = u.GetMailbox(); if(!mb) { //如果没有mb,是从本进程发来的包 return true; } if(mb->IsDelete()) { //已标记del的mb,其所有的包不再处理 return false; } uint8_t authz = mb->GetAuthz(); if(authz == MAILBOX_CLIENT_TRUSTED) { return true; } else if(authz == MAILBOX_CLIENT_UNAUTHZ) { //检查客户端地址是否可以信任 //return CheckTrustedMailbox(mb); return u.GetMsgId() == MSGID_CROSSSERVER_CHECK_MD5; } else if(authz == MAILBOX_CLIENT_AUTHZ) { //return CheckTrustedMailbox(mb); return true; } else { return false; } }
int CWorldCrossserver::FromRpcCall(CPluto& u) { //printf("CWorldLogin::from_rpc_call\n"); pluto_msgid_t msg_id = u.GetMsgId(); if(!CSCheckClientRpc(u)) { LogWarning("from_rpc_call", "invalid rpcall error.unknown msgid:%d\n", msg_id); return -1; } u.Decode(); int nRet = ERR_RPC_UNKNOWN_MSGID; switch(msg_id) { //case MSGID_CROSSSERVER_ON_CROSS_REQ: //{ // nRet = OnCrossReq(u); // break; //} case MSGID_CROSSSERVER_RPC: { nRet = OnCrossServerRpc(u); break; } case MSGID_CROSSSERVER_REGISTER_SERVER: { nRet = RegisterServer(u); break; } case MSGID_CROSSSERVER_CLIENT_RESP_PROXY: { nRet = OnCrossClientResp(u); break; } case MSGID_CROSSSERVER_CLIENT_BC_PROXY: { nRet = OnCrossClientBroadcast(u); break; } case MSGID_CROSSSERVER_CHECK_MD5: { nRet = CheckMd5(u); break; } default: { LogWarning("CWorldCrossserver::from_rpc_call", "unknown msgid:%d\n", msg_id); break; } } if(nRet != 0) { LogWarning("from_rpc_call", "rpc error.msg_id=%d;ret=%d\n", msg_id, nRet); } return 0; }
int CTimerdWorld::FromRpcCall(CPluto& u) { pluto_msgid_t msg_id = u.GetMsgId(); if(!CheckClientRpc(u)) { LogWarning("from_rpc_call", "invalid rpcall error.unknown msgid:%d\n", msg_id); return -1; } T_VECTOR_OBJECT* p = m_rpc.Decode(u); if(p == NULL) { LogWarning("from_rpc_call", "rpc decode error.unknown msgid:%d\n", msg_id); return -1; } if(u.GetDecodeErrIdx() > 0) { ClearTListObject(p); LogWarning("from_rpc_call", "rpc decode error.msgid:%d;pluto err idx=%d\n", msg_id, u.GetDecodeErrIdx()); return -2; } int nRet = -1; switch(msg_id) { case MSGID_ALLAPP_SHUTDOWN_SERVER: { nRet = ShutdownServer(p); break; } default: { LogWarning("from_rpc_call", "unknown msgid:%d\n", msg_id); } } if(nRet != 0) { LogWarning("from_rpc_call", "rpc error.msg_id=%d;ret=%d\n", msg_id, nRet); } ClearTListObject(p); return 0; }
int CWinTestWorld::FromRpcCall(CPluto& u) { using namespace mogo; //test code pluto_msgid_t msgid = u.GetMsgId(); switch(msgid) { case MSGID_BASEAPPMGR_REGISTERGLOBALLY: { //static CEpollServer s; //static CWorldMgrD w; //w.SetServer(&s); ////s.SetWorld(*w); //w.FromRpcCall(u); break; } case MSGID_DBMGR_INSERT_ENTITY: case MSGID_DBMGR_SELECT_ENTITY: case MSGID_DBMGR_UPDATE_ENTITY: case MSGID_DBMGR_RAW_MODIFY_NORESP: { //CWorldDbmgr w; //w.init("F:\\CW\\cw\\cw\\etc\\cw.etc.txt"); //CDbOper db(0); //string strErr; //db.Connect(w.GetDefParser().GetDbCfg(), w.GetDefParser().GetRedisCfg(), strErr); //w.FromRpcCall(u, db); break; } default: { //baseapp //static CWorldBase w; CWorldBase& w = (CWorldBase&)*g_pTheWorld; w.FromRpcCall(u); } } return 0; }
//检查一个rpc调用是否合法 bool world::CheckClientRpc(CPluto& u) { CMailBox* mb = u.GetMailbox(); if(!mb) { //如果没有mb,是从本进程发来的包 return true; } uint8_t authz = mb->GetAuthz(); //printf("authz status: %d\n", authz); pluto_msgid_t msg_id = u.GetMsgId(); if(authz == MAILBOX_CLIENT_TRUSTED) { printf("authz status: %d MAILBOX_CLIENT_TRUSTED:%d\n", authz,MAILBOX_CLIENT_TRUSTED); return true; } else if(authz == MAILBOX_CLIENT_AUTHZ) { //pluto_msgid_t msg_id = u.GetMsgId(); printf("authz status: %d MAILBOX_CLIENT_AUTHZ:%d msg_id: %d\n", authz,MAILBOX_CLIENT_AUTHZ, msg_id); return msg_id == MSGID_BASEAPP_CLIENT_RPCALL || msg_id == MSGID_BASEAPP_CLIENT_MOVE_REQ; } else if(authz == MAILBOX_CLIENT_UNAUTHZ) { printf("authz status: %d MAILBOX_CLIENT_UNAUTHZ:%d msg_id: %d\n", authz, MAILBOX_CLIENT_UNAUTHZ, msg_id); return msg_id == MSGID_LOGINAPP_LOGIN || msg_id == MSGID_BASEAPP_CLIENT_LOGIN || \ msg_id == MSGID_LOGINAPP_MODIFY_LOGIN_FLAG || msg_id == MSGID_LOGINAPP_SELECT_ACCOUNT_CALLBACK; } else { //printf("authz status: %d error: %d\n", authz, -1); return false; } }
int CWorldDbmgr::FromRpcCall(CPluto& u, CDbOper& db) { //printf("CWorldDbmgr::from_rpc_call\n"); //print_hex_pluto(u); pluto_msgid_t msg_id = -1; T_VECTOR_OBJECT* p = NULL; //这一段要加锁(2012/02/15改为不加锁) { //CMutexGuard _g(m_rpcMutex); msg_id = u.GetMsgId(); if(!CheckClientRpc(u)) { LogWarning("from_rpc_call", "invalid rpcall error.unknown msgid:%d\n", msg_id); return -1; } p = m_rpc.Decode(u); if(p == NULL) { LogWarning("from_rpc_call", "rpc Decode error.unknown msgid:%d\n", msg_id); return -1; } if(u.GetDecodeErrIdx() > 0) { ClearTListObject(p); //PrintHexPluto(u); LogWarning("from_rpc_call", "rpc Decode error.msgid:%d;pluto err idx=%d\n", msg_id, u.GetDecodeErrIdx()); return -2; } } //这一段不用加锁 int nRet = -1; switch(msg_id) { case MSGID_DBMGR_INSERT_ENTITY: { nRet = InsertDB(p, db); break; } case MSGID_DBMGR_SELECT_ENTITY: { nRet = LookupEntityByDbId(p, db); break; } case MSGID_DBMGR_UPDATE_ENTITY: { nRet = UpdateEntity(p, db); break; } case MSGID_DBMGR_UPDATE_ENTITY_REDIS: { nRet = UpdateEntityToRedis(p, db); break; } case MSGID_DBMGR_SELECT_ACCOUNT: { nRet = SelectAccount(p, db); break; } case MSGID_DBMGR_RAW_MODIFY_NORESP: { nRet = QueryModifyNoResp(p, db); break; } case MSGID_DBMGR_CREATEBASE_FROM_NAME: { nRet = LookupEntityByName(p, db); break; } case MSGID_DBMGR_LOAD_ALL_AVATAR: { nRet = LoadAllAvatars(p, db); break; } case MSGID_DBMGR_LOAD_ENTITIES_OF_TYPE: { nRet = LoadEntitiesOfType(p, db); break; } case MSGID_DBMGR_SHUTDOWN_SERVER: { nRet = ShutdownServer(p); break; } case MSGID_DBMGR_REDIS_HASH_LOAD: { nRet = RedisHashLoad(p, db); break; } case MSGID_DBMGR_REDIS_HASH_SET: { nRet = RedisHashSet(p, db); break; } case MSGID_DBMGR_REDIS_HASH_DEL: { nRet = RedisHashDel(p, db); break; } case MSGID_DBMGR_DEL_ACCOUNT_CACHE: { nRet = DestroyAccountCache(p); break; } case MSGID_DBMGR_UPDATE_ITEMS: { nRet = UpdateArrayItemsToDb(p, db); break; } case MSGID_DBMGR_LOADING_ITEMS: { nRet = LoadingArrayItemsToInventory(p, db); break; } case MSGID_DBMGR_INCREMENTAL_UPDATE_ITEMS: { nRet = IncremantalUpdateItems(p, db); break; } case MSGID_DBMGR_TABLE_SELECT: { nRet = TableSelect(p, db); break; } case MSGID_DBMGR_UPDATE_BATCH: { nRet = TableUpdateBatch(p, db); break; } case MSGID_DBMGR_TABLE_INSERT: { nRet = TableInsert(p, db); break; } case MSGID_DBMGR_TABLE_EXCUTE: { nRet = TableExcute(p, db); break; } case MSGID_DBMGR_TABLE2_SELECT: { nRet = Table2Select(p, db); break; } case MSGID_DBMGR_TABLE2_INSERT: { nRet = Table2Insert(p, db); break; } case MSGID_DBMGR_TABLE2_EXCUTE: { nRet = Table2Excute(p, db); break; } default: { break; } } if(nRet != 0) { LogWarning("from_rpc_call", "rpc error.msg_id=%d;ret=%d\n", msg_id, nRet); } ClearTListObject(p); return 0; }
int CWorldMgrD::FromRpcCall(CPluto& u) { //PrintHexPluto(u); pluto_msgid_t msg_id = u.GetMsgId(); #ifndef __TEST_LOGIN if(!CheckClientRpc(u)) { LogWarning("from_rpc_call", "invalid rpcall error.unknown msgid:%d\n", msg_id); return -1; } #endif // !__TEST_LOGIN T_VECTOR_OBJECT* p = m_rpc.Decode(u); if(p == NULL) { LogWarning("from_rpc_call", "rpc Decode error.unknown msgid:%d\n", msg_id); return -1; } if(u.GetDecodeErrIdx() > 0) { ClearTListObject(p); LogWarning("from_rpc_call", "rpc Decode error.msgid:%d;pluto err idx=%d\n", msg_id, u.GetDecodeErrIdx()); return -2; } int nRet = -1; switch(msg_id) { case MSGID_BASEAPPMGR_REGISTERGLOBALLY: { LogInfo("CWorldMgrD::FromRpcCall() MSGID_BASEAPPMGR_REGISTERGLOBALLY", ""); nRet = RegisterGlobally(p); break; } case MSGID_BASEAPPMGR_CREATEBASE_FROM_NAME_ANYWHERE: { nRet = CreateBaseFromDbByName(p); break; } case MSGID_BASEAPPMGR_CREATEBASE_FROM_NAME: { nRet = CreateBaseFromDbByName(p); break; } case MSGID_BASEAPPMGR_CREATE_CELL_IN_NEW_SPACE: { nRet = CreateCellInNewSpace(p); break; } case MSGID_BASEAPPMGR_CREATEBASE_ANYWHERE: { nRet = CreateBaseAnywhere(p); break; } case MSGID_BASEAPPMGR_SHUTDOWN_SERVERS: { nRet = ShutdownAllServers(p, u); break; } case MSGID_BASEAPPMGR_ON_SERVER_SHUTDOWN: { nRet = OnServerShutdown(p); break; } } if(nRet != 0) { LogWarning("from_rpc_call", "rpc error.msg_id=%d;ret=%d\n", msg_id, nRet); } ClearTListObject(p); return 0; }
SBaseappLoginData* login_account(const char* pszHost, int nPort, const char* pszAccount, const char* pszPasswd) { printf("try to login account: [ %s : %d ] [ %s ] \n", pszHost, nPort, pszAccount); if(this->connect(pszHost, nPort)) { //发送登录包 CPluto u; m_rpc.Encode(u, MSGID_LOGINAPP_LOGIN, pszAccount, pszPasswd); write_some(u.GetBuff(), u.GetLen()); } else { return NULL; } bool bWait = true; int nWaitCount = 0; SBaseappLoginData* bd = NULL; while(bWait) { CPluto* u = read_pluto(); if(u) { T_VECTOR_OBJECT* p = m_rpc.Decode(*u); if(p) { pluto_msgid_t msgid = u->GetMsgId(); if(msgid == MSGID_CLIENT_LOGIN_RESP) { uint8_t nRet = VOBJECT_GET_U8((*p)[0]); printf("login ret=%d\n", nRet); if(nRet != 0) { bWait = false; } } else if(msgid == MSGID_CLIENT_NOTIFY_ATTACH_BASEAPP) { const char* pszBaseappIp = VOBJECT_GET_STR((*p)[0]); uint16_t nBaseappPort = VOBJECT_GET_U16((*p)[1]); const char* pszKey = VOBJECT_GET_STR((*p)[2]); bd = new SBaseappLoginData; bd->m_strBase.assign(pszBaseappIp); bd->m_strKey.assign(pszKey); bd->m_unPort = nBaseappPort; bWait = false; printf("redirect to baseapp [ %s : %d ] [ %s ] \n", pszBaseappIp, nBaseappPort, pszKey); //断开到loginapp的连接 this->disconnect(); } else if(msgid == MSGID_CLIENT_NOTIFY_MULTILOGIN) { int a = 0; ++a; printf("mutlilogin, [%s] \n", pszAccount); } else { int a = 0; ++a; } ClearTListObject(p); } delete u; } else { if(++nWaitCount > 3) { return NULL; } } } return bd; }
//in parent class , this method has a pure virtual access limited int CWorldLogin::FromRpcCall(CPluto& u) { //printf("CWorldLogin::from_rpc_call\n"); //printf("handle pluto in FromRpcCall()! : start!\n"); pluto_msgid_t msg_id = u.GetMsgId(); //printf("message id : %d\n", msg_id); if(!CheckClientRpc(u)) { LogWarning("FromRpcCall", "invalid rpcall error.unknown msgid:%d\n", msg_id); return -1; } T_VECTOR_OBJECT* p = m_rpc.Decode(u); if(p == NULL) { LogWarning("FromRpcCall", "rpc decode error.unknown msgid:%d\n", msg_id); return -1; } if(u.GetDecodeErrIdx() > 0) { ClearTListObject(p); LogWarning("FromRpcCall", "rpc decode error.msgid:%d;pluto err idx=%d\n", msg_id, u.GetDecodeErrIdx()); return -2; } printf("MSGID_LOGINAPP_MODIFY_LOGIN_FLAG:%d\n", MSGID_LOGINAPP_MODIFY_LOGIN_FLAG); int nRet = -1; switch(msg_id) { case MSGID_LOGINAPP_LOGIN: { AddClientFdToVObjectList(u.GetMailbox()->GetFd(), p); nRet = AccountLogin(p); break; } case MSGID_LOGINAPP_SELECT_ACCOUNT_CALLBACK: { nRet = SelectAccountCallback(p); break; } case MSGID_LOGINAPP_NOTIFY_CLIENT_TO_ATTACH: { nRet = NotifyClientToAttach(p); break; } case MSGID_LOGINAPP_MODIFY_LOGIN_FLAG: { nRet = ModifyLoginFlag(p); break; } case MSGID_ALLAPP_SHUTDOWN_SERVER://103 { nRet = ShutdownServer(p); break; } default: { LogWarning("CWorldLogin::from_rpc_call", "unknown msgid:%d\n", msg_id); break; } } if(nRet != 0) { LogWarning("from_rpc_call", "rpc error.msg_id=%d;ret=%d\n", msg_id, nRet); } ClearTListObject(p); //printf("handle pluto in FromRpcCall()! : end!\n"); return 0; }
int CWorldOther::FromRpcCall(CPluto& u, CDbOper& db) { //printf("CWorldOther::from_rpc_call\n"); //print_hex_pluto(u); pluto_msgid_t msg_id = -1; T_VECTOR_OBJECT* p = NULL; //这一段要加锁(2012/02/15改为不加锁) { //CMutexGuard _g(m_rpcMutex); msg_id = u.GetMsgId(); if(!CheckClientRpc(u)) { LogWarning("from_rpc_call", "invalid rpcall error.unknown msgid:%d\n", msg_id); return -1; } p = m_rpc.Decode(u); if(p == NULL) { LogWarning("from_rpc_call", "rpc Decode error.unknown msgid:%d\n", msg_id); return -1; } if(u.GetDecodeErrIdx() > 0) { ClearTListObject(p); //PrintHexPluto(u); LogWarning("from_rpc_call", "rpc Decode error.msgid:%d;pluto err idx=%d\n", msg_id, u.GetDecodeErrIdx()); return -2; } } //这一段不用加锁 int nRet = -1; switch(msg_id) { case MSGID_LOG_INSERT: { nRet = InsertDB(p, db); break; } case MSGID_OTHER_HTTP_REQ: { nRet = ReqUrl(p); break; } case MSGID_ALLAPP_SHUTDOWN_SERVER: { nRet = ShutdownServer(p); break; } case MSGID_OTHER_ADD_GLOBALBASE: { nRet = RegisterGlobally(p); break; } case MSGID_OTHER_YUNYING_API: { nRet = SupportApi(p, u.GetMailbox(), db); break; } case MSGID_OTHER_CLIENT_RESPONSE: { nRet = Response2Browser(p); PrintHexPluto(u); break; } //case MSGID_OTHER_LOGIN_VERIFY: //{ // //todo:verify // nRet = SdkServerVerify(p, u); // break; //} // case MSGID_OTHER_PLAT_API: // { // nRet = PlatApi(p, u.GetMailbox(), db); // break; // } default: { LogWarning("from_rpc_call", "rpc unkown msg_id = %d\n", msg_id, nRet); break; } } if(nRet != 0) { LogWarning("from_rpc_call", "rpc error.msg_id=%d;ret=%d\n", msg_id, nRet); } ClearTListObject(p); return 0; }
int CWorldOther::SdkServerVerify(T_VECTOR_OBJECT* p, CPluto& u) { #if 0 //注释掉老代码 if (p->size() != 4) { return -1; } string& url = VOBJECT_GET_SSTR((*p)[0]); int32_t nFd = VOBJECT_GET_I32((*p)[1]); string& strAccount = VOBJECT_GET_SSTR((*p)[2]); string& strPlatId = VOBJECT_GET_SSTR((*p)[3]); CMailBox* pmb = u.GetMailbox(); if (NULL == pmb) { return -1; } CPluto* duplicate = new CPluto(u.GetBuff(), u.GetMaxLen()); duplicate->SetMailbox(pmb); int ret = threadpool_add_job(g_threadpool, ThreadJob_SdkServerVerify, (void*)(duplicate)); if (ret != 0) { //直接返回服务器繁忙,请稍后再试 //printf("服务器繁忙,请稍后再试!\n"); //std::cout << "服务器繁忙,请稍后再试!" << endl; LogWarning("CWorldOther::SdkServerVerify", "threadpool list is full."); CPluto* u2 = new CPluto; u2->Encode(MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK); (*u2)<< int32_t(-2) << nFd << strAccount << strPlatId << EndPluto; g_pluto_sendlist.PushPluto(u2); //不适合多线程发送 //pmb->RpcCall(GetRpcUtil(), MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, -2, nFd, strAccount, strPlatId); } return 0; #endif if (p->size() != 4) { return -1; } string& url = VOBJECT_GET_SSTR((*p)[0]); int32_t nFd = VOBJECT_GET_I32((*p)[1]); string& strAccount = VOBJECT_GET_SSTR((*p)[2]); string& strPlatId = VOBJECT_GET_SSTR((*p)[3]); CMailBox* pmb = u.GetMailbox(); pluto_msgid_t msg_id = u.GetMsgId();; string resp = ""; int ret = GetUrl_new(url.c_str(), resp); if (ret != CURLE_OK) { LogWarning("CWorldOther::SdkServerVerify", "%s ret = %d", strAccount.c_str(), ret); //todo:faild. SyncRpcCall(g_pluto_sendlist, SERVER_LOGINAPP, MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, ret, nFd, strAccount, strPlatId); return 0; } Trim(resp); cJSON* json = cJSON_Parse(resp.c_str()); cJSON* childJs = cJSON_GetObjectItem(json, "code"); int rst = cJSON_GetObjectItem(json, "code")->valueint; if (rst == 1) { //succeed. if (strAccount.c_str() != cJSON_GetObjectItem(json, "msg")->valuestring) { ret = 0; } else { LogWarning("CWorldOther::SdkServerVerify", "strAccount[%s] msg[%s]", strAccount.c_str(), cJSON_GetObjectItem(json, "msg")->valuestring); ret = 1; //pmb->RpcCall(GetRpcUtil(), MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, int32_t(1), nFd, strAccount, strPlatId); } } else { //failed. LogWarning("CWorldOther::SdkServerVerify", "strAccount[%s] rst[%d]", strAccount.c_str(), rst); ret = -1; //pmb->RpcCall(GetRpcUtil(), MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, -1, nFd, strAccount, strPlatId); } SyncRpcCall(g_pluto_sendlist, SERVER_LOGINAPP, MSGID_LOGINAPP_LOGIN_VERIFY_CALLBACK, ret, nFd, strAccount, strPlatId); return 0; }