Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added examples directory and wrapped ##epoll#epoll_ctl in an exported…

… function
  • Loading branch information...
commit 3f3a6c41c1a566f4e5c6c5ea0dc43cbe05667012 1 parent 93c3abf
David Reynolds authored

Showing 3 changed files with 23 additions and 7 deletions. Show diff stats Hide diff stats

  1. +2 0  .gitignore
  2. +6 0 examples/echo/echo.scm
  3. +15 7 src/server.scm
2  .gitignore
... ... @@ -1 +1,3 @@
1 1 *.swp
  2 +*.o
  3 +*.so
6 examples/echo/echo.scm
... ... @@ -0,0 +1,6 @@
  1 +(require 'rooster)
  2 +
  3 +(define (handler fd buf)
  4 + (send-to-client fd buf))
  5 +
  6 +(run-rooster handler)
22 src/server.scm
@@ -12,7 +12,11 @@
12 12
13 13 (declare
14 14 (uses tcp)
15   - (export run-rooster send-to-client send-to-all remove-client))
  15 + (export run-rooster
  16 + send-to-client
  17 + send-to-all
  18 + remove-client
  19 + rooster-epoll-ctl))
16 20
17 21 ;; required for make-hash-table (compiled vs. interpreted)
18 22 (require 'srfi-69)
@@ -49,6 +53,10 @@ EOF
49 53 ;; use foreign-safe-lambda because this C function calls back into Chicken
50 54 (define ##epoll#epoll_wait (foreign-safe-lambda void "_epoll_wait" int int))
51 55
  56 +;; exported wrapper around epoll_ctl
  57 +(define (rooster-epoll-ctl flags fd iostate)
  58 + (##epoll#epoll_ctl epfd flags fd iostate))
  59 +
52 60 ;; define this here because it's not exported by tcp.scm
53 61 (define setnonblock (foreign-lambda* bool ((int fd))
54 62 "int val = fcntl(fd, F_GETFL, 0);"
@@ -90,7 +98,7 @@ EOF
90 98 (set! fd-list (filter-out-fd fd))
91 99 (hash-table-remove fd-write-table fd)
92 100 (hash-table-remove fd-read-table fd)
93   - (##epoll#epoll_ctl epfd _EPOLL_CTL_DEL fd 0)
  101 + (rooster-epoll-ctl _EPOLL_CTL_DEL fd 0)
94 102 (##net#close fd))
95 103
96 104 (define (send-to-client fd str)
@@ -100,7 +108,7 @@ EOF
100 108 (let ((buf (hash-table-ref fd-write-table fd)))
101 109 (hash-table-set! fd-write-table fd (string-append buf str)))
102 110
103   - (##epoll#epoll_ctl epfd _EPOLL_CTL_MOD fd _WRITE))
  111 + (rooster-epoll-ctl _EPOLL_CTL_MOD fd _WRITE))
104 112
105 113 (define (send-to-all buf sender-fd)
106 114 (let fdloop ((fds fd-list))
@@ -117,7 +125,7 @@ EOF
117 125 (init-client fd)
118 126 (set! fd-list (cons fd fd-list))
119 127 (send-to-client fd "Simple Echo Server\n\n")
120   - (##epoll#epoll_ctl epfd _EPOLL_CTL_ADD fd _WRITE)))
  128 + (rooster-epoll-ctl _EPOLL_CTL_ADD fd _WRITE)))
121 129
122 130 (define (write-handler fd)
123 131 ;; epoll tells us to write to socket
@@ -131,7 +139,7 @@ EOF
131 139 (hash-table-set! fd-write-table fd "")
132 140
133 141 ;; update epoll to watch for a read event on this fd
134   - (##epoll#epoll_ctl epfd _EPOLL_CTL_MOD fd _READ))
  142 + (rooster-epoll-ctl _EPOLL_CTL_MOD fd _READ))
135 143
136 144 (define (read-loop fd maxlen)
137 145 (let ((rbuf (make-string maxlen)))
@@ -153,7 +161,7 @@ EOF
153 161 (let* ((len 4096)
154 162 (buf (read-loop fd len)))
155 163 (_RequestHandler fd buf)
156   - (##epoll#epoll_ctl epfd _EPOLL_CTL_MOD fd _WRITE)))
  164 + (rooster-epoll-ctl _EPOLL_CTL_MOD fd _WRITE)))
157 165
158 166 (define (fd-event-list-handler ls)
159 167 ;; takes a list of (fd . events) pairs
@@ -188,7 +196,7 @@ EOF
188 196 (set! epfd (##epoll#epoll_create))
189 197 (set! _RequestHandler request-handler)
190 198
191   - (##epoll#epoll_ctl epfd _EPOLL_CTL_ADD sfd _READ)
  199 + (rooster-epoll-ctl _EPOLL_CTL_ADD sfd _READ)
192 200
193 201 (let loop ()
194 202 (##epoll#epoll_wait epfd 200)

0 comments on commit 3f3a6c4

Please sign in to comment.
Something went wrong with that request. Please try again.