[Gambas-devel] About Watching and Wait()

Benoit Minisini gambas at ...20...
Mon Oct 27 23:04:25 CET 2003


Le Lundi 27 Octobre 2003 22:21, Daniel Campos a écrit :
> Hello Benoît (and company):
>
>  I was trying to create a little program using my "networking"
> component, but without using "QT" component. I could see that watching
> process was not working at all, and then I saw at Gambas Wiki that
> Wait() works only if "QT" component is present. The code is very simple:
>
> ...
> Socket.ConnectSocket("localhost",7)
> While Socket.Status <> 7  ' while connection is not completed
> 	Wait (0.1)
> 	Print "Connecting..."
> Loop
> ...
>
> Using this code, with QT all works OK, without QT loop never ends,
> as networking needs callbacks provided by GB.Watch().
>
> So I tried to use hooks in main file. Watch hook seems to do nothing
> (I don't know if it is really implemented), but then I used Wait hook,
> and calling "by hand" to the methods I use to control socket status
> all worked fine.
>
>  However, I have a question : It seems that gambas runtime only calls
> my Wait function when QT is not used in the program, so if in
> the future another component is using a watch hook, and gambas runtime
> decides to use the other's watch hook instead of mine, network component
> could not work at all... It sound like incompatible components in
> gambas!
>
>  Please tell me something about this as I don't understand it very well,
> I would like to make work with networking possible even if there's
> not any graphic library installed.
>
> Regards,

The aim of the WATCH hook is to watch file descriptor by calling select().

In a GUI library like QT, the event loop is in fact a select() that watch the 
file descriptor that points to the X11 socket.

In the Gambas QT component, the WATCH hook simply asks QT to watch other file 
descriptors.

The interpreter should already have a select() loop in its own WATCH hook, but 
as a lazy man, I didn't implement it yet ! Mmmm... It might be urgent, now 
:-)

The aim of the WAIT hook is to wait a specified time by calling the WATCH 
hook.

In the QT component, this is simply done by using a Qt timer. These Qt timers 
are internally managed by using the 'timeout' parameter of select().

I must implement the same thing in the interpreter, so that you can watch file 
descriptors AND call the WAIT instruction without using the QT component.

I will do that as soon as possible, so that it is in the next version.

Only ONE component can implement the event loop, i.e. the watch and the wait 
hooks. So the last loaded component implementing them wins.

This event loop is the ONLY one function that can block the interpreter by 
calling select(). So your networking component, for example, must not call 
accept() directly for example. It must instead watch the socket file 
descriptor by calling GB.Watch(), return to the event loop, and call accept() 
at the beginning of the watch callback.

It seems that you already did the good thing in your code :-)

But I see that you raised an event into a callback : never do that ! Because 
it can lead to infinite loops. The event can call a gambas event handler that 
may return raise the same callback. You must use instead GB.Post() to post a 
function that will be call at the return of the event loop.

Was I clear ?

Regards,

-- 
Benoit Minisini
mailto:gambas at ...1...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: watch hook.pdf
Type: application/pdf
Size: 22298 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/devel/attachments/20031027/c32bd1b5/attachment.pdf>


More information about the Devel mailing list