Skip to content

Instantly share code, notes, and snippets.

@rgchris
Last active January 5, 2017 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgchris/1520d92b4d7c53806ea95c2ad613050e to your computer and use it in GitHub Desktop.
Save rgchris/1520d92b4d7c53806ea95c2ad613050e to your computer and use it in GitHub Desktop.
Port example: Send block to Server, Responds with reversed block.
#!/usr/local/bin/ren-c
Rebol [
Title: "Ping Scheme"
Date: 5-Jan-2017
Author: "Christopher Ross-Gill"
Home: https://github.com/revault/rebol-wiki/wiki/Port-Examples#tcp-ping-pong-messages
]
sys/make-scheme [
Title: "Ping Client"
Name: 'ping
Actor: [
Write: func [port data /local subport][
wait open subport: make port! [
scheme: 'tcp
host: port/spec/host
port-id: port/spec/port-id
awake: func [event][
switch event/type [
lookup [
open event/port
false
]
connect [
write event/port to binary! mold data
false
]
wrote [
read event/port
false
]
done [event/port/data: read event/port true]
]
]
]
wait 0.01 ; surely this shouldn't be needed
any [
attempt [load subport/data]
[fail]
]
]
]
]
probe write ping://127.0.0.1:8000 [foo bar]
#!/usr/local/bin/ren-c
Rebol [
Title: "Pong Scheme"
Date: 5-Jan-2017
Author: "Christopher Ross-Gill"
Home: https://github.com/revault/rebol-wiki/wiki/Port-Examples#tcp-ping-pong-messages
]
sys/make-scheme [
Title: "Pong Server"
Name: 'pong
Actor: [
open: func [port][
print ["Opening Server:" port/locals: rejoin [tcp://: port/spec/port-id]]
port/locals: open port/locals
port/locals/awake: func [event /local client][
if event/type = 'accept [
client: first event/port
client/awake: func [event /local message] [
;probe event/type
switch event/type [
read [
message: probe attempt [load event/port/data]
clear event/port/data
write event/port to binary! mold probe either message [
compose/only [ok (reverse compose [(message)])]
][
[fail]
]
return false
]
wrote [
print "Server sent pong to client"
close event/port
return true
]
close [
close event/port
return true
]
]
false
]
read client
]
false
]
port
]
close: func [port][
close port/locals
]
]
]
wait open pong://:8000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment