示例#1
0
void CBaseObj::SendRefMsg(Msg &pMsg)
{
	assert(m_GateIDMax < gateinfo_count);
	for (int i = m_GateIDMin; i <= m_GateIDMax; ++i)
	{
		gateinfo[i]->Reset();
	}

	static bool bNeeSendMsg = false;
	static msgtail tail;
	std::unordered_map<uint32, CBaseObj *> *playerlist = GetAoiList();
	std::unordered_map<uint32, CBaseObj *>::iterator iter = playerlist->begin();
	for (; iter != playerlist->end(); ++iter)
	{
		if (iter->second->IsPlayer())
		{
			CPlayer * p = (CPlayer *)iter->second;
			if (FuncUti::isValidCret(p))
			{
				int32 gateid = p->GetGateID();
				if (gateid >= 0 && gateid < gateinfo_count)
				{
					if (gateid > m_GateIDMax)
					{
						m_GateIDMax = gateid;
					}
					else if (gateid < m_GateIDMin)
					{
						m_GateIDMin = gateid;
					}

					if (gateinfo[gateid]->gate == nullptr)
					{
						gateinfo[gateid]->gate = p->GetGateInfo();
					}
					gateinfo[gateid]->nClientId[gateinfo[gateid]->nCount] = p->GetClientID();
					gateinfo[gateid]->nCount += 1;
				}
			}
		}
	}

	static bool bIsPlayer = false;
	bIsPlayer = IsPlayer();
	if (bNeeSendMsg || bIsPlayer)
	{
		if (bIsPlayer)
		{
			CPlayer *p = (CPlayer *)this;
			int32 gateid = p->GetGateID();
			if (gateid >= 0 && gateid < gateinfo_count)
			{
				if (gateid > m_GateIDMax)
				{
					m_GateIDMax = gateid;
				}
				else if (gateid < m_GateIDMin)
				{
					m_GateIDMin = gateid;
				}

				if (gateinfo[gateid]->gate == nullptr)
				{
					gateinfo[gateid]->gate = p->GetGateInfo();
				}

				gateinfo[gateid]->nClientId[gateinfo[gateid]->nCount] = p->GetClientID();
				gateinfo[gateid]->nCount += 1;
			}
		}

		bNeeSendMsg = false;
		MessagePack pkmain;
		pkmain.PushInt32(pMsg.GetLength());
		pkmain.PushBlock(&pMsg, pMsg.GetLength());
		int32 pkmainlen = pkmain.GetLength();

		for (int i = m_GateIDMin; i < m_GateIDMax; ++i)
		{
			if (gateinfo[i]->nCount > 0)
			{
				tail.id = 0;
				for (int j = 0; j < gateinfo[i]->nCount; ++j)
				{
					pkmain.PushInt32(gateinfo[i]->nClientId[j]);
					--tail.id;
				}
				GameGatewayMgr.SendMsg(gateinfo[i]->gate, pkmain, &tail, sizeof(tail));
				pkmain.SetLength(pkmainlen);
				pkmain.m_index = pkmainlen - (int32)sizeof(Msg);
			}
		}
	}
}
示例#2
0
文件: listen.cpp 项目: Manhelp/lxnet
int main()
{
	if(!lxnet::net_init(512, 1, 1024*32, 100, 1, 4, 1))
	{
		printf("init network error!\n");
		system("pause");
		return 0;
	}

	lxnet::Listener *list = lxnet::Listener::Create();
	if (!list->Listen(30012, 10))
	{
		printf("listen error\n");
	}
	if (list)
	{
		MessagePack sendpack;
		MessagePack *recvpack;
		char neirong[1024*30]="a1234567";
		//char recvneirong[32*1024];
		int size = sizeof(neirong);
		sendpack.PushBlock(neirong, size);


		printf("listen succeed!\n");
		lxnet::Socketer *newclient = NULL;
		while (1)
		{
#ifdef WIN32
			Sleep(100);
#else
			usleep(100000);
#endif
			lxnet::net_run();
			if (!list->CanAccept())
				continue;
			if (!(newclient = list->Accept()))
				continue;

			printf("accept succeed!\n");
			//newclient->UseCompress();
			//newclient->UseUncompress();
			//newclient->UseDecrypt();
			//newclient->UseEncrypt();
			newclient->SetSendLimit(-1);
			//newclient->SetRecvLimit(16*1024);

			newclient->CheckRecv();
			while(1)
			{
				recvpack = (MessagePack *)newclient->GetMsg();
				if (recvpack)
				{
					//recvpack->Begin();
					//recvpack->GetBlock(recvneirong, size);
					//if (memcmp(recvneirong, neirong, size) != 0)
					//{
					//	printf("data error!\n");
					//	system("pause");
					//}
					newclient->SendMsg(&sendpack);
					newclient->CheckSend();
				}
				else
#ifdef WIN32
					Sleep(0);
#else
					sleep(0);
#endif

				lxnet::net_run();
				if (newclient->IsClose())
				{
					system("pause");
					lxnet::Socketer::Release(newclient);
					goto s_exit;
				}
			}
		}
	}
	else
	{
		printf("listen failed!\n");
	}

s_exit:
	system("pause");
	printf("begin ... release..\n");
#ifdef WIN32
	Sleep(1000);
#else
	usleep(1000000);
#endif
	lxnet::Listener::Release(list);
	lxnet::net_release();
	system("pause");
	return 0;
}