[REBOL] Async Callback
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 []
]