int CWorldDbmgr::TableInsert(T_VECTOR_OBJECT* p, CDbOper& db) { if (p->size() != 3) { LogError("CWorldDbmgr::TableInsert", "p->size()=%d", p->size()); return -1; } uint16_t nBaseappId = VOBJECT_GET_U16((*p)[0]); const string& strSql = VOBJECT_GET_SSTR((*p)[1]); uint32_t ref = VOBJECT_GET_U32((*p)[2]); //LogDebug("CWorldDbmgr::TableInsert", "nBaseappId=%d;ref=%d;strSql=%s", // nBaseappId, ref, strSql.c_str()); string strErr; TDBID newId = db.TableInsert(strSql, strErr); if(newId == 0) { LogWarning("InsertDB_err", "newid=0;err=%s", strErr.c_str()); //cout << strErr << endl; //return -2; } //通知db结果 CEpollServer* s = this->GetServer(); CMailBox* mb = s->GetServerMailbox(nBaseappId); if(mb) { #ifdef _WIN32 mb->RpcCall(GetRpcUtil(), MSGID_BASEAPP_TABLE_INSERT_CALLBACK, ref, newId ); #else CRpcUtil& rpc = GetRpcUtil(); CPluto* u = new CPluto; rpc.Encode(*u, MSGID_BASEAPP_TABLE_INSERT_CALLBACK, ref, newId); u->SetMailbox(mb); LogDebug("CDbOper::InsertDB", "u.GenLen()=%d", u->GetLen()); g_pluto_sendlist.PushPluto(u); #endif } 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; }
//同步带client标记的属性给客户端 void CEntityParent::SyncClientProp(int32_t nPropId, const VOBJECT& v) { //获得当前entity的client fd static const string strClient = "client"; map<string, VOBJECT*>::iterator iter = m_data.find(strClient); if(iter != m_data.end()) { //当前已经有client了 world* pWorld = GetWorld(); lua_State* L = pWorld->GetLuaState(); ClearLuaStack(L); CLuaCallback& cb = pWorld->GetLuaTables(); int ref = (int)(iter->second->vv.i32); //LogDebug("CEntityParent::SyncClientProp", "m_id=%d;ref=%d", m_id, ref); cb.GetObj(L, ref); if(lua_istable(L, -1)) { lua_rawgeti(L, -1, g_nMailBoxServerIdKey); int fd = (int)lua_tointeger(L, -1); CMailBox* mb = pWorld->GetServer()->GetClientMailbox(fd); if(mb) { //LogDebug("CEntityParent::SyncClientProp", "m_id=%d;nPropId=%d;", m_id, nPropId); CPluto* u = new CPluto; (*u).Encode(MSGID_CLIENT_AVATAR_ATTRI_SYNC) << (uint16_t)nPropId; u->FillPluto(v); (*u) << EndPluto; mb->PushPluto(u); } } ClearLuaStack(L); } }
//同步带cell标记的属性 void CEntityBase::SyncBaseAndCellProp(int32_t nPropId, const VOBJECT& v) { int nCellId = GetCellServerId(); if(nCellId > 0) { CMailBox* mb = GetWorld()->GetServerMailbox(nCellId); if(mb) { CPluto* u = new CPluto; (*u).Encode(MSGID_CELLAPP_ENTITY_ATTRI_SYNC) << m_id << m_etype << (uint16_t)nPropId; u->FillPluto(v); (*u) << EndPluto; //LogDebug("CEntityBase::SyncBaseAndCellProp", "u->GetLen()=%d;", u->GetLen()); mb->PushPluto(u); } } }
bool CWorldCrossclient::CcCheckClientRpc(CPluto& u) { CMailBox* mb = u.GetMailbox(); if(mb && mb->GetMailboxId() == EXTERN_MAILBOX_ID) { //如果消息来自跨服连接 return true; } return CheckClientRpc(u); }
//来自跨服的回调 int CWorldCrossclient::OnCrossClientResp(CPluto& u) { //printf("CWorldCrossclient::onCrossClientResp\n"); CHECK_AND_GET_RPC_FIELD(u, sid, uint16_t); CMailBox* mb = GetServerMailbox(sid); //printf("CWorldCrossclient::onCrossClientResp 222, sid=%d;mb=%x\n", sid, mb); if(mb) { //发回给发起调用的那个entity CPluto* u2 = new CPluto; u2->Encode(MSGID_BASEAPP_ENTITY_RPC) << sid; u2->FillBuff(u.GetBuff()+u.GetLen(), u.GetMaxLen()-u.GetLen()); u2->endPluto(); mb->PushPluto(u2); } return 0; }
int CWorldDbmgr::DestroyAccountCache(T_VECTOR_OBJECT* p) { if(p->size() != 2) { LogError("CWorldDbmgr::DestroyAccountCache", "p->size()=%d", p->size()); return -1; } //const CEntityMailbox& emb = VOBJECT_GET_EMB((*p)[0]); const string& strKey = VOBJECT_GET_SSTR((*p)[0]); const string& strEntityType = VOBJECT_GET_SSTR((*p)[1]); //const int32_t ref = VOBJECT_GET_I32((*p)[3]); //先查找该key相关的entity是否已经创建出来了 SEntityLookup* pLookup = LookupEntityInfo(strKey, strEntityType); if(pLookup != NULL) { //LogDebug("CWorldDbmgr::DestroyAccountCache", "entity exists;strKey=%s;eid=%d;baseapp=%d", strKey.c_str(), pLookup->eid, pLookup->sid); CMailBox* mb = GetServerMailbox(pLookup->sid); if(mb) { CPluto* u = new CPluto; u->Encode(MSGID_BASEAPP_DEL_ACCOUNT_CACHE_CALLBACK); (*u) << pLookup->eid << EndPluto; u->SetMailbox(mb); //LogDebug("CWorldDbmgr::LookupEntityByName", "u.GenLen()=%d", u->GetLen()); DeleteEntityInfo(strKey, strEntityType); g_pluto_sendlist.PushPluto(u); return 0; } } LogError("CWorldDbmgr::DestroyAccountCache", "pLookup is not exists.strKey = %s, strEntityType = %s ",strKey.c_str(), strEntityType.c_str()); return -1; //DeleteEntityInfo(strKey, strEntityType); //回调 //RpcCall(emb.m_nServerMailboxId, MSGID_BASEAPP_DEL_ACCOUNT_CACHE_CALLBACK, emb); //return 0; }
//转发跨服调用 int CWorldCrossclient::OnCrossServerRpc(CPluto& u) { //跨服服务名 CHECK_AND_GET_RPC_FIELD(u, strService, string); //检查对应的跨服服务器是否已连接 CMailBox* pmb = ((CCrossclientServer*)GetServer())->GetExternMailbox(strService); if(pmb == NULL) { //外系统当前不可用,记录日志;返回错误消息也无用 LogWarning("CWorldCrossclient::OnCrossServerRpc", "extern error,%s\n", strService.c_str()); return 0; } CPluto* u2 = new CPluto; u2->Encode(MSGID_CROSSSERVER_RPC); //转发包内容 u2->FillBuff(u.GetBuff()+u.GetLen(), u.GetMaxLen()-u.GetLen()); u2->endPluto(); pmb->PushPluto(u2); 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 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; }
bool CEntityBase::PickleClientToPluto(CPluto& u) { //类似于CEntityParent::pickle_to_pluto,打包所有需要存盘的字段 //打包所有待client标记的字段 const SEntityDef* pDef = this->GetEntityDef(); if(pDef) { u << m_etype << m_id << m_dbid; //必须包含entity_type,以便接包时查询def数据 LogDebug("CEntityBase::PickleClientToPluto", "m_etype=%d;m_id=%d;m_dbid=%d", m_etype, m_id, m_dbid); map<string, _SEntityDefProperties*>::const_iterator iter = pDef->m_properties.begin(); for(; iter != pDef->m_properties.end(); ++iter) { _SEntityDefProperties* p = iter->second; if(IsClientFlag(p->m_nFlags)) { const string& strEntityName = iter->first; map<string, VOBJECT*>::const_iterator iter2 = m_data.find(strEntityName); if(iter2 == m_data.end()) { //todo warning... continue; } //u << iter2->first.c_str(); uint16_t attr_id = (uint16_t)(pDef->m_propertiesMap.GetIntByStr(iter2->first)); u << attr_id; //LogDebug("CEntityBase::PickleClientToPluto", "attr_id=%d;first=%s", attr_id, iter2->first.c_str()); u.FillPluto(*(iter2->second)); } } //print_hex_pluto(u); } return pDef != NULL; }
//来自原始服的跨服调用 int CWorldCrossserver::OnCrossServerRpc(CPluto& u) { //printf("CWorldCrossserver::OnCrossServerRpc 111\n"); CHECK_AND_GET_RPC_FIELD(u, uSrcServerId, uint16_t); CHECK_AND_GET_RPC_FIELD(u, etype, TENTITYTYPE); CHECK_AND_GET_RPC_FIELD(u, eid, TENTITYID); CHECK_AND_GET_RPC_FIELD(u, strGlobal, string); //CHECK_AND_GET_RPC_FIELD(u, strFuncId, uint16_t); uint32_t nSeq = GetNextRpcSeq(u, uSrcServerId, etype, eid); if(nSeq == 0) { return -1; } //printf("CWorldCrossserver::OnCrossServerRpc 222, %d - %d - %d - %s - seq =%d \n", uSrcServerId, etype, eid, strGlobal.c_str(), nSeq); map<string, CEntityMailbox*>::iterator iter1 = m_globalBases.find(strGlobal); if(iter1 == m_globalBases.end()) { return -2; } CEntityMailbox* emb = iter1->second; CMailBox* mb = GetServerMailbox(emb->m_nServerMailboxId); if(mb) { CPluto* u2 = new CPluto; u2->Encode(MSGID_BASEAPP_ENTITY_RPC) << (*emb); u2->FillBuff(u.GetBuff()+u.GetLen(), 2); //msg_id u2->FillField(nSeq); //填充流水号字段 u2->FillBuff(u.GetBuff()+u.GetLen()+2, u.GetMaxLen()-u.GetLen()-2);//copy其他字段 u2->endPluto(); mb->PushPluto(u2); return 0; } return -2; }
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; }
//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 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 CWorldDbmgr::IncremantalUpdateItems(T_VECTOR_OBJECT* p, CDbOper& db) { if ( p->size() != 5 ) { LogError("CWorldDbmgr::IncremantalUpdateItems", "p->size()=%d", p->size()); return -1; } const string & tblName = VOBJECT_GET_SSTR((*p)[0]); const string & optName = VOBJECT_GET_SSTR((*p)[1]); const uint16_t nBaseappId = VOBJECT_GET_U16((*p)[2]); const int32_t ref = VOBJECT_GET_I32((*p)[3]); const string& itemsData = VOBJECT_GET_SSTR((*p)[4]); LogDebug("CWorldDbmgr::IncremantalUpdateItems", "tblName=%s;optName=%s;nBaseappId=%d;ref=%d", tblName.c_str(), optName.c_str(), nBaseappId, ref); CPluto* u = new CPluto(); u->FillBuff(itemsData.c_str(), itemsData.size()); u->SetMaxLen(u->GetLen()); string strErr; uint8_t ret = -1; if ( optName.compare("update") == 0 ) { ret = db.IncrementalUpdateItems(tblName, nBaseappId, *u, ref, strErr); if( ret != 0 ) { CPluto* u1 = new CPluto; u1->Encode(MSGID_BASEAPP_UPDATE_ITEMS_CALLBACK); *u1 << ref << (uint16_t)1 << strErr<< EndPluto; CMailBox* mb = GetServerMailbox(nBaseappId); if(mb) { u1->SetMailbox(mb); //LogDebug("CWorldDbmgr::IncremantalUpdateItems", "u1.GenLen()=%d", u1->GetLen()); g_pluto_sendlist.PushPluto(u1); } else { delete u1; u1 = NULL; LogWarning("CWorldDbmgr::IncremantalUpdateItems", ""); } } delete u; u = NULL; return 0; } else if( optName.compare("insert") == 0 ) { ret = db.IncremantalInsertItems(tblName, nBaseappId, *u, ref, strErr); if( ret != 0 ) { CPluto* u1 = new CPluto; u1->Encode(MSGID_BASEAPP_INSERT_ITEMS_CALLBACK); *u1 << ref << (uint16_t)1 << strErr<< EndPluto; CMailBox* mb = GetServerMailbox(nBaseappId); if(mb) { u1->SetMailbox(mb); //LogDebug("CWorldDbmgr::IncremantalInsertItems", "u1.GenLen()=%d", u1->GetLen()); g_pluto_sendlist.PushPluto(u1); } else { delete u1; u1 = NULL; LogWarning("CWorldDbmgr::IncremantalInsertItems", ""); } } delete u; u = NULL; return 0; } }
int CWorldDbmgr::LookupEntityByName(T_VECTOR_OBJECT* p, CDbOper& db) { if(p->size() != 4) { return -1; } uint16_t nBaseappId = VOBJECT_GET_U16((*p)[0]); uint8_t nCreateFlag = VOBJECT_GET_U8((*p)[1]); const char* pszEntityName = VOBJECT_GET_STR((*p)[2]); const char* pszKey = VOBJECT_GET_STR((*p)[3]); const string& strKey(pszKey); //先查找该key相关的entity是否已经创建出来了 SEntityLookup* pLookup = LookupEntityInfo(strKey, pszEntityName); if(pLookup != NULL) { //LogInfo("CWorldDbmgr::LookEntityByName", "entity exists;key=%s;eid=%d;baseapp=%d", \ // pszKey, pLookup->eid, pLookup->sid); CMailBox* mb = GetServerMailbox(pLookup->sid); if(mb) { CPluto* u = new CPluto; u->Encode(MSGID_BASEAPP_ENTITY_MULTILOGIN); (*u) << pLookup->eid << EndPluto; u->SetMailbox(mb); //LogDebug("CWorldDbmgr::LookupEntityByName", "u.GenLen()=%d", u->GetLen()); g_pluto_sendlist.PushPluto(u); } return 0; } TENTITYID new_eid = MyGetNextEntityId(); CPluto* u = new CPluto; string strErr; if(db.LookupEntityByName(nCreateFlag, pszEntityName, strKey, new_eid, *u, nBaseappId, strErr) != 0) { delete u; //cout << strErr << endl; LogWarning("LookEntityByName_err", "%s", strErr.c_str()); return -2; } //add for lookup CreateNewEntityToLookup(strKey, pszEntityName, new_eid, nBaseappId); //通知db结果 CEpollServer* s = this->GetServer(); CMailBox* mb = s->GetServerMailbox(nBaseappId); if(mb) { #ifdef _WIN32 mb->RpcCall(*u); #else u->SetMailbox(mb); LogDebug("CWorldDbmgr::LookupEntityByName", "u.GenLen()=%d", u->GetLen()); g_pluto_sendlist.PushPluto(u); #endif } else { LogWarning("CWorldDbmgr::LookupEntityByName", "u.GenLen()=%d", u->GetLen()); delete u; } return 0; }
bool random_cell_rpc() { //远程方法名 CDefParser& defp = GetWorld()->GetDefParser(); const SEntityDef* pDef = defp.GetEntityDefByName("Avatar"); int nMaxFuncId = (int)pDef->m_cellMethods.size(); int nFunc = rand() % nMaxFuncId; const string& strFunc = pDef->m_cellMethodsMap.GetStrByInt(nFunc); ostringstream oss; oss << strFunc; CPluto u; u.Encode(MSGID_BASEAPP_CLIENT_RPC2CELL_VIA_BASE) << nFunc; //其他参数 map<string, _SEntityDefMethods*>::const_iterator iter11 = pDef->m_cellMethods.find(strFunc); if(iter11 != pDef->m_cellMethods.end()) { _SEntityDefMethods* pmethod = iter11->second; list<VTYPE>::const_iterator iter = pmethod->m_argsType.begin(); for(; iter != pmethod->m_argsType.end(); ++iter) { VTYPE vt = *iter; string sv; switch(vt) { case V_STR: sv.assign("abcd"); break; case V_INT8: case V_UINT8: case V_INT16: case V_UINT16: case V_INT32: case V_UINT32: case V_INT64: case V_UINT64: case V_FLOAT32: case V_FLOAT64: sv.assign("14"); break; case V_BLOB: sv.assign("{}"); break; default: sv.assign(""); break; } oss << "|" << sv; u.FillPlutoFromStr(*iter, sv.c_str(), (unsigned long)sv.size()); } } u << EndPluto; //发送 printf("send cell_cmd: %d %s \n", nFunc, oss.str().c_str()); //print_hex_pluto(u); write_some(u.GetBuff(), u.GetLen()); //收取回应包 //async_read_some(); //CPluto* u2 = read_pluto(); //delete u2; return true; }
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; }
int BroadcastClientRpc(lua_State* L) { const char* pszEntity = luaL_checkstring(L, 1); const char* pszFunc = luaL_checkstring(L, 2); world* the_world = GetWorld(); if (!the_world) { return 0; } const SEntityDef* pDef = the_world->GetDefParser().GetEntityDefByName(pszEntity); if (!pDef) { return 0; } map<string, _SEntityDefMethods*>::const_iterator iter11 = \ pDef->m_clientMethods.find(pszFunc); if(iter11 == pDef->m_clientMethods.end()) { return 0; } //把pszEntity和pszFunc从堆栈中移除掉 lua_remove(L, 1); lua_remove(L, 1); const _SEntityDefMethods* pMethods = iter11->second; const list<VTYPE>& args = pMethods->m_argsType; int nArgCount = lua_gettop(L); if(nArgCount != (int)args.size()) { return 0; } uint16_t nFuncId = (uint16_t)pDef->m_clientMethodsMap.GetIntByStr(pszFunc); CPluto* u = new CPluto; u->Encode(MSGID_BASEAPP_BROADCAST_CLIENT_PRC); (*u) << the_world->GetDefParser().GetTypeId(pszEntity) << nFuncId; //打包rpc的所有参数为一个string int idx = 0; list<VTYPE>::const_iterator iter = args.begin(); for(; iter != args.end(); ++iter) { ++idx; VTYPE vt = *iter; u->FillPlutoFromLua(vt, L, idx); } (*u) << EndPluto; the_world->GetServer()->AddLocalRpcPluto(u); vector<CMailBox*>& mbs = the_world->GetServer()->GetAllServerMbs(); vector<CMailBox*>::iterator iter2 = mbs.begin(); for(; iter2 != mbs.end(); ++iter2) { CMailBox* basemb = *iter2; if(basemb && basemb->GetServerMbType() == SERVER_BASEAPP) { //发给每个其他baseapp的mb的pluto都是发给本地进程的那个pluto的拷贝 CPluto* u2 = new CPluto(u->GetBuff(), u->GetLen()); basemb->PushPluto(u2); } } 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; }
int lIncrementalUpdateItems(lua_State* L) { int n = lua_gettop(L); if( n != 4 ) { LogError("lIncrementalUpdateItems", "Parameters number from lua not enough"); lua_pop(L, n); lua_pushnumber(L, -1); return 1; } if( LUA_TTABLE != lua_type(L, 1) && LUA_TSTRING != lua_type(L, 2) && LUA_TSTRING != lua_type(L, 3)) { LogError("Parameters type lua error", "%d", -1); lua_pop(L, n); lua_pushnumber(L, -1); return 1; } luaL_checkany(L, 4); const char* tName = lua_tostring(L, 2); const string tblName(tName); //更新的表名字 const SEntityDef* pDef = GetWorldbase().GetDefParser().GetEntityDefByName(tblName); if( !pDef ) { LogError("world:init().error", "%s", "Not the Entitydef"); lua_pushnumber(L, -1); return 1; } CLuaCallback& cb = GetWorld()->GetCallback(); int32_t ref = (int32_t)cb.Ref(L); const char* oName = lua_tostring(L, 3); lua_pop(L, 2); const string optName(oName); //操作内型 uint16_t nBaseappId = GetWorld()->GetMailboxId(); CPluto *u = new CPluto(); u->Encode(MSGID_DBMGR_INCREMENTAL_UPDATE_ITEMS); *u << tblName << optName << nBaseappId << ref << (uint16_t)0; int i = 1; while(true) { map<string, _SEntityDefProperties*>::const_iterator iter = pDef->m_properties.begin(); lua_pushnumber(L, i++); lua_gettable(L, -2); if( lua_type(L, -1) != LUA_TTABLE ) { break; } //独立处理道具的id字段 lua_pushstring(L, "id"); lua_gettable(L, -2); if( lua_type(L, -1) == LUA_TNIL ) { LogError("table Item data is not enough: ", "%s", "id"); delete u; u = NULL; cb.Unref(L, ref); lua_pushnumber(L, -1); return 1; } u->FillPlutoFromLua(V_INT64, L, -1); lua_pop(L, 1); //处理def中存盘字段 for(; iter != pDef->m_properties.end(); ++iter) { const _SEntityDefProperties* pProp = iter->second; if( IsBaseFlag( pProp->m_nFlags ) && pProp->m_bSaveDb ) { //cout<<"attribution name = "<<pProp->m_name.c_str()<<endl; lua_pushstring(L, pProp->m_name.c_str()); lua_gettable(L, -2); if( lua_type(L, -1) == LUA_TNIL ) { LogError("table Item data is not enough: ", "%s", pProp->m_name.c_str()); delete u; u = NULL; cb.Unref(L, ref); lua_pushnumber(L, -1); return 1; } u->FillPlutoFromLua(pProp->m_nType, L, -1); lua_pop(L, 1); } } lua_pop(L, 1); } (*u) << EndPluto; uint32_t pos = (MSGLEN_TEXT_POS + sizeof(uint16_t)*2 + tblName.size() + optName.size() + sizeof(nBaseappId) + sizeof(int32_t)); uint16_t value = (u->GetLen() - pos - sizeof(uint16_t)); u->ReplaceField(pos, value); //PrintHexPluto(*u); CMailBox* mb = GetWorldbase().GetServerMailbox(SERVER_DBMGR); if(mb) { mb->PushPluto(u); lua_pushnumber(L, 0); return 1; } else { delete u; u = NULL; cb.Unref(L, ref); lua_pushnumber(L, -1); return 1; } }
//mogo.UpdateBatchToDb(arrayList, table, uniq, cbF) int lUpdateBatchToDb(lua_State* L) { int n = lua_gettop(L); if( n != 4 && n != 3) { LogError("lUpdateBatchToDb", "Parameters number from lua not enough"); lua_pop(L, n); lua_pushnumber(L, 1); return 1; } if( LUA_TTABLE != lua_type(L, 1) || LUA_TSTRING != lua_type(L, 2) || LUA_TSTRING != lua_type(L, 3) ) { LogError("Parameters type lua error", "%d", -1); lua_pop(L, n); lua_pushnumber(L, 2); //lua_pop(L, 2); return 1; } CLuaCallback& cb = GetWorld()->GetCallback(); int32_t ref = LUA_REFNIL; if(n == 4) { luaL_checkany(L, 4); ref = (int32_t)cb.Ref(L); } //cout<<lua_gettop(L)<<endl; const char* pszUniqKey = lua_tostring(L, 3); lua_pop(L, 1); const string uniqKey(pszUniqKey); const char* item = lua_tostring(L, 2); lua_pop(L, 1); const string itemName(item); uint16_t nBaseappId = GetWorld()->GetMailboxId(); //map<string, map<string, VOBJECT> > items; CPluto *u = new CPluto(); u->Encode(MSGID_DBMGR_UPDATE_BATCH); //ostringstream oss; *u << itemName << uniqKey << nBaseappId << ref << (uint16_t)0; //char str[10240]; //const string itemName= "Item"; const SEntityDef* pDef = GetWorldbase().GetDefParser().GetEntityDefByName(itemName); if( !pDef ) { LogError("lUpdateBatchToDb", "Not the Entitydef"); lua_pushnumber(L, 3); delete u; u = NULL; if(LUA_REFNIL != ref) cb.Unref(L, ref); return 1; } int i = 1; while(true) { map<string, _SEntityDefProperties*>::const_iterator iter = pDef->m_properties.begin(); //CPluto *u = new CPluto(); //cout<<u->GetLen()<<endl; //LogDebug("parse table list start", "%d", 1); //oss <<"["; lua_pushnumber(L, i++); lua_gettable(L, -2); if( lua_type(L, -1) != LUA_TTABLE ) { break; } for(; iter != pDef->m_properties.end(); ++iter) { const _SEntityDefProperties* pProp = iter->second; //VOBJECT *v = new VOBJECT(); if(IsBaseFlag(pProp->m_nFlags)) { //cout<<"attribution name = "<<pProp->m_name.c_str()<<endl; lua_pushstring(L, pProp->m_name.c_str()); lua_gettable(L, -2); if( lua_type(L, -1) == LUA_TNIL ) { LogError("lUpdateBatchToDb", "table Item[%s] data is nil. ", pProp->m_name.c_str()); delete u; u = NULL; if(LUA_REFNIL != ref) cb.Unref(L, ref); lua_pushnumber(L, -1); return 1; } u->FillPlutoFromLua(pProp->m_nType, L, -1); lua_pop(L, 1); } } lua_pop(L, 1); //LogDebug("parse table list end", "%d", 1); } //printf("%d", u->GetLen()); (*u) << EndPluto; // PrintHexPluto(*u); //printf("%d, %d", u->GetLen(), itemName.size()); //*u << itemName << uniqKey << nBaseappId << ref << (uint16_t)0; uint32_t pos = (MSGLEN_TEXT_POS + sizeof(uint16_t) + itemName.size() + sizeof(uint16_t) + uniqKey.size() + sizeof(nBaseappId) + sizeof(int32_t)); uint16_t value = (u->GetLen() - pos - sizeof(uint16_t)); //printf("%d %d", pos, value); u->ReplaceField(pos, value); //PrintHexPluto(*u); CMailBox* mb = GetWorldbase().GetServerMailbox(SERVER_DBMGR); if(mb) { //LogInfo("upate items data to db ", "msg_id = %d, itemName = %s, status = %d", MSGID_DBMGR_UPDATE_BATCH, itemName.c_str(), 1); mb->PushPluto(u); //以下两行为测试数据用 } else { if(LUA_REFNIL != ref) cb.Unref(L, ref); delete u; u = NULL; } return 0; }
int lWriteArrayToDb(lua_State* L) { int n = lua_gettop(L); if( n != 4 ) { LogError("lWriteArrayToDb", "Parameters number from lua not enough"); lua_pop(L, n); lua_pushnumber(L, -1); return 1; } if( LUA_TTABLE != lua_type(L, 1) && LUA_TSTRING != lua_type(L, 2) && LUA_TNUMBER != lua_type(L, 3) ) { LogError("Parameters type lua error", "%d", -1); lua_pop(L, n); lua_pushnumber(L, -1); return 1; } luaL_checkany(L, 4); const char* item = lua_tostring(L, 2); const string itemName(item); const SEntityDef* pDef = GetWorldbase().GetDefParser().GetEntityDefByName(itemName); if( !pDef ) { LogError("world:init().error", "%s", "Not the Entitydef"); lua_pushnumber(L, -1); return 1; } CLuaCallback& cb = GetWorld()->GetCallback(); int32_t ref = (int32_t)cb.Ref(L); TDBID dbid = lua_tonumber(L, 3); lua_pop(L, 2); uint16_t nBaseappId = GetWorld()->GetMailboxId(); CPluto *u = new CPluto(); u->Encode(MSGID_DBMGR_UPDATE_ITEMS); *u << itemName << dbid << nBaseappId << ref << (uint16_t)0; int i = 1; while(true) { map<string, _SEntityDefProperties*>::const_iterator iter = pDef->m_properties.begin(); //CPluto *u = new CPluto(); //cout<<u->GetLen()<<endl; //LogDebug("parse table list start", "%d", 1); //oss <<"["; lua_pushnumber(L, i++); lua_gettable(L, -2); if( lua_type(L, -1) != LUA_TTABLE ) { break; } for(; iter != pDef->m_properties.end(); ++iter) { const _SEntityDefProperties* pProp = iter->second; //VOBJECT *v = new VOBJECT(); if(IsBaseFlag(pProp->m_nFlags) && pProp->m_bSaveDb ) { //cout<<"attribution name = "<<pProp->m_name.c_str()<<endl; lua_pushstring(L, pProp->m_name.c_str()); lua_gettable(L, -2); if( lua_type(L, -1) == 0 ) { LogError("table Item data is not enough", "%s", pProp->m_name.c_str()); delete u; u = NULL; cb.Unref(L, ref); lua_pushnumber(L, -1); return 1; } u->FillPlutoFromLua(pProp->m_nType, L, -1); lua_pop(L, 1); } } lua_pop(L, 1); //LogDebug("parse table list end", "%d", 1); } //printf("%d", u->GetLen()); (*u) << EndPluto; // PrintHexPluto(*u); //printf("%d, %d", u->GetLen(), itemName.size()); uint32_t pos = (MSGLEN_TEXT_POS + sizeof(uint16_t) + itemName.size() + sizeof(dbid) + sizeof(nBaseappId) + sizeof(int32_t)); uint16_t value = (u->GetLen() - pos - sizeof(uint16_t)); //printf("%d %d", pos, value); u->ReplaceField(pos, value); //PrintHexPluto(*u); CMailBox* mb = GetWorldbase().GetServerMailbox(SERVER_DBMGR); if(mb) { //LogInfo("lWriteArrayToDb", "msg_id = %d, itemName = %s, status = %d", MSGID_DBMGR_UPDATE_ITEMS, itemName.c_str(), 1); mb->PushPluto(u); lua_pushnumber(L, 0); return 1; } else { delete u; u = NULL; cb.Unref(L, ref); lua_pushnumber(L, -1); return 1; } }
int CEntityBase::GiveClient(lua_State* L, int fd) { static const string strClient = "client"; int old_fd = -1; ClearLuaStack(L); map<string, VOBJECT*>::iterator iter = m_data.lower_bound(strClient); if(iter != m_data.end() && iter->first == strClient) { CWorldBase& the_world = GetWorldbase(); CLuaCallback& cb = the_world.GetLuaTables(); int ref = (int)(iter->second->vv.i32); cb.GetObj(L, ref); LogDebug("CEntityBase::GiveClient", "m_id=%d;ref=%d", m_id, ref); if(lua_istable(L, -1)) { lua_rawgeti(L, -1, g_nMailBoxServerIdKey); old_fd = (int)lua_tointeger(L, -1); //printf("old_fd:%d,new_fd:%d\n", old_fd, fd); lua_pop(L, 1); lua_pushinteger(L, fd); lua_rawseti(L, -2, g_nMailBoxServerIdKey); } } else { //先用table,以后再改为userdata;table的话可以在lua里修改id,sid等几个值 NewClientMailbox(L, fd, m_etype, m_id); CLuaCallback& cb = GetWorld()->GetLuaTables(); int nRef = cb.Ref(L); LogDebug("CEntityBase::GiveClient", "m_id=%d;nRef=%d", m_id, nRef); VOBJECT* v = new VOBJECT; v->vt = V_LUATABLE; v->vv.i32 = nRef; m_data.insert(iter, make_pair("client", v) ); } this->SetClientFd(fd); m_bHasClient = true;//获得client标记 //通知客户端attach到entity,并刷所有带CLIENT标记的数据给客户端 CMailBox* mb = GetWorld()->GetServer()->GetClientMailbox(fd); if(mb) { CPluto* u = new CPluto; u->Encode(MSGID_CLIENT_ENTITY_ATTACHED); #ifdef __RELOGIN //客户端一旦登录,则先生成一个key值,用于断线重连时使用 const string& key = GetWorldbase().MakeClientReLoginKey("", m_id); s_clientReLoginKey = key; (*u) << key.c_str(); LogDebug("CEntityBase::GiveClient", "s_clientReLoginKey=%s;m_id=%d", s_clientReLoginKey.c_str(), m_id); #endif if(PickleClientToPluto(*u)) { (*u) << EndPluto; mb->PushPluto(u); //LogDebug("CEntityBase::GiveClient", "u->GetLen()=%d;", u->GetLen()); } else { delete u; } //如果有cell则通知cell同步数据 this->NotifyCellSyncClientAttris(); } //通知cell刷aoi数据给客户端 int nCellId = GetCellServerId(); if(nCellId > 0) { GetWorld()->RpcCall(nCellId, MSGID_CELLAPP_PICKLE_AOI_ENTITIES, m_id); } //通知脚本 { //clear_lua_stack(L); int _n = EntityMethodCall(L, this, "onClientGetBase", 0, 0); lua_pop(L, _n); } if(old_fd > 0 && old_fd != fd) { //关闭老的连接 GetWorldbase().KickOffFd(old_fd); } 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 CEntityBase::lTable2Insert(lua_State* L) { //param 1 is userdata uint32_t nCbId = (uint32_t)luaL_checkint(L, 2); //callback id const char* pszTbl = luaL_checkstring(L, 3); //table name luaL_checkany(L, 4); //table if(!lua_istable(L, 4)) { lua_pushstring(L, "tableInsert,param 4 need a table."); lua_error(L); return 0; } world* the_world = GetWorld(); CMailBox* mb = the_world->GetServerMailbox(SERVER_DBMGR); if(mb == NULL) { return 0; } CDefParser& def = the_world->GetDefParser(); const SEntityDef* pDef = def.GetEntityDefByName(pszTbl); if(pDef == NULL) { lua_pushfstring(L, "error entity name:%s", pszTbl); lua_error(L); return 0; } CPluto* u = new CPluto; u->Encode(MSGID_DBMGR_TABLE2_INSERT) << m_mymb.m_nServerMailboxId << m_id << nCbId << def.GetTypeId(pszTbl); lua_pushnil(L); while(lua_next(L, 4) != 0) { const char* pszKey = luaL_checkstring(L, -2); map<string, _SEntityDefProperties*>::const_iterator iter = pDef->m_properties.find(pszKey); if(iter != pDef->m_properties.end()) { _SEntityDefProperties* p = iter->second; if(p && p->m_bSaveDb) { u->FillField<uint16_t>(pDef->m_propertiesMap.GetIntByStr(pszKey)); u->FillPlutoFromLua(p->m_nType, L, 6); lua_pop(L, 1); continue; } } delete u; lua_pushfstring(L, "%s hasn't field %s or field not need to save.", pszTbl, pszKey); lua_error(L); return 0; } u->endPluto(); //print_hex_pluto(*u); mb->PushPluto(u); return 0; }
//根据一个已知的mb,坐标等数据创建cell entity int CEntityBase::lCreateCellEntity(lua_State* L) { //已经有cell或者cell正在创建之中 if(m_nCellState != E_HASNT_CELL) { lua_pushinteger(L, 1); return 1; } //first param userdata //其他参数: base_mailbox, x, y, string if(!lua_istable(L, 2)) { lua_pushstring(L, "createCellEntity need a table param"); lua_error(L); return -1; } //检查目标base的server_id lua_rawgeti(L, 2, g_nMailBoxServerIdKey); int nServerId = luaL_checkint(L, -1); lua_rawgeti(L, 2, g_nMailBoxEntityIdKey); TENTITYID nId = (TENTITYID)luaL_checkint(L, -1); int16_t x = (int16_t)luaL_checkint(L, 3); int16_t y = (int16_t)luaL_checkint(L, 4); const char* szMask = luaL_checkstring(L, 5); //LogDebug("CEntityBase::lCreateCellEntity", szMask); CPluto* u = new CPluto; u->Encode(MSGID_BASEAPP_CREATE_CELL_VIA_MYCELL); (*u) << nId << m_mymb << x << y << szMask; PickleCellPropsToPluto(*u); (*u) << EndPluto; world* the_world = GetWorld(); if(nServerId == the_world->GetMailboxId()) { m_nCellState = E_CELL_IN_CREATING; the_world->GetServer()->AddLocalRpcPluto(u); } else { CMailBox* mb = the_world->GetServerMailbox(nServerId); if(mb) { m_nCellState = E_CELL_IN_CREATING; //LogDebug("CEntityBase::lCreateCellEntity", "nServerId=%d;the_world->GetMailboxId()=%d;u->GetLen()=%d;", nServerId, the_world->GetMailboxId(),u->GetLen()); mb->PushPluto(u); } else { delete u; } } lua_pushinteger(L, 0); return 1; }