WaitNetworkServerEvent() & WaitNetworkClientEvent()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

WaitNetworkServerEvent() & WaitNetworkClientEvent()

Post by Poshu »

Hello Fred.
I would like to see:
WaitNetworkServerEvent([Timeout]) : Wait until an event occurs. It's the same function as NetworkServerEvent() but locks the program execution, which is very important in a multitasking environment.

WaitNetworkClientEvent([Timeout]) : Wait until an event blah blah blah...

The first one is especially important to me.

Thx ^^;
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Hmm, interesting idea but not that nice actualy,
you really should use WaitWindowEvent() or similar instead to control the loop,
and use a Delay(1) in the loop if nothing is happening.

The only advantage I can see of your suggestion is if the program has no interface at all.
But even if it was a service you really do not want it be stuck waiting,
as you would be unable to "stop" the service quickly and more.

Even a simple

Repeat
Delay(1)
Forever

would be better as you would be able to handle various events and situation while checking if network events happen.

Delay(1) hardly uses any system resources at all so don't worry.

You see it is important that a program is not stalled (waiting for something) as the system could decide to kill your program because
it has been stalled (not doing anything), that obviously depends on the way a WaitNetworkServerEvent() where to be implemented though.

This is just my view on this, I'm not too keen on multiple ways to "stall" a app.
WaitWindowEvent() would be usable in 99% of cases.


Hmm, I just checked something and this is a amusing discovery.
WaitWindowEvent() actualy works in a program even if there is no actual window at all.

Code: Select all

Repeat
 event=WaitWindowEvent() 
 Debug event
 Delay(1)
Until event=#PB_Event_CloseWindow

Debug "closing"
Good luck sending a close window message to the windowless program though hehe.

Fred (or anyone else that know), would such a "program" react correctly
to stuff like timer events and system messages etc.?

If so then there would be no need for network waiting as WaitWindowEvent() does the multitasking thing just as nicely :)
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

I think the most advantage ould be to have it waiting in Threads as one does normally with the API Recv function
Poshu
Enthusiast
Enthusiast
Posts: 459
Joined: Tue Jan 25, 2005 7:01 pm
Location: Canada

Post by Poshu »

I'm working in console mode & don't care about windows, the only way to stop my programme is from the client side.

I can't really use a delay, cause it would slow the my code for nothing (I need speed, so much speed) & no wait would slow down the others threads (cause it's multi threaded).

I can't see any other idea than "waitnetworkserverevent()"
If anyone as an idea, even in asm, I would hear it gladly.
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

Use Winsock it pretty straight forward and easy to do cross platform since the functions under linux are the same.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Network events could be easily handled as normal window events. Just take a look on the WSAsyncSelect_() command. Its just as easy!

After bindind the socket to a windows message queue its more then easy to wait for a network event.

A delay(1) in a pooling network loop is not the smartest way to handle network events, couse: On traffic usage the delay() slows down your application a lot.
Tranquil
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

You only do the delay on networkevent()=0 of course, and handling Network events over the window events is definitely slower then the origianl way with a blocking recv in a different thread it's also not nearly as Platform independet. And it's even very good for resources as with a blocking recv in a Thread you do not even need a delay and have the thread waiting in a very effective way.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

Nik wrote:You only do the delay on networkevent()=0 of course, and handling Network events over the window events is definitely slower then the origianl way with a blocking recv in a different thread it's also not nearly as Platform independet. And it's even very good for resources as with a blocking recv in a Thread you do not even need a delay and have the thread waiting in a very effective way.
You are right, I totaly agree! Pooling is faster then message queueing.
Tranquil
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

And it's what PB currently uses I think (correct me Fred if I am wrong), I thing Fred uses a Thread which waits for recv to finish having the peek flag set, so wehn this event occurs he adds a event to the list of events used bei NetworkEvent()
klaver
Enthusiast
Enthusiast
Posts: 147
Joined: Wed Jun 28, 2006 6:55 pm
Location: Schröttersburg

Post by klaver »

In my opinion it's very needed! Delay(1) is just an absurd - it pauses the thread for ~20ms! It causes a PING 20 ON LOCALHOST...
WaitWindowEvent() is great, why you don't want to add a WaitNetworkEvent() ?
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

klaver wrote:Delay(1) is just an absurd - it pauses the thread for ~20ms! It causes a PING 20 ON LOCALHOST...
Use _timeBeginPeriod(1) and _timeEndPeriod(1) or 2 as value as that improves the granularity of the system task switching and thus Delay(1) can reach around 2ms accuracy.
See the PSDK or MSDN for more info on timeBeginPeriod
Windows Media Player does this I believe, so does the BASS sound libary to improve the system timing on XP and the NT5.x variations. Not sure about Vista but probably the same there too.
Post Reply