Skip to content

sysread/nl-event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 

Repository files navigation

nl-event

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

Todo

  • Signals

Timers

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

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

(libevent:run)

Events

(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)

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)

License

Copyright (C) 2012 "Jeff Ober"

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

About

libevent2 support for newlisp

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages