Sign in to follow this  
GeekPlusPlus

Tricks and stuff for Non-Blocking UDP?

Recommended Posts

GeekPlusPlus    124
Hello, This is all for C++ on a windows box. I was just wondering if there were any standard rules, tricks, checks I need to do when using a non blocking UDP setup for a rather lage multi player game. My main processing function is as follows: I know you're missing a lot of the classes, but there's a lot too it to post. This is the heart of it all, if you have any suggestions I'm listening. =)
void CConnectionManager::Process(CPacketHandler* pPacketHandler)
{
	FD_ZERO(&m_read);
	FD_SET(m_sock, &m_read);

	const timeval timeWait = {0, 0};

	while(select(1, &m_read, 0, 0, &timeWait))
	{
		FD_ZERO(&m_read);
		FD_SET(m_sock, &m_read);
		
		int iFromLength = sizeof(m_pPacketBuffer->GetFromAddr());
		int iLength = recvfrom(m_sock, ((char*)m_pPacketBuffer)+4, MAX_PACKET_SIZE, 0, (sockaddr*)m_pPacketBuffer->GetpFromAddr(), &iFromLength);
			
		m_pPacketBuffer->SetLength(iLength - sizeof(CPacket));

		if(iLength >= sizeof(CPacket))
			pPacketHandler->HandlePacket(m_pPacketBuffer);
	}

	return;
}
Thanks,

Share this post


Link to post
Share on other sites
cbenoi1    484
> if you have any suggestions

You don't really need the select() here because you can get the information from recvfrom()'s returning value along with WSAGetLastError(). 0 for no data, SOCKET_ERROR for an error.

> when using a non blocking UDP setup for a rather
> large multi player game

Lemme guess.... a MMORPG? {large grin}

Handling a very large number of users can only be achieved using IOCP on Windows NT/2K/XP-class machines. Here is some code to start you up on this path:

Hope this helps.

http://pages.infinit.net/cbenoi1/iocp_udp.zip

-cb

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this