Jump to content
  • Advertisement
Sign in to follow this  
GeekPlusPlus

Tricks and stuff for Non-Blocking UDP?

This topic is 5099 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
> 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!