Pull out event-handling stub, to show where future hooks will go

This commit is contained in:
Tony Garnock-Jones 2011-12-15 16:21:07 -05:00
parent 7e56c9bf12
commit 5181e0fce0
1 changed files with 25 additions and 15 deletions

View File

@ -4,6 +4,7 @@
(require racket/match)
(require racket/udp)
(require (only-in srfi/1 append-reverse))
(require "dump-bytes.rkt")
(provide (struct-out udp-packet)
@ -53,9 +54,9 @@
(values '() state)))
event-handlers))
(define (dispatch-events events old-state)
(define (dispatch-events events next-events-rev old-state)
(if (null? events)
(read-and-dispatch old-state)
(check-for-io (reverse next-events-rev) old-state)
(let ((classified-packet (car events)))
(define-values (new-events new-state)
(let search ((handlers event-handlers))
@ -63,20 +64,29 @@
[(null? handlers) (default-handler classified-packet old-state)]
[((caar handlers) classified-packet) ((cdar handlers) classified-packet old-state)]
[else (search (cdr handlers))])))
(dispatch-events (append (cdr events) new-events) new-state))))
(dispatch-events (cdr events)
(append-reverse new-events next-events-rev)
new-state))))
(define (read-and-dispatch old-state)
(define (check-for-io pending-events old-state)
(define buffer (make-bytes packet-size-limit))
(define-values (packet-length source-hostname source-port)
(udp-receive! s buffer))
(define packet (subbytes buffer 0 packet-length))
(define new-events
(sync (handle-evt (udp-receive!-evt s buffer)
(match-lambda
[(list packet-length source-hostname source-port)
(define packet (subbytes buffer 0 packet-length))
(printf ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>~n~v~n" packet)
(dump-bytes! buffer packet-length)
(flush-output)
(printf ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>~n~v~n" packet)
(dump-bytes! buffer packet-length)
(flush-output)
(define packet-and-source
(udp-packet packet source-hostname source-port))
(define classified-packet (packet-classifier packet-and-source))
(list classified-packet)]))
(if (null? pending-events)
never-evt ;; Nothing waiting to be done, don't wake up until sth external arrives
(handle-evt (system-idle-evt)
(lambda (dummy) '())))))
(dispatch-events (append new-events pending-events) '() old-state))
(define packet-and-source (udp-packet packet source-hostname source-port))
(define classified-packet (packet-classifier packet-and-source))
(dispatch-events (list classified-packet) old-state))
(read-and-dispatch initial-state))
(check-for-io '() initial-state))