Chinaunix
标题:
能帮我简化下这个破函数吗?
[打印本页]
作者:
jd808
时间:
2016-01-21 20:40
标题:
能帮我简化下这个破函数吗?
int BaseTEngine::CallConnList(std::tr1::function<int(Conn *,Conn *,const char *,int,void*)> f,Conn * ActConn,const char *command,int cmdlen,void* n)
{
list<Conn *> pconlist;
pconlist.clear();
Conn *pNext= NULL, *pcon= NULL;
ConnList *m_conlist=NULL;
ConnList* m_UserList = g_pServer->GetUserList();
for (int i=0; i<g_pServer->GetUserListCount(); i++) {
m_conlist=&m_UserList[i];
pthread_rwlock_rdlock(&m_conlist->ConnListLock);
pcon = m_conlist->GetHead()->GetNext();
while (pcon != NULL) {
pNext = pcon->GetNext();
if(pcon->is_login==1&& pcon->fd_type==1&&pcon->is_down==0)
pconlist.push_back(pcon);
pcon = pNext;
}
pthread_rwlock_unlock(&m_conlist->ConnListLock);
}
Conn *xpconn = NULL;
list<Conn *>::iterator itor;
itor=pconlist.begin();
while(itor!=pconlist.end())
{
xpconn=*itor++;
if(xpconn!=NULL)
f(xpconn,ActConn,command,cmdlen,n);
}
return 0;
}
int TServer::send_client(Conn *pcon,Conn *Build_conn,const char *command,int cmdlen,void *p_id)
{
int mip=*(int *)p_id;
if(Build_conn->mip==mip)
m_TimeThreadInfo->t_Engine->Base_Client_Lock(pcon,command,cmdlen, g_pConf->STRZIP);
return 0;
}
CallConnList(bind(&TServer::send_client, this, tr1::placeholders::_1, tr1::placeholders::_2, tr1::placeholders::_3, tr1::placeholders::_4, tr1::placeholders::_5),pcon,command,DataLen,(void*)&p_id);
复制代码
每次调用都要写这么长
CallConnList(bind(&TServer::send_client, this, tr1::placeholders::_1, tr1::placeholders::_2, tr1::placeholders::_3, tr1::placeholders::_4, tr1::placeholders::_5),pcon,command,DataLen,(void*)&p_id);
复制代码
不方便啊,能不能吧bind移到CallConnList函数里呀
作者:
windoze
时间:
2016-01-21 21:38
你就不能自己在外面包一层?
作者:
jd808
时间:
2016-01-22 15:11
windoze 发表于 2016-01-21 21:38
你就不能自己在外面包一层?
TServer::send_client这函数我不会传,我吧他定义成
auto *func=&TServer::send_client;
CallConnList(func, pcon,command,DataLen,(void*)&p_id);
int BaseTEngine::CallConnList(auto *func,Conn * ActConn,const char *command,int cmdlen,void* n)
{
auto f=bind(func, tr1::placeholders::_1, tr1::placeholders::_2, tr1::placeholders::_3, tr1::placeholders::_4);
return f(ActConn,command,cmdlen,n);
}
复制代码
这样,似乎行不通
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2