-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from jd/master
Low-level fd listening support
- Loading branch information
Showing
5 changed files
with
61 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
(in-package :cl-async) | ||
|
||
(cffi:defcallback fd-cb :void ((fd :int) (what :short) (data-pointer :pointer)) | ||
(declare (ignore fd)) | ||
(let* ((ev (deref-data-from-pointer data-pointer)) | ||
(callbacks (get-callbacks data-pointer)) | ||
(timeout-cb (getf callbacks :timeout-cb)) | ||
(read-cb (getf callbacks :read-cb)) | ||
(write-cb (getf callbacks :write-cb)) | ||
(event-cb (getf callbacks :event-cb))) | ||
(catch-app-errors event-cb | ||
(when (and (< 0 (logand what le:+ev-read+)) | ||
read-cb) | ||
(funcall read-cb)) | ||
(when (and (< 0 (logand what le:+ev-write+)) | ||
write-cb) | ||
(funcall write-cb)) | ||
(when (and (< 0 (logand what le:+ev-timeout+)) | ||
timeout-cb) | ||
(funcall timeout-cb))))) | ||
|
||
(defun fd-add (fd &key event-cb read-cb write-cb timeout-cb timeout) | ||
"Run a function, asynchronously, when the specified file descriptor is | ||
ready for write or read operations. An event loop must be running for | ||
this to work." | ||
(check-event-loop-running) | ||
(let* ((data-pointer (create-data-pointer)) | ||
(ev (le:event-new *event-base* | ||
fd | ||
;; listen to read/timeout events, and keep listening | ||
(logior | ||
(if timeout-cb le:+ev-timeout+ 0) | ||
(if read-cb le:+ev-read+ 0) | ||
(if write-cb le:+ev-write+ 0) | ||
le:+ev-persist+) | ||
(cffi:callback fd-cb) | ||
data-pointer))) | ||
(save-callbacks data-pointer (list :read-cb read-cb | ||
:write-cb write-cb | ||
:timeout-cb timeout-cb | ||
:event-cb event-cb)) | ||
(attach-data-to-pointer data-pointer ev) | ||
(if (numberp timeout) | ||
(with-struct-timeval time-c timeout | ||
(le:event-add ev time-c)) | ||
(le:event-add ev (cffi:null-pointer))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,9 @@ | |
;; timer functions | ||
#:delay | ||
|
||
;; fd functions | ||
#:fd-add | ||
|
||
;; signal numbers | ||
#:+sighup+ | ||
#:+sigint+ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters