Skip to content

Commit

Permalink
增加支持js返回值字典类型
Browse files Browse the repository at this point in the history
  • Loading branch information
fanchy committed Feb 20, 2018
1 parent b7ec5a3 commit 51b416f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 45 deletions.
44 changes: 22 additions & 22 deletions fflib/server/ffworker.cpp
Expand Up @@ -6,7 +6,7 @@
using namespace ff;
using namespace std;

#define FFWORKER "FFWORKER"
#define FFWORKER_LOG "FFWORKER"
WorkerInitFileInfo FFWorker::gSetupFunc[100];
WorkerFunc FFWorker::gExitFunc[100];
int FFWorker::regSetupFunc(WorkerFunc f, const char* file, int line, int priority){
Expand Down Expand Up @@ -41,7 +41,7 @@ static bool callSetupFunc(){
if (FFWorker::gSetupFunc[i].func == NULL)
continue;
if ((*(FFWorker::gSetupFunc[i].func))() == false){
LOGERROR((FFWORKER, "FFWorker::open failed when exe %s[%d]",
LOGERROR((FFWORKER_LOG, "FFWorker::open failed when exe %s[%d]",
FFWorker::gSetupFunc[i].strFile, FFWorker::gSetupFunc[i].nLine));
return false;
}
Expand Down Expand Up @@ -73,7 +73,7 @@ FFWorker* FFWorker::gSingletonWorker = NULL;
//int FFWorker::open(ArgHelper& arg_helper, string worker_name)
int FFWorker::open(const string& brokercfg, int worker_index)
{
LOGTRACE((FFWORKER, "FFWorker::open begin"));
LOGTRACE((FFWORKER_LOG, "FFWorker::open begin"));
FFWorker::gSingletonWorker = this;

m_nWorkerIndex = worker_index;
Expand All @@ -90,7 +90,7 @@ int FFWorker::open(const string& brokercfg, int worker_index)

if (m_ffrpc->open(brokercfg))
{
LOGERROR((FFWORKER, "FFWorker::open failed check brokercfg %s", brokercfg));
LOGERROR((FFWORKER_LOG, "FFWorker::open failed check brokercfg %s", brokercfg));
return -1;
}
string host = m_ffrpc->get_host();
Expand All @@ -102,7 +102,7 @@ int FFWorker::open(const string& brokercfg, int worker_index)
m_shared_mem_mgr.init_worker(port, worker_index, &(m_ffrpc->get_tq()));
Singleton<FFWorkerMgr>::instance().add(m_logic_name, this);

LOGTRACE((FFWORKER, "FFWorker::open end ok"));
LOGTRACE((FFWORKER_LOG, "FFWorker::open end ok"));

SCRIPT_CACHE.init();
return 0;
Expand All @@ -118,9 +118,9 @@ int FFWorker::close()
return 0;
}
bool FFWorker::initModule(){
LOGINFO((FFWORKER, "FFWorker::open initModule begin ..."));
LOGINFO((FFWORKER_LOG, "FFWorker::open initModule begin ..."));
if (false == callSetupFunc()){
LOGERROR((FFWORKER, "FFWorker::open initModule failed when callSetupFunc"));
LOGERROR((FFWORKER_LOG, "FFWorker::open initModule failed when callSetupFunc"));
return false;
}
return true;
Expand All @@ -131,14 +131,14 @@ bool FFWorker::cleanupModule(){
}
catch(exception& e_)
{
LOGERROR((FFWORKER, "cleanupModule failed er=<%s>", e_.what()));
LOGERROR((FFWORKER_LOG, "cleanupModule failed er=<%s>", e_.what()));
}
return true;
}
//! 转发client消息
int FFWorker::processSessionReq(ffreq_t<RouteLogicMsg_t::in_t, RouteLogicMsg_t::out_t>& req_)
{
LOGTRACE((FFWORKER, "FFWorker::processSessionReq begin cmd[%u]", req_.msg.cmd));
LOGTRACE((FFWORKER_LOG, "FFWorker::processSessionReq begin cmd[%u]", req_.msg.cmd));
std::map<userid_t, WorkerClient>::iterator it = m_worker_client.find(req_.msg.session_id);
if (it == m_worker_client.end()){
WorkerClient& worker_client = m_worker_client[req_.msg.session_id];
Expand Down Expand Up @@ -170,7 +170,7 @@ int FFWorker::processSessionReq(ffreq_t<RouteLogicMsg_t::in_t, RouteLogicMsg_t::
req_.response(out);
}
getSharedMem().writeLockEnd();
LOGTRACE((FFWORKER, "FFWorker::processSessionReq end ok"));
LOGTRACE((FFWORKER_LOG, "FFWorker::processSessionReq end ok"));
return 0;
}
int FFWorker::onSessionReq(userid_t session_id_, uint16_t cmd_, const std::string& data_)
Expand All @@ -186,15 +186,15 @@ int FFWorker::onSessionReq(userid_t session_id_, uint16_t cmd_, const std::strin
//! 处理client 下线
int FFWorker::processSessionOffline(ffreq_t<SessionOffline::in_t, SessionOffline::out_t>& req_)
{
LOGTRACE((FFWORKER, "FFWorker::processSessionOffline begin"));
LOGTRACE((FFWORKER_LOG, "FFWorker::processSessionOffline begin"));

onSessionOffline(req_.msg.session_id);

SessionOffline::out_t out;
req_.response(out);
m_worker_client.erase(req_.msg.session_id);
getSharedMem().writeLockEnd();
LOGTRACE((FFWORKER, "FFWorker::processSessionOffline end ok"));
LOGTRACE((FFWORKER_LOG, "FFWorker::processSessionOffline end ok"));
return 0;
}
int FFWorker::onSessionOffline(userid_t session_id)
Expand All @@ -209,7 +209,7 @@ int FFWorker::onSessionOffline(userid_t session_id)
//! 处理client 跳转
int FFWorker::processSessionEnter(ffreq_t<SessionEnterWorker::in_t, SessionEnterWorker::out_t>& req_)
{
LOGTRACE((FFWORKER, "FFWorker::processSessionEnter begin gate[%s]", req_.msg.from_gate));
LOGTRACE((FFWORKER_LOG, "FFWorker::processSessionEnter begin gate[%s]", req_.msg.from_gate));

WorkerClient& worker_client = m_worker_client[req_.msg.session_id];
worker_client.from_gate = req_.msg.from_gate;
Expand All @@ -219,7 +219,7 @@ int FFWorker::processSessionEnter(ffreq_t<SessionEnterWorker::in_t, SessionEnter
onSessionEnter(req_.msg.session_id, req_.msg.extra_data);

getSharedMem().writeLockEnd();
LOGTRACE((FFWORKER, "FFWorker::processSessionEnter end ok"));
LOGTRACE((FFWORKER_LOG, "FFWorker::processSessionEnter end ok"));

return 0;
}
Expand All @@ -234,14 +234,14 @@ int FFWorker::FFWorker::onSessionEnter(userid_t session_id, const std::string& e
//! scene 之间的互调用
int FFWorker::processWorkerCall(ffreq_t<WorkerCallMsgt::in_t, WorkerCallMsgt::out_t>& req_)
{
LOGTRACE((FFWORKER, "FFWorker::processWorkerCall begin cmd[%u]", req_.msg.cmd));
LOGTRACE((FFWORKER_LOG, "FFWorker::processWorkerCall begin cmd[%u]", req_.msg.cmd));

WorkerCallMsgt::out_t out;
out.body = onWorkerCall(req_.msg.cmd, req_.msg.body);
req_.response(out);

getSharedMem().writeLockEnd();
LOGTRACE((FFWORKER, "FFWorker::processWorkerCall end ok"));
LOGTRACE((FFWORKER_LOG, "FFWorker::processWorkerCall end ok"));
return 0;
}
std::string FFWorker::onWorkerCall(uint16_t cmd, const string& body)
Expand Down Expand Up @@ -327,40 +327,40 @@ int FFWorker::sessionSendMsg(const string& gate_name, const userid_t& session_id
{
if (gate_name.empty())
return -1;
LOGTRACE((FFWORKER, "FFWorker::send_msg_session begin session_id_<%ld>", session_id_));
LOGTRACE((FFWORKER_LOG, "FFWorker::send_msg_session begin session_id_<%ld>", session_id_));

GateRouteMsgToSession::in_t msg;
msg.session_id.push_back(session_id_);
msg.cmd = cmd_;
msg.body = data_;
m_ffrpc->call(gate_name, msg);
LOGTRACE((FFWORKER, "FFWorker::send_msg_session end ok gate[%s]", gate_name));
LOGTRACE((FFWORKER_LOG, "FFWorker::send_msg_session end ok gate[%s]", gate_name));
return 0;
}
int FFWorker::sessionMulticastMsg(const string& gate_name, const vector<userid_t>& session_id_, uint16_t cmd_, const string& data_)
{
LOGTRACE((FFWORKER, "FFWorker::multicast_msg_session begin session_id_<%u>", session_id_.size()));
LOGTRACE((FFWORKER_LOG, "FFWorker::multicast_msg_session begin session_id_<%u>", session_id_.size()));

GateRouteMsgToSession::in_t msg;
msg.session_id = session_id_;
msg.cmd = cmd_;
msg.body = data_;
m_ffrpc->call(gate_name, msg);
LOGTRACE((FFWORKER, "FFWorker::multicast_msg_session end ok gate[%s]", gate_name));
LOGTRACE((FFWORKER_LOG, "FFWorker::multicast_msg_session end ok gate[%s]", gate_name));
return 0;
}
int FFWorker::sessionKFSendMsg(const string& group_name, const string& gate_name,
const userid_t& session_id_,
uint16_t cmd_, const string& data_)
{
LOGTRACE((FFWORKER, "FFWorker::send_msg_session begin session_id_<%ld>", session_id_));
LOGTRACE((FFWORKER_LOG, "FFWorker::send_msg_session begin session_id_<%ld>", session_id_));

GateRouteMsgToSession::in_t msg;
msg.session_id.push_back(session_id_);
msg.cmd = cmd_;
msg.body = data_;
m_ffrpc->call(group_name, gate_name, msg);
LOGTRACE((FFWORKER, "FFWorker::send_msg_session end ok gate[%s]", gate_name));
LOGTRACE((FFWORKER_LOG, "FFWorker::send_msg_session end ok gate[%s]", gate_name));
return 0;
}
//! 广播 整个gate
Expand Down
4 changes: 2 additions & 2 deletions fflib/server/ffworker.h
Expand Up @@ -252,7 +252,7 @@ class FFWorkerMgr
private:
std::map<std::string, FFWorker*> m_all_worker;
};
#define FFWORKER_SINGLETON (*FFWorker::gSingletonWorker)
#define FFWORKER (*FFWorker::gSingletonWorker)

class FFWorker::session_enter_arg: public FFSlot::CallBackArg
{
Expand All @@ -272,7 +272,7 @@ class FFWorker::session_enter_arg: public FFSlot::CallBackArg
}
std::string session_ip;
std::string gate_name;
userid_t session_id;//! 包含用户id
userid_t session_id;//! 包含用户id
std::string from_worker;//! 从哪个scene跳转过来,若是第一次上线,from_worker为空
std::string to_worker;//! 跳到哪个scene上面去,若是下线,to_worker为空
std::string extra_data;//! 附带数据
Expand Down
2 changes: 1 addition & 1 deletion main.js
Expand Up @@ -71,7 +71,7 @@ function onWorkerCall(cmd, data){

function testScriptCall(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9){
h2ext.print('testScriptCall', arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
return 1122334;
return {'a': 'b', 'c': 'd', 1 : 'abc'};
}
function testCode(){
h2ext.callFunc("Cache.set", "m.n[10]", "mmm1");
Expand Down
5 changes: 4 additions & 1 deletion src/common/cmdmgr.cpp
Expand Up @@ -8,7 +8,10 @@ static void handleSessionCmd(SessionReqEvent& e){
CmdHandlerPtr cmdHandler = CMD_MGR.getCmdHandler(e.cmd);
if (cmdHandler){
e.isDone = true;
cmdHandler->handleCmd(ENTITY_MGR.getBySession(e.session_id), e.cmd, e.data);
EntityPtr entity = ENTITY_MGR.getEntityBySession(e.session_id);
if (entity){
cmdHandler->handleCmd(entity, e.cmd, e.data);
}
}
}
static bool initEnvir(){
Expand Down
22 changes: 15 additions & 7 deletions src/common/entity.cpp
Expand Up @@ -11,6 +11,7 @@ map<long, WeakPtr<Entity> > Entity::EntityPtr2Ref;
SharedPtr<Entity> Entity::genEntity(int ntype, userid_t id, userid_t sid){
SharedPtr<Entity> ret = new Entity(ntype, id, sid);
Entity::EntityPtr2Ref[long(ret.get())] = ret;
ENTITY_MGR.add(ret);
return ret;
}
SharedPtr<Entity> Entity::toEntity(long p){
Expand All @@ -28,7 +29,7 @@ Entity::~Entity(){
bool Entity::sendMsg(uint16_t cmd, const std::string& msg)
{
if (m_sessionID){
FFWORKER_SINGLETON.sessionSendMsg(m_sessionID, cmd, msg);
FFWORKER.sessionSendMsg(m_sessionID, cmd, msg);
return true;
}
return false;
Expand Down Expand Up @@ -60,20 +61,27 @@ void Entity::clearField(){
m_fields.clear();
}

void EntityMgr::add(EntityPtr& p){
m_all_entity[p->getType()][p->getUid()] = p;
void EntityMgr::add(EntityPtr p){
m_allEntity[p->getType()][p->getUid()] = p;
if (p->getSession()){
m_session2entity[p->getSession()] = p;
}
}
bool EntityMgr::del(int ntype, userid_t id){
std::map<userid_t, EntityPtr>& allEntity = m_all_entity[ntype];
std::map<userid_t, EntityPtr>& allEntity = m_allEntity[ntype];
std::map<userid_t, EntityPtr>::iterator it = allEntity.find(id);
if (it != allEntity.end()){
EntityPtr p = it->second;
if (p->getSession()){
m_session2entity.erase(p->getSession());
}
allEntity.erase(it);
return true;
}
return false;
}
EntityPtr EntityMgr::get(int ntype, userid_t id){
std::map<userid_t, EntityPtr>& allEntity = m_all_entity[ntype];
std::map<userid_t, EntityPtr>& allEntity = m_allEntity[ntype];
std::map<userid_t, EntityPtr>::iterator it = allEntity.find(id);
if (it != allEntity.end()){
return it->second;
Expand All @@ -98,9 +106,9 @@ static bool initEntityEnvir(){
/*example code
printf("initEntityEnvir....\n");
ScriptArgObjPtr ret = SCRIPT_UTIL.callScript<ScriptArgObjPtr>("testScriptCall");
ret->dump();
ScriptArgs varScriptArgs;
SCRIPT_UTIL.callScriptRaw<void>("testScriptCall", varScriptArgs);
int arg1 = 0;
string arg2 = "sss";
double arg3 = 3.14;
Expand Down
20 changes: 13 additions & 7 deletions src/common/entity.h
Expand Up @@ -96,7 +96,7 @@ class Entity
};
typedef SharedPtr<Entity> EntityPtr;
typedef WeakPtr<Entity> EntityRef;
#define NEW_ENTITY(ntype, id) Entity::genEntity(ntype, id)
#define NEW_ENTITY(ntype, id, sessionId) Entity::genEntity(ntype, id, sessionId)
#define TO_ENTITY(ptr) Entity::toEntity(long(ptr))
#define UID_TO_ENTITY(ntype, id) Singleton<EntityMgr>::instance().get(ntype, id)

Expand Down Expand Up @@ -149,25 +149,31 @@ class EntityMgr{
EntityMgr(){}
virtual ~EntityMgr(){}

void add(EntityPtr& p);
void add(EntityPtr p);
bool del(int ntype, userid_t id);
EntityPtr get(int ntype, userid_t id);
size_t size(int ntype) { return m_all_entity[ntype].size(); }
size_t size(int ntype) { return m_allEntity[ntype].size(); }

template <typename T>
void foreach(int ntype, T f){
std::map<userid_t, EntityPtr>& allEntity = m_all_entity[ntype];
std::map<userid_t, EntityPtr>& allEntity = m_allEntity[ntype];
std::map<userid_t, EntityPtr>::iterator it = allEntity.begin();
for (; it != allEntity.end(); ++it){
f(it->second);
}
}

EntityPtr getBySession(userid_t session_id){
return NULL;//!todo
EntityPtr getEntityBySession(userid_t sessionId){
std::map<userid_t, EntityPtr>::iterator it = m_session2entity.find(sessionId);
if (it != m_session2entity.end()){
return it->second;
}
return NULL;
}

protected:
std::map<int/*entity type*/, std::map<userid_t, EntityPtr> > m_all_entity;
std::map<int/*entity type*/, std::map<userid_t, EntityPtr> > m_allEntity;
std::map<userid_t, EntityPtr> m_session2entity;
};
#define ENTITY_MGR Singleton<EntityMgr>::instance()

Expand Down
2 changes: 1 addition & 1 deletion src/npc/npc.cpp
Expand Up @@ -15,7 +15,7 @@ bool NPCMgr::init(){
}
EntityPtr NPCMgr::addNPC(NPCConfigPtr cfg){
static int nId = 0;
EntityPtr eNPC = NEW_ENTITY(ENTITY_NPC, ++nId);
EntityPtr eNPC = NEW_ENTITY(ENTITY_NPC, ++nId, 0);
return eNPC;
}

Expand Down
3 changes: 0 additions & 3 deletions src/task/task.cpp
Expand Up @@ -373,9 +373,6 @@ static bool initEnvir(){
SCRIPT_UTIL.reg("Task.getTaskCfg", TaskScriptFunctor::getTaskCfg);
SCRIPT_UTIL.reg("Task.addTaskCfg", TaskScriptFunctor::addTaskCfg);

//EntityPtr entity = NEW_ENTITY(1, 1);
//string arg = "SoNice";
//SCRIPT_UTIL.callScript<void>(string("foo.sayhi"), arg);
return true;
}
WORKER_AT_SETUP(initEnvir);
Expand Down
13 changes: 12 additions & 1 deletion workerjs/ffworker_js.cpp
Expand Up @@ -844,7 +844,18 @@ static ScriptArgObjPtr toScriptArg(Local<Value>& v){
}
else if (v->IsObject()){
ret->toDict();
//Local<Object> pa = v.As<Object>();
Local<Object> dictObj = v.As<Object>();
Local<Array> keyList = dictObj->GetOwnPropertyNames();
uint32_t size = keyList->Length();

for (uint32_t i = 0; i < size; ++i){
Local<Value> keyv = keyList->Get(i);
ScriptArgObjPtr ekey = toScriptArg(keyv);
string strKey = ekey->getString();

Local<Value> elemVal = PERSISTENT2LOCAL(dictObj->Get(NewStrValue(strKey.c_str(), strKey.size())));
ret->dictVal[strKey] = toScriptArg(elemVal);
}
}
return ret;
}
Expand Down

0 comments on commit 51b416f

Please sign in to comment.