WSAEventSelect not working? Or am I using it incorrectly?

Started by
3 comments, last by Tribad 11 years, 7 months ago
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;
}
Advertisement
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.
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 :(
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 };
WSACreateEvent()
where is it?

This topic is closed to new replies.

Advertisement