Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
REBOL []
awake-handler: funct [
event
/local port
][
port: event/port
switch/default probe event/type [
lookup [
open port
]
connect [
write port port/locals
]
wrote [
read port
]
read [
port/spec/data: copy port/data
clear port/data
return true
]
close [
return true
]
] [
print ["Unexpected event:" event/type]
close port
return true
]
false ; returned
]
sys/make-scheme [
name: 'irc
title: "IRC Protocol"
spec: make system/standard/port-spec-net [
port-id: 6667
timeout: 0:05
data: make binary! 1000
]
actor: [
open: funct [port] [
port/state: context [
tcp-port: none
]
tcp-port: port/state/tcp-port: make port! [
scheme: 'tcp
host: port/spec/host
port-id: port/spec/port-id
timeout: port/spec/timeout
data: make binary! 1000
ref: rejoin [tcp:// host ":" port-id]
port-state: 'init
]
tcp-port/awake: :awake-handler
open tcp-port
port
]
open?: func [port] [
not none? port/state
]
close: func [port] [
port/state: none
port
]
write: func [
port data
] [
unless open? port [open port]
tcp-port: port/state/tcp-port
if none? tcp-port/locals [
tcp-port/locals: make binary! 1000
]
append tcp-port/locals data
true
]
read: func [port /part len] [
tcp-port: port/state/tcp-port
wait [tcp-port port/spec/timeout]
clear tcp-port/locals
print ["len: " length? tcp-port/spec/data]
probe to string! tcp-port/spec/data
tcp-port/spec/data
]
]
]
test: [
irc: open irc://irc.freenode.net
write irc rejoin [
"USER guest 0 * :Ronnie Reagan" crlf
"NICK rebol" crlf
]
ret: to string! read irc
ret: to string! read irc
]
do test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment