libevent2.lsp

Module index

source download

Module: libevent

Low-level newlisp bindings for libevent2.

Version: 0.1
Author: Jeff Ober


Location: https://raw.github.com/jsober/nl-event/master/libevent2.lsp


The libevent module provides a wrapper on top of the libevent2 library.

TODO

Example:
 ; ------------------------------------------------------------------------------
 ; Timers
 ; ------------------------------------------------------------------------------
 (libevent:init)

 (libevent:set-interval 10
   (fn () (println "Another 10ms have passed!")))

 (libevent:run)


 ; ------------------------------------------------------------------------------
 ; IO
 ; ------------------------------------------------------------------------------
 (libevent:init)
 (setf socket (net-connect "www.google.com" 80))
 (setf buffer "")

 ; Wait until socket is write-ready
 (libevent:watch-once socket libevent:WRITE
   (fn (fd e id)
     ; send HTTP request
     (write socket "GET / HTTP/1.0\r\n\r\n")

     ; wait for response
     (libevent:watch socket libevent:READ
       (fn (fd e id , buf bytes)
         ; read to local buffer
         (setf bytes (read fd buf 4096))
         (if bytes
           ; write to global buffer
           (write buffer buf)
           ; kill watcher and stop loop
           (begin
             (libevent:unwatch id)
             (libevent:stop)))))))

 (libevent:run)
 (println buffer)


 ; ------------------------------------------------------------------------------
 ; Using buffers
 ; ------------------------------------------------------------------------------
 (libevent:init)
 
 (setf html "")
 
 (define (on-read data)
   (write html data))
 
 (define (on-event ev data)
   (cond
     ((libevent:masks? ev libevent:BUFFER_EOF)
      (write html data)
      (println "Disconnected")
      (libevent:stop))
     ((libevent:masks? ev libevent:BUFFER_ERROR)
      (println "An error occurred")
      (libevent:stop))
     ((libevent:masks? ev libevent:BUFFER_TIMEOUT)
      (println "Timed out")
      (libevent:stop))))
 
 (or (setf socket (net-connect "www.google.com" 80))
     (throw-error "Unable to connect"))
 
 (setf buffer (libevent:make-buffer socket (regex-comp "[\r\n]+" 4) on-read on-event))
 (libevent:buffer-send buffer "GET / HTTP/1.0\r\n\r\n")
 (libevent:run)
 
 (println html)

Event constants

Const: READ
Const: WRITE
Const: TIMEOUT
Const: SIGNAL

Buffer constants

Const: BUFFER_READING
Const: BUFFER_WRITING
Const: BUFFER_EOF
Const: BUFFER_ERROR
Const: BUFFER_TIMEOUT
Const: BUFFER_CONNECTED


§

init

syntax: (init)
Initializes the event loop. Will not re-init a previously initialized loop unless cleanup is called first.

§

run

syntax: (run)
Starts the event loop. Does not return until the loop is stopped.

§

stop

syntax: (stop)
Halts the event loop after the next iteration.

§

watch

syntax: (watch fd ev cb once)
parameter: int - fd An open file descriptor
parameter: int - ev A bitmask of event constants
parameter: fn - cb A callback function
parameter: bool - once When true (default false) callback is triggered only once

return: string id used to manage the event watcher

Registers callback function cb to be called whenever an event masked in ev is triggered for fd. cb is called with the file descriptor, event, and id as its arguments.

Example:
 (watch socket (| READ WRITE)
   (fn (fd e)
     (cond
       (== e READ) (...)
       (== e WRITE) (...))))


§

unwatch

syntax: (unwatch id)
parameter: string - id ID returned by watch
Unregisters an event watcher. Once unwatched, the watcher id is invalid and may no longer be used.

Example:
 (watch socket WRITE
   (lambda (fd e id)
     (unwatch id)
     (write fd "Hello world")))


§

watch-once

syntax: (watch-once fd ev cb)
parameter: int - fd An open file descriptor
parameter: int - ev A bitmask of event constants
parameter: fn - cb A callback function
Registers a callback cb for events ev on descriptor fd. After the callback is triggered, it is automatically unregistered for events ev. For example, the example code from unwatch could be rewritten as:

Example:
 (watch-once socket WRITE
   (lambda (fd e)
     (write fd "Hello world")))


§

set-interval

syntax: (set-interval msec cb)
parameter: int - msec Millisecond interval
parameter: fn - cb A callback function

return: string Returns the timer id

Registers a callback cb to be executed every msec milliseconds. Note that the timing is not guaranteed; cb will be called on the first iteration of the event loop after msec milliseconds have passed since its last execution. Returns an event ID that may be used to clear the interval event using clear-interval.

Example:
 (set-interval 500 (fn () (println "Another 500ms have passed")))


§

clear-interval

syntax: (clear-interval id)
parameter: string - id id of a timer event
Clears an interval id.

Example:
 (setf n 10)
 (set-interval 500
   (fn (fd e id) ; fd is nil and e is TIMEOUT
     (when (zero? (dec n))
       (clear-interval id))))


§

set-timer

syntax: (set-timer msec cb)
parameter: int - msec Millisecond interval
parameter: fn - cb A callback function

return: string Returns the timer id

Registers a callback cb to be executed one time after msec milliseconds.

Example:
 (set-timer 500 (fn () (println "500ms have elapsed.")))


§

make-buffer

syntax: (make-buffer socket read-marker on-read on-event)
parameter: int - socket an open socket; must not be a pipe
parameter: regex - read-marker a compiled regex
parameter: fn - on-read
parameter: fn - on-event

return: string an id used to identify the buffer

Creates a new buffer object. Configures buffer to call on-read whenever the buffer is able to match its contents against pre-compiled regex read-marker. on-event is triggered in the event of a disconnected socket, error, etc.

§

free-buffer

syntax: (free-buffer id)
parameter: string - id buffer id
Cleans up after a buffer. The buffer is not usable after calling this routine.

§

buffer-send

syntax: (buffer-send id data)
parameter: string - id buffer id
parameter: string - data data to send
Queues data to be sent along the socket transport of buffer buffer-id.

- ∂ -

generated with newLISP  and newLISPdoc