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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - server-socket.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 12pt; font-size: 1em; }
.Delimiter { color: #800080; }
.muControl { color: #c0a020; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #c00000; }
.muRecipe { color: #ff8700; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Example of reading from a socket using channels and writing back to it</span>
<span class="Comment"># directly. Running this file and navigating to <address of server>:8080</span>
<span class="Comment"># should result in your browser displaying "SUCCESS!".</span>
<span class="Comment">#</span>
<span class="Comment"># Unfortunately, the reading implementation has some redundant, inelegant</span>
<span class="Comment"># code to make up for my lack of insight into Linux's socket internals.</span>
<span class="muRecipe">def</span> main [
<span class="Constant">local-scope</span>
socket:num<span class="Special"> <- </span>$socket <span class="Constant">8080/port</span>
$print <span class="Constant">[Mu socket creation returned ]</span>, socket, <span class="Constant">10/newline</span>
session:num<span class="Special"> <- </span>$accept socket
contents:&:source:char, sink:&:sink:char<span class="Special"> <- </span>new-channel <span class="Constant">30</span>
sink<span class="Special"> <- </span>start-running transmit-from-socket session, sink
buf:&:buffer<span class="Special"> <- </span>new-buffer <span class="Constant">30</span>
<span class="Delimiter">{</span>
c:char, done?:bool, contents<span class="Special"> <- </span>read contents
<span class="muControl">break-if</span> done?
buf<span class="Special"> <- </span>append buf, c
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
socket-text:text<span class="Special"> <- </span>buffer-to-array buf
$print <span class="Constant">[Done reading from socket.]</span>, <span class="Constant">10/newline</span>
write-to-socket session, <span class="Constant">[HTTP/1.0 200 OK</span>
<span class="Constant">Content-type: text/plain</span>
<span class="Constant">SUCCESS!</span>
<span class="Constant">]</span>
$print <span class="Constant">10/newline</span>, <span class="Constant">[Wrote to and closing socket...]</span>, <span class="Constant">10/newline</span>
$close-socket session
$close-socket socket
]
<span class="muRecipe">def</span> write-to-socket session-socket:number, s:address:array:character [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
len:number<span class="Special"> <- </span>length *s
i:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
done?:boolean<span class="Special"> <- </span>greater-or-equal i, len
<span class="muControl">break-if</span> done?
c:character<span class="Special"> <- </span>index *s, i
$print <span class="Constant">[writing to socket: ]</span>, i, <span class="Constant">[ ]</span>, c, <span class="Constant">10/newline</span>
$write-to-socket session-socket, c
i<span class="Special"> <- </span>add i, <span class="Constant">1</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
]
<span class="muRecipe">def</span> transmit-from-socket session:num, sink:&:sink:char<span class="muRecipe"> -> </span>sink:&:sink:char [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
req:text, bytes-read:number<span class="Special"> <- </span>$read-from-socket session, <span class="Constant">4096/bytes</span>
$print <span class="Constant">[read ]</span>, bytes-read, <span class="Constant">[ bytes from socket]</span>, <span class="Constant">10/newline</span>
i:number<span class="Special"> <- </span>copy <span class="Constant">0</span>
<span class="Delimiter">{</span>
done?:boolean<span class="Special"> <- </span>greater-or-equal i, bytes-read
<span class="muControl">break-if</span> done?
c:char<span class="Special"> <- </span>index *req, i
end-of-request?:bool<span class="Special"> <- </span>equal c, <span class="Constant">10/newline</span>
<span class="muControl">break-if</span> end-of-request? <span class="Comment"># To be safe, for now.</span>
sink<span class="Special"> <- </span>write sink, c
i<span class="Special"> <- </span>add i, <span class="Constant">1</span>
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
sink<span class="Special"> <- </span>close sink
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|