-
Notifications
You must be signed in to change notification settings - Fork 17
/
prot-daytime.r
90 lines (87 loc) · 1.6 KB
/
prot-daytime.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Rebol [
file: %prot-daytime.r
author: "Graham"
rights: 'BSD
date: 8-Jan-2010
]
make-scheme [
name: 'daytime
title: "Daytime Protocol"
spec: make system/standard/port-spec-net [port-id: 13 ]
awake: func [event /local port] [
; print ["=== Client event:" event/type]
port: event/port
switch event/type [
lookup [
; print "DNS lookup"
open port
]
connect [
print "connected"
read port
; print to-string port/data
]
read [
; print [" " to-string port/data]
close port
return true ; quits the awake
]
wrote [read port]
]
false
]
actor: [
open: func [
port [port!]
/local conn
] [
if port/state [return port]
if none? port/spec/host [http-error "Missing host address"]
port/state: context [
state: 'ready
connection:
error: none
awake: :port/awake
close?: yes
]
port/state/connection: conn: make port! [
scheme: 'tcp
host: port/spec/host
port-id: port/spec/port-id
ref: rejoin [tcp:// host ":" port-id]
]
conn/awake: :awake
open conn
port
]
open?: func [
port [port!]
][
all [ port/state ]
]
close: func [
port [port!]
] [
if open? port [
close port/state/connection
port/state/connection/awake: none
port/state: none
]
port
]
read: func [
port [port!]
/local conn
] [
either any-function? :port/awake [
; unless open? port [cause-error 'Access 'not-open port/spec/ref]
unless open? port [
wait open port
]
port
] [
; do something synchronous here
]
]
]
]