Jump to content

  • Log In with Google      Sign In   
  • Create Account


WSAEventSelect not working? Or am I using it incorrectly?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 hupsilardee   Members   -  Reputation: 485

Like
0Likes
Like

Posted 01 September 2012 - 06:19 AM

Hello again. I have designed a pretty typical network IO model for my game based on WSAEventSelect, but it is not working. Some packets get sent (TCP) but then the program locks up at the WSAWaitForMultipleEvents function, which is in a separate thread.

Perhaps some pseudo-code will help. My networking looks like this

Vector3 playerPos;
Vector3 enemyPos;

SOCKET GameSocket;
EVENT exitThread;
WSAEVENT networkEvent;

// main thread
int main()
{
	 // Initialise GameSocket. If server, use listen() / GameSocket = accept()
	 // If client, use connect(GameSocket)

	 WSAEventSelect(GameSocket, networkEvent, FD_READ | FD_WRITE)
	 beginthreadex(network)

	 while (message pump)
	 {
	 	 render(playerPos, enemyPos);

	 	 EnterCriticalSection(positions);
	 	 // Modify playerPos due to keyboard input etc
	 	 LeaveCriticalSection(positions);

	 	 if (escape pressed)
	 	 	 break;
	 }

	 SetEvent(exitThread);
	 WaitForSingleEvent(network thread);
}

// network thread
int network()
{
	 Vector3 playerPos2;
	 Vector3 enemyPos2;

	 while (true)
	 {
	 	 EnterCriticalSection(positions);
	 	 playerPos2 = playerPos;
	 	 LeaveCriticalSection(positions);

	 	 WSAWaitForMultipleEvents()
	 	 WSAEnumNetworkEvents()
	 	 if (network event read)
	 	 {
	 	 	 recv(enemyPos2);
	 	 }
	 	 if (network event write)
	 	 {
	 	 	 send (playerPos2);
	 	 }

	 	 EnterCriticalSection(positions);
	 	 enemyPos = enemyPos2;
	 	 LeaveCriticalSection(positions);

	 	 if (WaitForSingleEvent(exitThread, timeout = 0))
	 	 	 break;
	 }
	 return 0;
}

Edited by hupsilardee, 01 September 2012 - 08:40 AM.


Sponsor:

#2 Tribad   Members   -  Reputation: 452

Like
0Likes
Like

Posted 01 September 2012 - 07:48 AM

You should have a look at the event handling description. I think I read there something about the tiny thing, that the events are thread specific.
But this is about 10 years ago. So. Have a look for it.

#3 hupsilardee   Members   -  Reputation: 485

Like
0Likes
Like

Posted 01 September 2012 - 08:27 AM

Moved the SocketEvent into the worker thread, so it is created at thread start, closed at thread end, and never used by the main thread.
Aaaaand... it still doesn't work :(

#4 hplus0603   Moderators   -  Reputation: 4533

Like
0Likes
Like

Posted 01 September 2012 - 04:15 PM

Events are not thread specific, BUT WSAEventSelect() has a bunch of caveats in how you can use it. Specifically, your code may end up deadlocking because of race conditions if I remember right.
WSAEventSelect(), and WSAAsyncSelect(), are relics from the Windows 3.1 / Windows 95 days. You do not want to use them for any new code. If you want portable code, use regular select(). If you want high-performance, non-blocking Windows code, use OVERLAPPED I/O with I/O completion ports.
Or, if you want multi-threaded, non-blocking code, that's portable, use boost::asio.

enum Bool { True, False, FileNotFound };

#5 Tribad   Members   -  Reputation: 452

Like
0Likes
Like

Posted 02 September 2012 - 01:52 AM

WSACreateEvent()
where is it?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS