Async Callback
[1/5] from: charles::wardell::dendrite::com at: 18-Nov-2004 10:58
I finally managed to get the client and server working with Anton's
support.
I don't fully understand the callback mechanism. Here is the code for
the test server below. Let's say I wanted to print the #'s 1 through a
million but handle the events as they occur. Where would I put my print
statement? Am I wrong in assuming that this is the way the event works.
I would like to put some standard processing in the event loop but have
no idea where to put it.
do %/d/projects/rebol/rebolspaces/async-protocol2.r
listen: open/no-wait tcp://:8181
listen/awake: func [l /local p] [
;print "Server: A Client connected"
p: first listen
port: make port! [scheme: 'async sub-port: p]
open port
port/awake: do server-handler
false
]
server-handler: [use [ buffers ] [
buffers: copy []
func [port [port!] state [word! error!] /local tmp cmd buffer] [
if error? :state [print mold disarm state return true]
switch state [
connect [
;print "Server: connect"
]
read [
;print "Server: read"
if not find buffers port [append buffers reduce [port
copy ""]]
print ["buffers length:" length? buffers]
buffer: buffers/:port
append buffer copy port
while [tmp: find buffer newline]
[
cmd: copy/part buffer tmp
remove/part buffer next tmp
;print ["Server: Recieved:" mold cmd]
print [mold cmd]
]
false ;close port
]
write [
;print "Server: write"
false
]
close [
;print "Server: close"
close port
false
]
]
]
]
]
insert tail system/ports/wait-list listen
port: none
forever [
wait []
]
[2/5] from: gabriele:colellachiara at: 18-Nov-2004 18:48
Hi Charles,
On Thursday, November 18, 2004, 4:58:32 PM, you wrote:
WC> forever [
WC> wait []
WC> ]
The forever is not needed if you always return FALSE from your
AWAKE functions.
I'm not sure I understand your question about where to put a
PRINT.
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/
[3/5] from: charles::wardell::dendrite::com at: 18-Nov-2004 14:22
Thanks for the tip.
I would like to be able to do some processing while waiting for the NEXT
request. If possible, where would I put that code?
[4/5] from: antonr::lexicon::net at: 19-Nov-2004 17:11
Hi Charles,
You could break up your processing into small chunks
and do them each iteration of the following loop:
repeat i 10000 [
; <-- do a bit of the work here
wait 0.05 ; allow time for processing events
]
Anton.
[5/5] from: gabriele:colellachiara at: 19-Nov-2004 12:44
Hi Charles,
On Thursday, November 18, 2004, 8:22:31 PM, you wrote:
WC> I would like to be able to do some processing while waiting for the NEXT
WC> request. If possible, where would I put that code?
The short answer is that it is not possible, since if you are
doing any processing, you are not receiving events.
However, what you can do is doing a bit of processing, then
waiting a bit for events, then doing another bit of processing,
and so on. In this case, you'd need a loop, because you want to
exit from the WAIT every so often so that you can do your bit of
processing.
For example:
forever [
wait 0 ; this does not actually wait, but allows the
; processing of events that have already happened
; if you don't want to be processing all the time,
; you might want to wait more.
do-some-processing ; here you should do a little step of
; processing; while you are doing this,
; you are *NOT* processing events, so this
; step should be as quick as possible.
]
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amiga Group Italia sez. L'Aquila --- SOON: http://www.rebol.it/