PDA

View Full Version : Window freezes while interrupting MainLoop()



05-20-2003, 11:54 AM
Hi

I'm coding a small Multiplayer-Game with GLUT and Winsock. My Problem is, that i need to interrupt the MainLoop() while recieving the Gamedata from the other Player. Depending on how long the other Player needs to choose his next step, the execution is stopped for a long time.

During this time, my OpenGL Window hangs until the awaited string arrives. I've tried to modify the MainLoop Function with the MainLoopUpdate hack from http://sjbaker.org/steve/software/glut_hack.html , but i got the same problem when the execution of MainLoopUpdate is stopped.

Anyone has an idea how i could fix that? My Game is mostly 2D, and there are no changes in the Display Function while waiting for the other users input.

Thanks in Advance
risp

nexusone
05-20-2003, 01:24 PM
I have been wanting to write a winsock app for openGL, but have not got around to it.

Could you not use the glut timmer loop to check every X ms for a message from the winsocket?



Originally posted by risp:
Hi

I'm coding a small Multiplayer-Game with GLUT and Winsock. My Problem is, that i need to interrupt the MainLoop() while recieving the Gamedata from the other Player. Depending on how long the other Player needs to choose his next step, the execution is stopped for a long time.

During this time, my OpenGL Window hangs until the awaited string arrives. I've tried to modify the MainLoop Function with the MainLoopUpdate hack from http://sjbaker.org/steve/software/glut_hack.html , but i got the same problem when the execution of MainLoopUpdate is stopped.

Anyone has an idea how i could fix that? My Game is mostly 2D, and there are no changes in the Display Function while waiting for the other users input.

Thanks in Advance
risp

errno
05-20-2003, 11:00 PM
you could do this job in idle func.

05-20-2003, 11:20 PM
Originally posted by yoyo:
you could do this job in idle func.

That was my next idea, but i didn't get the time to try it out. But i don't think it works, cause the string is only sent once. When my socket isn't in recv-mode in exactly this time, the string sent is lost. Maybe i can go around this problem by sending the string in a loop, stoping when a confirmation from the client arrives.

errno
05-21-2003, 12:15 AM
the string is lost if the client is not in receive mode ??? what protocol do you use ?

05-21-2003, 12:21 AM
My Project is coded in c! I'm using winsock in TCP-Mode. I tought the socket can't hold the string if it's not waiting for a string to arrive. But thanks for your time, i'll try it when i'm back home.

Deiussum
05-21-2003, 04:25 AM
Use select to see if there is data waiting to be recvd before you actually call recv. recv is a blocking call, so by calling that in the same thread as your Windows messages are in is going to make your window unresponsive. You will NOT lose the string if the string is sent and your receiving app isn't currently blocking on recv. It just goes into a buffer, and will be available next time you call recv. (Unless you are doing something stupid like closing the socket right after you accept a connection.)

Another option would be to create another thread to do all your networking stuff in. Then you wouldn't have to worry about blocking the Windows message thread.

Rob Fletcher
05-22-2003, 03:14 AM
Idle loop will work fine.

I simply poll the socket, if no data continue.

If data, process ...

Of course the socket is established at startup time, and not closed until end of prog.

I have my display func hanging of a glut timer to give me more or less the frame rate I need.

I am able to do real time control of graphics objects this way with continuous data being sent over the net to the socket. So, a string now and then should be no problem.

Init code ... sockfd = socket(AF_INET, SOCK_DGRAM, 0);
my_addr.sin_family = AF_INET; /* host byte order */
my_addr.sin_port = htons(MYPORT); /* short, network byte order */
my_addr.sin_addr.s_addr = INADDR_ANY; /* automatically fill with my IP */
bzero(&(my_addr.sin_zero), 8); /* zero the rest of the struct */
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
addr_len = sizeof(struct sockaddr);
fcntl(sockfd, F_SETFL, O_NONBLOCK);


in the idle func ...


rx=recvfrom(sockfd,buf,MAXBUFLEN,0,(struct sockaddr *)&their_addr,&addr_len);

if(rx > 0)
{
sscanf(buf,"%d %f %f %f",&scenenum,&trigger,&continuousController1,&continuousController2);
if( buf[0] == 'X') cleanup();

... process data
}

The two controllers are attached to the x/y
coords of graphic objects, which appear in the different scenes (which get changed!),
and the trigger item triggers events in the scenes ...

see http://www.york.ac.uk/res/rimm

if you are in any way interested in what it was all about!

r

05-22-2003, 07:16 AM
Thank you all for your help! I'll try to fix my Program with your suggestions.

I hope it works this way