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 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 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; }
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; }
// 将指定的 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); } }
//同步带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); } } }
//来自原始服的跨服调用 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 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 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; }
//根据一个已知的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 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; }
// 登录请求,初步处理过后,转给dbMgr 验证 int CWorldLogin::AccountLogin(T_VECTOR_OBJECT* p) { //printf("handle in CWorldLogin::AccountLogin()! : start!\n"); if(p->size() != 3) { return -1; } if(!m_bCanLogin) { printf("server no service\n"); string& strAccount = VOBJECT_GET_SSTR((*p)[0]); string& strPasswd = VOBJECT_GET_SSTR((*p)[1]); int32_t nFd = VOBJECT_GET_I32((*p)[2]); Trim(strAccount); Trim(strPasswd); const char* pszAccount = strAccount.c_str(); const char* pszPasswd = strPasswd.c_str(); bool status = true; if(strcmp(pszAccount, pszPasswd)) { status = false; } //printf("strAccount cmp strPasswd : %d\n", strcmp(pszAccount, pszPasswd)); string constr = strAccount.append(pszPasswd); CMailBox* mb = GetServer()->GetClientMailbox(nFd); if(mb) { CPluto* u = new CPluto; (*u).Encode(MSGID_CLIENT_LOGIN_RESP) << (uint8_t)ENUM_LOGIN_NO_SERVICE << (int32_t)nFd << (bool)status << (string)constr << EndPluto; mb->PushPluto(u); } return 0; } string& strAccount = VOBJECT_GET_SSTR((*p)[0]); string& strPasswd = VOBJECT_GET_SSTR((*p)[1]); int32_t nFd = VOBJECT_GET_I32((*p)[2]); //删除账户名/密码两边的空格,mysql中select * from table where s = '***'中不会区分***是否带空格 Trim(strAccount); Trim(strPasswd); const char* pszAccount = strAccount.c_str(); const char* pszPasswd = strPasswd.c_str(); LogInfo("CWorldLogin::account_login", "client attempt to login;account=%s;passwd=%s;fd=%d\n", pszAccount, pszPasswd, nFd); if(m_fd2accounts.find(nFd) != m_fd2accounts.end()) { //同一个连接上的重复认证,不给错误提示 LogInfo("CWorldLogin::account_login", "login is in progress;account=%s;passwd=%s;fd=%d\n", pszAccount, pszPasswd, nFd); return 0; } map<string, int>::const_iterator iter = m_accounts2fd.find(pszAccount); if(iter != m_accounts2fd.end()) { int fd2 = iter->second; if(nFd == fd2) { //同一个连接上的重复认证,不给错误提示 LogInfo("CWorldLogin::account_login", "login is in progress(2);account=%s;passwd=%s;fd=%d\n", pszAccount, pszPasswd, nFd); return 0; } else { //不同连接上的同一个用户的认证,踢掉老的用户 m_fd2accounts.erase(fd2); m_accounts2fd.erase(pszAccount); GetServer()->CloseFdFromServer(fd2); LogInfo("CWorldLogin::account_login", "multilogin,kick off old;account=%s;passwd=%s;fd=%d;old=%d\n", \ pszAccount, pszPasswd, nFd, fd2); } } m_fd2accounts.insert(make_pair(nFd, pszAccount)); m_accounts2fd.insert(make_pair(pszAccount, nFd)); //printf("account :%s \npassword : %s\n", pszAccount, pszPasswd); /* CMailBox* mb = this->GetServerMailbox(SERVER_DBMGR); if(mb) { mb->RpcCall(GetRpcUtil(), MSGID_DBMGR_SELECT_ACCOUNT, GetMailboxId(), nFd, pszAccount, pszPasswd); } */ CMailBox* mb = GetServer()->GetClientMailbox(nFd); if(mb) { CPluto* u = new CPluto; //printf("login success : %s\n", pszAccount); (*u).Encode(MSGID_CLIENT_LOGIN_RESP) << (uint8_t)ENUM_LOGIN_SUCCESS << EndPluto; mb->PushPluto(u); } //printf("handle in CWorldLogin::AccountLogin()! : end!\n"); return 0; }
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; }