//模拟错误的rpc包 bool err_avatar_rpc() { //远程方法名 CDefParser& defp = GetWorld()->GetDefParser(); const SEntityDef* pDef = defp.GetEntityDefByName("Avatar"); string strFunc("UseSkillReq"); uint16_t nFunc = (uint16_t)pDef->m_baseMethodsMap.GetIntByStr(strFunc); CPluto u; u.Encode(MSGID_BASEAPP_CLIENT_RPCALL) << nFunc; //其他参数 u << (uint64_t) 1 << (uint16_t) 2 << (uint16_t)3 << (uint8_t) 4 << (uint16_t) 5 ; u << (uint8_t) 6; //error u << EndPluto; //发送 printf("send err_cmd: %s \n", strFunc.c_str()); //print_hex_pluto(u); write_some(u.GetBuff(), u.GetLen()); //收取回应包 //async_read_some(); //CPluto* u2 = read_pluto(); //delete u2; return true; }
//随机走动 void random_move() { uint16_t x = 3016 + rand() % 300; uint16_t y = 2020 + rand() % 300; CPluto u; u.Encode(MSGID_BASEAPP_CLIENT_MOVE_REQ) << x << y << EndPluto; //u.Cryto(); write_some(u.GetBuff(), u.GetLen()); }
//创建新的space并在其中创建entity的cell部分 int CEntityBase::lCreateInNewSpace(lua_State* L) { //已经有cell或者cell正在创建之中 if(m_nCellState != E_HASNT_CELL) { lua_pushinteger(L, 1); return 1; } //first param userdata //附加了其他参数 #ifdef __USE_MSGPACK charArrayDummy d; msgpack::sbuffer sbuff; msgpack::packer<msgpack::sbuffer> pker(&sbuff); #else string strOtherParam; #endif if(lua_gettop(L) > 1) { if(lua_istable(L, 2)) { #ifdef __USE_MSGPACK LuaPickleToBlob(L, pker); #else LuaPickleToString(L, strOtherParam); #endif } } world* the_world = GetWorld(); CMailBox* mb = the_world->GetServerMailbox(SERVER_BASEAPPMGR); if(mb) { m_nCellState = E_CELL_IN_CREATING; CPluto* u = new CPluto; u->Encode(MSGID_BASEAPPMGR_CREATE_CELL_IN_NEW_SPACE); #ifdef __USE_MSGPACK d.m_s = sbuff.data(); d.m_l = (uint16_t)sbuff.size(); (*u) << m_mymb << m_etype << d; #else (*u) << m_mymb << m_etype << strOtherParam.c_str(); #endif PickleCellPropsToPluto(*u); (*u) << EndPluto; //LogDebug("CEntityBase::lCreateInNewSpace", "u->GetLen()=%d;", u->GetLen()); mb->PushPluto(u); } lua_pushinteger(L, 0); return 1; }
bool avatar_cell_rpc(const char* cmd) { list<string> ls; SplitString(cmd, '|', ls); if(ls.empty()) { printf("error rpc, empty string. \n"); return false; } //远程方法名 CDefParser& defp = GetWorld()->GetDefParser(); const SEntityDef* pDef = defp.GetEntityDefByName("Avatar"); string strFunc = ls.front(); ls.pop_front(); uint16_t nFunc = (uint16_t)pDef->m_cellMethodsMap.GetIntByStr(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) { if(ls.empty()) { printf("error rpc, less params, %s \n", cmd); return false; } const string& sv = ls.front(); u.FillPlutoFromStr(*iter, sv.c_str(), (unsigned long)sv.size()); ls.pop_front(); } } u << EndPluto; //发送 printf("send cell cmd: %s \n", cmd); //print_hex_pluto(u); write_some(u.GetBuff(), u.GetLen()); //收取回应包 //async_read_some(); //CPluto* u2 = read_pluto(); //delete u2; return true; }
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 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; }
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; }
//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 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; } }
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::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; }
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; }
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; }