int CDbMgrServer::HandleSendPluto() { enum { SEND_COUNT = 1000, }; CPluto* u; int i = 0; while (!g_pluto_sendlist.Empty()) { u = g_pluto_sendlist.PopPluto(); CMailBox* mb = u->GetMailbox(); if(mb) { LogDebug("CDbMgrServer::HandleSendPluto", "u.GenLen()=%d", u->GetLen()); mb->PushPluto(u); } //每次只发送一定条数,主要用在loadAllAvatar处 if(++i > SEND_COUNT) { break; } } CEpollServer::HandleSendPluto(); return 0; }
//来自跨服的广播 int CWorldCrossclient::OnCrossClientBroadcast(CPluto& u) { //printf("CWorldCrossclient::OnCrossClientBroadcast \n"); if(m_nBcBaseappId == 0) { //挑一个baseapp来处理,挑完之后不会再变了 vector<CMailBox*>& mbs = GetServer()->GetAllServerMbs(); vector<CMailBox*>::iterator iter1 = mbs.begin(); for(; iter1 != mbs.end(); ++iter1) { CMailBox* mb2 = *iter1; if(mb2 && mb2->GetServerMbType() == SERVER_BASEAPP) { m_nBcBaseappId = mb2->GetMailboxId(); break; } } } CMailBox* mb = GetServerMailbox(m_nBcBaseappId); if(mb) { CPluto* u2 = new CPluto(u.GetBuff(), u.GetMaxLen()); u2->ReplaceField<uint16_t>(6, MSGID_BASEAPP_CROSSCLIENT_BROADCAST); mb->PushPluto(u2); } return 0; }
int CWorldLogin::NotifyClientToAttach(T_VECTOR_OBJECT* p) { if(p->size() != 3) { return -1; } const char* pszAccount = VOBJECT_GET_STR((*p)[0]); uint16_t baseapp_id = VOBJECT_GET_U16((*p)[1]); const char* pszKey = VOBJECT_GET_STR((*p)[2]); map<string, int>::const_iterator iter = m_accounts2fd.find(pszAccount); if(iter == m_accounts2fd.end()) { LogWarning("NotifyClientToAttach", "Account '%s' hasn't client", pszAccount); return -2; } LogDebug("NotifyClientToAttach", "account=%s;fd=%d", pszAccount, iter->second); CMailBox* mb = GetServer()->GetClientMailbox(iter->second); if(mb != NULL) { CMailBox* smb = GetServer()->GetServerMailbox(baseapp_id); if(smb == NULL) { LogWarning("NotifyClientToAttach", "error baseapp_id:%d", baseapp_id); return -3; } mb->RpcCall(GetRpcUtil(), MSGID_CLIENT_NOTIFY_ATTACH_BASEAPP, smb->GetServerName().c_str(), smb->GetServerPort(), pszKey); } return 0; }
// implementation void Post( u_long idReceiver, CMail* pMail, CQuery* pQuery ) { CPost* pPost = CPost::GetInstance(); pPost->m_csPost.Enter(); BOOL bResult = pPost->AddMail( idReceiver, pMail ) > 0; pPost->m_csPost.Leave(); if( bResult ) { char szQuery[QUERY_SIZE] = {0, }; CDbManager::MakeQueryAddMail( szQuery, pMail, idReceiver ); SQLINTEGER cbLen = SQL_NTS; BOOL bOk = pQuery->BindParameter( 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 128, 0, (void*)pMail->m_szTitle, 0, &cbLen ) && pQuery->BindParameter( 2, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 1024, 0, (void*)pMail->m_szText, 0, &cbLen ); if( bOk ) bOk = pQuery->Exec( szQuery ); if( bOk ) { CDPTrans::GetInstance()->SendPostMail( TRUE, idReceiver, pMail ); } else { Error( "BindParameter() or Exec(): PostDeposit()" ); pPost->m_csPost.Enter(); CMailBox* pMailBox = pPost->GetMailBox( idReceiver ); ASSERT( pMailBox ); pMailBox->RemoveMail( pMail->m_nMail ); pPost->m_csPost.Leave(); } } }
//检查一个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 CWorldMgrD::CreateCellInNewSpace(T_VECTOR_OBJECT* p) { if(p->size() != 4) { return -1; } CEntityMailbox& emb = VOBJECT_GET_EMB((*p)[0]); uint16_t etype = VOBJECT_GET_U16((*p)[1]); #ifdef __USE_MSGPACK charArrayDummy *d = (charArrayDummy*)VOBJECT_GET_BLOB((*p)[2]); #else const char* pszParams = VOBJECT_GET_STR((*p)[2]); #endif charArrayDummy& props = *((charArrayDummy*)((*p)[3]->vv.p)); uint16_t nCellappId = ChooseACellApp(etype); CMailBox* mb = GetServerMailbox(nCellappId); if(mb) { #ifdef __USE_MSGPACK mb->RpcCall(GetRpcUtil(), MSGID_CELLAPP_CREATE_CELL_IN_NEW_SPACE, emb, etype, *d, props); #else mb->RpcCall(GetRpcUtil(), MSGID_CELLAPP_CREATE_CELL_IN_NEW_SPACE, emb, etype, pszParams, props); #endif } return 0; }
int CWorldMgrD::init(const char* pszEtcFile) { LogInfo("CWorldMgrD::init()", ""); int nWorldInit = world::init(pszEtcFile); if(nWorldInit != 0) { return nWorldInit; } //将所有的 list<CMailBox*>& mbs = m_mbMgr.GetMailboxs(); list<CMailBox*>::iterator iter = mbs.begin(); for(; iter != mbs.end(); ++iter) { CMailBox* p = *iter; if(p->GetServerMbType() == SERVER_BASEAPP) { m_baseBalance.AddNewId(p->GetMailboxId()); } else if(p->GetServerMbType() == SERVER_CELLAPP) { m_cellBalance.AddNewId(p->GetMailboxId()); } } return 0; }
//获取连接的IP地址 int CEntityBase::lGetIPAddr(lua_State* L) { if (this->m_bHasClient) { int fd = this->GetClientFd(); CMailBox* mb = GetWorld()->GetServer()->GetClientMailbox(fd); if (mb) { lua_pushstring(L, mb->GetServerName().c_str()); return 1; } else { lua_pushstring(L, "GetIPAddr, client not connected"); lua_error(L); return 0; } } else { lua_pushstring(L, "GetIPAddr, entity has no client"); lua_error(L); return 0; } }
//获取一个流水号 uint32_t CWorldCrossserver::GetNextRpcSeq(CPluto& u, uint16_t sid, TENTITYTYPE etype, TENTITYID eid) { CMailBox* mb = u.GetMailbox(); if(mb == NULL) { return 0; } int fd = mb->GetFd(); int nSeq; if(m_lsFreeRpcSeq.empty()) { //没有空闲流水号了 ++m_nCrossRpcSeq; nSeq = m_nCrossRpcSeq; } else { nSeq = m_lsFreeRpcSeq.front(); m_lsFreeRpcSeq.pop_front(); } _SCrossClientInfo* pInfo = new _SCrossClientInfo(fd, sid, etype, eid); m_InRpc.insert(make_pair(nSeq, pInfo)); return nSeq; }
int CWorldCrossserver::OnCrossClientResp(CPluto& u) { CHECK_AND_GET_RPC_FIELD(u, nSeq, uint32_t); //检查流水号对应的外系统连接是否还在 map<uint32_t, _SCrossClientInfo*>::iterator iter1 = m_InRpc.find(nSeq); if(iter1 == m_InRpc.end()) { LogWarning("CWorldCrossserver::OnCrossClientResp", "notfound_seq=%d", nSeq); return -1; } _SCrossClientInfo* pInfo = iter1 ->second; CMailBox* mb = GetServer()->GetClientMailbox(pInfo->fd); if(mb == NULL) { //外系统已经断开连接了 LogWarning("CWorldCrossserver::OnCrossClientResp", "notfoundclient_seq=%d", nSeq); return -2; } CPluto* u2 = new CPluto; u2->Encode(MSGID_CROSSCLIENT_RESP) << pInfo->sid << pInfo->etype << pInfo->eid; //转发包内容 u2->FillBuff(u.GetBuff()+u.GetLen(), u.GetMaxLen()-u.GetLen()); u2->endPluto(); mb->PushPluto(u2); //删除数据,即每个流水号只能被回调一次 delete pInfo; m_InRpc.erase(iter1); m_lsFreeRpcSeq.push_back(nSeq); //流水号重用 return 0; }
int CWorldDbmgr::SelectAccount(T_VECTOR_OBJECT* p, CDbOper& db) { #if __PLAT_PLUG_IN || __PLAT_PLUG_IN_NEW if(p->size() != 3) #else if(p->size() != 4) #endif { return -1; } uint16_t nServerId = VOBJECT_GET_U16((*p)[0]); int32_t nFd = VOBJECT_GET_I32((*p)[1]); const char* s1 = VOBJECT_GET_STR((*p)[2]); #if __PLAT_PLUG_IN || __PLAT_PLUG_IN_NEW #else const char* s2 = VOBJECT_GET_STR((*p)[3]); #endif CPluto* u = new CPluto; string strErr; #if __PLAT_PLUG_IN || __PLAT_PLUG_IN_NEW if(db.SelectAccount(nFd, s1, *u, strErr) != 0) #else if(db.SelectAccount(nFd, s1, s2, *u, strErr) != 0) #endif { delete u; //cout << strErr << endl; LogWarning("SelectAccount_err", "%s", strErr.c_str()); return -2; } //通知db结果 CEpollServer* s = this->GetServer(); CMailBox* mb = s->GetServerMailbox(nServerId); if(mb) { #ifdef _WIN32 mb->RpcCall(*u); #else u->SetMailbox(mb); LogDebug("CWorldDbmgr::SelectAccount", "u.GenLen()=%d", u->GetLen()); g_pluto_sendlist.PushPluto(u); #endif } else { delete u; LogWarning("SelectAccount_err", "no mb!"); return -3; } return 0; }
int CWorldMgrD::CreateBaseFromDbByName(T_VECTOR_OBJECT* p) { if(p->size() < 3) { return -1; } uint8_t createFlag = VOBJECT_GET_U8((*p)[0]); const char* pszEntityType = VOBJECT_GET_STR((*p)[1]); const char* pszKey = VOBJECT_GET_STR((*p)[2]); uint16_t nBaseappId; if(p->size() > 3) { //指定了baseapp nBaseappId = VOBJECT_GET_U16((*p)[3]); } else { //未指定baseapp,选择一个 nBaseappId = ChooseABaseApp(pszEntityType); } #ifdef __TEST_LOGIN CEpollServer* s = this->GetServer(); CMailBox* mb = s->GetServerMailbox(nBaseappId); if (mb) { TENTITYID nOtherEntityId = 0; CPluto* u = new CPluto; (*u).Encode(MSGID_BASEAPP_LOOKUP_ENTITY_CALLBACK); (*u) << (uint64_t)0 << this->GetNextEntityId()<< createFlag << pszKey << nOtherEntityId; (*u) << this->GetDefParser().GetTypeId(pszEntityType); TDBID dbid2 = 0; (*u).ReplaceField(PLUTO_FILED_BEGIN_POS, dbid2); (*u) << EndPluto; u->SetMailbox(mb); LogDebug("CWorldMgrD::CreateBaseFromDbByName", "u.GenLen()=%d", u.GetLen()); mb->PushPluto(u); } #else CMailBox* mb = GetServerMailbox(SERVER_DBMGR); if(mb) { mb->RpcCall(GetRpcUtil(), MSGID_DBMGR_CREATEBASE_FROM_NAME, nBaseappId, createFlag, pszEntityType, pszKey); } #endif return 0; }
//创建新的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; }
// DbMgr 验证后的回调。 通过后通知baseapp 创建角色对象 int CWorldLogin::SelectAccountCallback(T_VECTOR_OBJECT* p) { if(p->size() != 3) { return -1; } int32_t fd = VOBJECT_GET_I32((*p)[0]); const char* pszAccount = VOBJECT_GET_STR((*p)[1]); uint8_t nRet = VOBJECT_GET_U8((*p)[2]); //printf("select account call back vector objects: fd: %d account: %s ret: %d\n",fd, pszAccount, nRet ); LogInfo("CWorldLogin::select_account_callback", "account=%s;fd=%d;ret=%d\n", pszAccount, fd, nRet); CMailBox* mb = GetServer()->GetClientMailbox(fd); CPluto* pu = new CPluto; if(nRet == 0) { m_fd2accounts.erase(fd); //验证失败,删除对应关系 m_accounts2fd.erase(pszAccount); //账号错误 (*pu).Encode(MSGID_CLIENT_LOGIN_RESP) << (uint8_t)ENUM_LOGIN_RET_ACCOUNT_PASSWD_NOMATCH << EndPluto; } else { //账号校验通过 if(mb) { //printf("set the client to authorize!\n"); mb->SetAuthz(MAILBOX_CLIENT_AUTHZ); } uint32_t tp = 0x11111111; (*pu).Encode(MSGID_CLIENT_LOGIN_RESP) << (uint8_t)ENUM_LOGIN_SUCCESS << tp << EndPluto; /* //创建account CMailBox* mb2 = GetServerMailbox(SERVER_BASEAPPMGR); if(mb2) { //标记1表示如果数据库中不存在,也要创建一个entity mb2->RpcCall(GetRpcUtil(), MSGID_BASEAPPMGR_CREATEBASE_FROM_NAME_ANYWHERE, (uint8_t)1, "Account", pszAccount); } */ } if(mb != NULL) { mb->PushPluto(pu); //printf("select account login call back success!\n"); } return 0; }
bool CWorldCrossclient::CcCheckClientRpc(CPluto& u) { CMailBox* mb = u.GetMailbox(); if(mb && mb->GetMailboxId() == EXTERN_MAILBOX_ID) { //如果消息来自跨服连接 return true; } return CheckClientRpc(u); }
int CWorldDbmgr::InsertDB(T_VECTOR_OBJECT* p, CDbOper& db) { if(p->size() != 3) { return -1; } CEntityMailbox& emb = VOBJECT_GET_EMB((*p)[0]); int32_t ref = VOBJECT_GET_I32((*p)[1]); SEntityPropFromPluto* p2 = (SEntityPropFromPluto*)((*p)[2]->vv.p); const string& strEntityName = GetDefParser().GetTypeName(p2->etype); //string strSql; //db.make_insert_sql(strEntityName, p2->data, strSql); //cout << strSql << endl; //string strSql2; //db.make_create_sql(strEntityName, strSql2); //cout << strSql2 << endl; //LogWarning("insert to db", "emb ref strEntityName = %s", strEntityName.c_str()); string strErr; TDBID newid = db.InsertEntity(strEntityName, p2->data, 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(emb.m_nServerMailboxId); if(mb) { #ifdef _WIN32 mb->RpcCall(GetRpcUtil(), MSGID_BASEAPP_INSERT_ENTITY_CALLBACK, emb, newid, ref, strErr.c_str()); #else CRpcUtil& rpc = GetRpcUtil(); CPluto* u = new CPluto; rpc.Encode(*u, MSGID_BASEAPP_INSERT_ENTITY_CALLBACK, emb, newid, ref, strErr.c_str()); u->SetMailbox(mb); LogDebug("CDbOper::InsertDB", "u.GenLen()=%d", u->GetLen()); g_pluto_sendlist.PushPluto(u); #endif } return 0; }
int LoadAllAvatars(lua_State* L) { const static char szEntity[] = "Avatar"; const static char szIndex[] = "account_name"; world* the_world = GetWorld(); CMailBox* mb = the_world->GetServerMailbox(SERVER_DBMGR); if(mb) { mb->RpcCall(the_world->GetRpcUtil(), MSGID_DBMGR_LOAD_ALL_AVATAR, szEntity, szIndex); } return 0; }
int LoadEntitiesOfType(lua_State* L) { const char* pszEntity = luaL_checkstring(L, 1); uint16_t nBaseappId = GetWorld()->GetMailboxId(); world* the_world = GetWorld(); CMailBox* mb = the_world->GetServerMailbox(SERVER_DBMGR); if(mb) { mb->RpcCall(the_world->GetRpcUtil(), MSGID_DBMGR_LOAD_ENTITIES_OF_TYPE, pszEntity, nBaseappId); } return 0; }
int CWorldDbmgr::init(const char* pszEtcFile) { LogDebug("CWorldDbmgr::init()", "a=%d", 1); int nWorldInit = world::init(pszEtcFile); if(nWorldInit != 0) { return nWorldInit; } try { GetDefParser().init(m_cfg->GetValue("init", "def_path").c_str()); GetDefParser().ReadDbCfg(m_cfg); } catch(const CException& e) { LogDebug("CWorldDbmgr::init().error", "%s", e.GetMsg().c_str()); return -1; } ////需要一个空的lua环境来保存LUA_TABLE类型的数据 /* m_L = lua_open(); if(m_L==NULL) { return -1; } luaL_newmetatable(m_L, "G_LUATABLES"); lua_setglobal(m_L, "G_LUATABLES"); */ //将所有的 list<CMailBox*>& mbs = m_mbMgr.GetMailboxs(); list<CMailBox*>::iterator iter = mbs.begin(); for(; iter != mbs.end(); ++iter) { CMailBox* p = *iter; if(p->GetServerMbType() == SERVER_BASEAPP) { m_baseBalance.AddNewId(p->GetMailboxId()); } //else if(p->GetServerMbType() == SERVER_CELLAPP) //{ // m_cellBalance.addNewId(p->GetMailboxId()); //} } return 0; }
//根据server_id获取服务器绑定端口 uint16_t world::GetServerPort(uint16_t sid) { list<CMailBox*>& mbs = m_mbMgr.GetMailboxs(); list<CMailBox*>::iterator iter = mbs.begin(); for(; iter != mbs.end(); ++iter) { CMailBox* pmb = *iter; if(pmb->GetMailboxId() == sid) { return pmb->GetServerPort(); } } return 0; }
int CWorldDbmgr::TableExcute(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::TableExcute", "nBaseappId=%d;ref=%d;strSql=%s", // nBaseappId, ref, strSql.c_str()); string strErr; uint8_t ret = db.TableExcute(strSql, strErr); if(ret != 0) { LogWarning("ExcuteDB_err", "ret=%d;err=%s", ret, strErr.c_str()); } //ref == 0 无返回 if (ref == 0) { return 0; } //通知db结果 CEpollServer* s = this->GetServer(); CMailBox* mb = s->GetServerMailbox(nBaseappId); if(mb) { #ifdef _WIN32 mb->RpcCall(GetRpcUtil(), MSGID_BASEAPP_TABLE_EXCUTE_CALLBACK, ref, ret ); #else CRpcUtil& rpc = GetRpcUtil(); CPluto* u = new CPluto; rpc.Encode(*u, MSGID_BASEAPP_TABLE_EXCUTE_CALLBACK, ref, ret); u->SetMailbox(mb); LogDebug("CDbOper::InsertDB", "u.GenLen()=%d", u->GetLen()); g_pluto_sendlist.PushPluto(u); #endif } return 0; }
int CWorldOther::Response2Browser(int nFd, const string& response) { //CMailBox* mb = GetServerMailbox(emb.m_nServerMailboxId); CMailBox* mb = GetServer()->GetClientMailbox(nFd); if (mb) { CPluto* u = new CPluto; (*u).Encode(MSGID_LOG_INSERT) <<response.c_str() << EndPluto; //这里是返回给浏览器 msg_id 用不到 就随便给个50以上的了 mb->PushPluto(u); LogDebug("Response2Browser", "fd: %d,response:%s",nFd, response.c_str()); } return 0; }
int CEntityBase::lNotifyClientMultiLogin(lua_State* L) { //first param is userdata const char* pszAccount = luaL_checkstring(L, 2); LogDebug("CEntityBase::lNotifyClientMultiLogin", "id=%d;Account=%s", m_id, pszAccount); CWorldBase& worldbase = GetWorldbase(); CMailBox* mb = worldbase.GetServerMailbox(SERVER_LOGINAPP); if(mb) { mb->RpcCall(worldbase.GetRpcUtil(), MSGID_LOGINAPP_NOTIFY_CLIENT_MULTILOGIN, pszAccount); } return 0; }
int CEntityParent::lWriteToDB(lua_State* L) { //first param is userdata world& the_world = *GetWorld(); int32_t ref = 0; if(m_dbid == 0) { luaL_checkany(L, 2); //callable object CLuaCallback& cb = the_world.GetCallback(); lua_pushvalue(L, 2); ref = (int32_t)cb.Ref(L); } else { //没有设置脏数据标记,返回 if(!m_bIsMysqlDirty) { LogDebug("CEntityParent::lWriteToDB", "not_dirty;etype=%d;id=%d;dbid=%d", m_etype, m_id, m_dbid); return 0; } } CMailBox* mb = the_world.GetServerMailbox(SERVER_DBMGR); if(mb) { if(m_dbid == 0) { mb->RpcCall(the_world.GetRpcUtil(), MSGID_DBMGR_INSERT_ENTITY, m_mymb, ref, *this); } else { mb->RpcCall(the_world.GetRpcUtil(), MSGID_DBMGR_UPDATE_ENTITY, m_mymb, m_dbid, *this); } } //清理脏数据标记,设置上次存盘时间 m_nTimestamp = time(NULL); m_bIsDirty = false; m_bIsMysqlDirty = false; LogInfo("CEntityParent::lWriteToDB", "etype=%d;id=%d;dbid=%d;ref=%d", m_etype, m_id, m_dbid, ref); return 0; }
//根据dbid查找entity int CWorldDbmgr::LookupEntityByDbId(T_VECTOR_OBJECT* p, CDbOper& db) { if(p->size() != 4) { return -1; } uint8_t nServerId = VOBJECT_GET_U8((*p)[0]); const char* szEntityName = VOBJECT_GET_STR((*p)[1]); TDBID dbid = VOBJECT_GET_U64((*p)[2]); int32_t ref = VOBJECT_GET_I32((*p)[3]); CPluto* u = new CPluto; string strErr; if(db.LookupEntityByDbId(szEntityName, dbid, ref, *u, strErr) != 0) { delete u; //cout << strErr << endl; LogWarning("LookupEntityByDbId_err", "%s", strErr.c_str()); return -2; } //通知db结果 CEpollServer* s = this->GetServer(); CMailBox* mb = s->GetServerMailbox(nServerId); if(mb) { #ifdef _WIN32 mb->RpcCall(*u); #else u->SetMailbox(mb); LogDebug("CWorldDbmgr::LookupEntityByDbId", "u.GenLen()=%d", u->GetLen()); g_pluto_sendlist.PushPluto(u); #endif } else { LogWarning("CWorldDbmgr::LookupEntityByDbId", "u.GenLen()=%d", u->GetLen()); delete u; } return 0; }
// 将指定的 pluto 发给 对应的服务器mailbox bool world::PushPlutoToMailbox(uint16_t nServerId, CPluto* u) { if(GetMailboxId() == nServerId) { //本服务器 GetServer()->AddLocalRpcPluto(u); } else { CMailBox* mb = GetServerMailbox(nServerId); if(mb) { mb->PushPluto(u); } } return true; }
//同步带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); } }
int CEntityBase::lNotifyClientToAttach(lua_State* L) { //first param is userdata const char* pszAccount = luaL_checkstring(L, 2); //测试数据日志 LogDebug("CEntityBase::lNotifyClientToAttach", "id=%d;Account=%s", m_id, pszAccount); //only Account can invoke, and control with scripts CWorldBase& worldbase = GetWorldbase(); CMailBox* mb = worldbase.GetServerMailbox(SERVER_LOGINAPP); if(mb) { const string& key = worldbase.MakeClientLoginKey(pszAccount, m_id); mb->RpcCall(worldbase.GetRpcUtil(), MSGID_LOGINAPP_NOTIFY_CLIENT_TO_ATTACH, pszAccount, m_mymb.m_nServerMailboxId, key.c_str()); } return 0; }
//同步带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); } } }
//通知db销毁account int CEntityBase::lNotifyDbDestroyAccountCache(lua_State* L) { //first param userdata const char* pszAccountName = luaL_checkstring(L, 2); static const string strEntityType = "Account"; world& the_world = *GetWorld(); /* luaL_checkany(L, 3); //callable object CLuaCallback& cb = the_world.GetCallback(); lua_pushvalue(L, 3); int ref = cb.Ref(L); */ CMailBox* mb = the_world.GetServerMailbox(SERVER_DBMGR); if(mb) { mb->RpcCall(the_world.GetRpcUtil(), MSGID_DBMGR_DEL_ACCOUNT_CACHE, pszAccountName, strEntityType); } return 0; }