Esempio n. 1
0
void Worker::closeClient(Connection *client)
{
	ClientData *pData = (ClientData*)client->getData();
	std::string sid = pData->session.getId();
	LOG("close client, fd=%d, sid=%s", client->getSocket().getFd(), sid.c_str());
	
	//router通知
	sendToRouter(ROUTER_MSG_CLOSE, SID_LENGTH, pData->session.getId(), 0, NULL);
	
	//后端通知
	Config *pConfig = Config::getInstance();
	if(pConfig->bEventClose){
		Backend *backend = new Backend(pEventLoop, NULL, NULL);
		backend->copyParam(pData->params);
		backend->addParam("EVENT", sizeof("EVENT") - 1, "2", 1);
		backend->setCloseHandler(EV_CB(this, Worker::onBackendClose));
		backend->setResponseHandler(EV_CB(this, Worker::onBackendResponse));
		if(!backend->run()){
			delete backend;
		}
	}
	
	//释放后端请求
	for(BackendList::const_iterator it = pData->backends.begin(); it != pData->backends.end(); ++it){
		Backend *backend = it->first;
		backend->setClient(NULL);
		backend->shutdown();
	}
	
	//取消频道订阅
	for(ChannelSet::const_iterator it2 = pData->channels.begin(); it2 != pData->channels.end(); ++it2){
		const std::string &chname = it2->first;
		
		ChannelList::iterator cit = arrChannels.find(chname);
		if(cit != arrChannels.end()){
			cit->second.erase(client);
			if(cit->second.empty()){
				sendToRouter(ROUTER_MSG_CH_UNSUB, chname.size(), chname.c_str(), 0, NULL);
				arrChannels.erase(cit);
			}
		}
	}
	
	//计数更新
	pMaster->delClient(nId);
	
	//客户端列表更新
	arrClients.erase(sid);
	
	//lua更新
	if(pMaster->pScript && pMaster->pScript->hasCloseProc()){
		pMaster->pScript->procClose(client);
	}
	
	delete pData;
	delete client;
}