예제 #1
0
int32 CClientMgr::OnNewClient()
{
	if ((int32)m_ClientList.size() >= m_MaxClientNum)
		return 0;

	int32 id = 0;
	lxnet::Socketer *sock = NULL;
	CClient *newclient = NULL;
	sock = m_Listen->Accept();
	if (!sock)
		return 0;

	id = idmgr_allocid(m_IDPool);
	if (id <= 0)
	{
		log_error("为新Client分配ID失败!, id:%d", id);
		goto do_error;
	}

	newclient = client_create();
	if (!newclient)
	{
		log_error("创建Client失败!");
		goto do_error;
	}

	sock->SetRecvLimit(m_RecvDataLimit);
	sock->SetSendLimit(m_SendDataLimit);
	//sock->UseCompress();
	//sock->UseEncrypt();
	//sock->UseDecrypt();
	//sock->UseTGW();

	newclient->SetClientID(id);
	newclient->SetCon(sock);
	newclient->SetConnectTime(g_currenttime);
	newclient->SetPingTime(g_currenttime);

	m_ClientList.push_back(newclient);
	newclient->SetInNormal();

	char ip[64];
	sock->GetIP(ip, sizeof(ip) - 1);
	ip[sizeof(ip) - 1] = 0;

	//ClientConnectLog("新Client连接, ip:%s, ID:%d, 当前Client总数量:%d", ip, id, (int32)m_ClientList.size());

	assert(NULL == m_ClientSet[id]);
	m_ClientSet[id] = newclient;

	return id;

do_error:
	if (id > 0)
	{
		if (!idmgr_freeid(m_IDPool, id))
			log_error("释放ID失败!, ID:%d", id);
	}
	lxnet::Socketer::Release(sock);
	return 0;
}