annotate samples/epoll.tp @ 375:f8d80c16abbd

Add epoll module and a basic epoll sample
author Michael Pavone <pavone@retrodev.com>
date Fri, 14 Aug 2015 23:08:54 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
375
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #{
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 echo <- :sock {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 print: "New connection\n"
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 data <- sock recv: 4096
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 while: { (data length) > 0 } do: {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 sock send: data
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 data <- sock recv: 4096
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 print: "Connection closed\n"
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 continue? <- true
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 acceptHandler <- :sock ep {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 epe <- epoll events
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 :_ {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 (sock accept) value: :csock {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 ep addFD: (csock fd) withMask: (epe in) data: (clientHandler: csock ep)
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 } none: {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 print: "Failed to accept new connection\n"
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
20 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
21 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
22
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 clientHandler <- :sock ep {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24 epe <- epoll events
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 _buffers <- #[]
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 waitingOut <- false
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
27 _me <- :eventMask {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
28 if: eventMask and (epe in) != 0 {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
29 _buffers append: (sock recv: 4096)
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
30 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 if: eventMask and (epe out) != 0 && (_buffers length) > 0 {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 buf <- _buffers join: ""
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 _buffers <- #[]
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 sent <- sock send: buf
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 if: sent < (buf byte_length) {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 _buffers append: (buf from: sent)
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 if: (_buffers length) > 0 {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 if: (not: waitingOut) {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 waitingOut <- true
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 ep modifyFD: (sock fd) setMask: (epe in) or (epe out) data: _me
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 } else: {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 if: waitingOut {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 waitingOut <- false
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
47 ep modifyFD: (sock fd) setMask: (epe in) data: _me
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
48 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
49 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
50 //TODO: Handle connection close/error
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 _me
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
53 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 main <- :args {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 port <- "2323"
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 if: (args length) > 1 {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 port <- args get: 1
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 (socket listenOnPort: port) value: :lsock {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 print: "Listening on port " . port . "\n"
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 epe <- epoll events
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 (epoll create) value: :ep {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 ep addFD: (lsock fd) withMask: (epe in) data: (acceptHandler: lsock ep)
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 while: { continue? } do: {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 (ep wait: -1 maxEvents: 16) value: :events {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 foreach: events :idx ev {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 handler <- ev data
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 handler: (ev event)
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 } none: {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 print: "Failed to wait for events\n"
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 continue? <- false
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 } none: {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 print: "Failed to create epoll file descriptor"
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 } none: {
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 print: "Failed to listen on port " . port . "\n"
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 }
f8d80c16abbd Add epoll module and a basic epoll sample
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 }