hlibev-0.4.0: FFI interface to libev

Safe HaskellNone

Network.Libev

Contents

Description

Network.Libev is a low-level binding to the libev library (http://libev.schmorp.de/). The libev documentation is available here: http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod.

Synopsis

Event loops

type EvLoopPtr = Ptr EvLoop

evLoop :: EvLoopPtr -> CInt -> IO ()

EVLOOP_*, EVUNLOOP_* flags

EVFLAG_* flags

evRecommendedBackends :: IO CEvFlagType

Returns the default set of CEvFlagType flags

EVBACKEND_* flags

Locking for event loops

type MutexCallback = EvLoopPtr -> IO ()

MutexCallback is called by ev_set_loop_release_cb

setupLockingForLoop :: EvLoopPtr -> IO (FunPtr MutexCallback, FunPtr MutexCallback, MVar ())

Set up the given loop for mutex locking from haskell-land -- if you want to touch the loop from other Haskell threads, you'll need to do this. The two FunPtr objects returned need to be explicitly freed with freeMutexCallback.

IMPORTANT: if you want multithreaded access to an EvLoopPtr, you'll have to acquire the MVar returned here (using withMVar) whenever you call any of the ev functions. Very bad C-land crash/bang/boom could otherwise result.

ALSO IMPORTANT: any changes you make to an EvLoopPtr from another thread while the event loop thread is blocked inside ev_loop() will NOT take effect until the the event loop thread unblocks. You'll need to set up an ev_async watcher in order to wake up the event loop thread.

Event flags

type CEventType = CInt

CEventType is a bitfield used to flag whether a file descriptor is readable, writable, or both. Valid values are ev_read and ev_write. TODO: deprecate and replace by a datatype

ev_undef :: CEventType

eventmask, revents, events...

ev_idle :: CEventType

CEvFlagType is a bitfield used to pass flags into evDefaultLoop. Values (evflag_auto, evflag_noenv, etc.) are combined with bitwise or. TODO: replace with a newtype with a monoid instance

ev_io

type EvIoPtr = Ptr EvIo

type IoCallback = EvLoopPtr -> EvIoPtr -> CEventType -> IO ()

An IoCallback is called when a file descriptor becomes readable or writable. It takes a pointer to an ev_loop structure, a pointer to an ev_io structure, and an event mask.

mkEvIo :: IO EvIoPtr

Makes a new ev_io struct using malloc. You are responsible for freeing it with freeEvIo.

freeEvIo :: EvIoPtr -> IO ()

free() an EvIoPtr

mkIoCallback :: IoCallback -> IO (FunPtr IoCallback)

Wrap up an IoCallback so it can be delivered into C-land. This resource is not garbage-collected, you are responsible for freeing it with freeIoCallback.

ev_timer

data EvTimer

Instances

type TimerCallback = EvLoopPtr -> EvTimerPtr -> CEventType -> IO ()

A TimerCallback is called when a timer expires. It takes a pointer to an ev_loop structure, a pointer to an ev_timer structure, and an (unused?) event mask.

mkEvTimer :: IO EvTimerPtr

Makes a new ev_timer struct using malloc. You are responsible for freeing it with freeEvTimer.

freeEvTimer :: EvTimerPtr -> IO ()

free() an EvTimer

mkTimerCallback :: TimerCallback -> IO (FunPtr TimerCallback)

Wrap up a TimerCallback so it can be delivered into C-land. This resource is not garbage-collected, you are responsible for freeing it with freeTimerCallback.

type EvAsyncPtr = Ptr EvAsync

type AsyncCallback = EvLoopPtr -> EvAsyncPtr -> CEventType -> IO ()

An AsyncCallback is called when you wakeup an event loop with ev_async_send

mkEvAsync :: IO EvAsyncPtr

Makes a new ev_async struct using malloc. You are responsible for freeing it with freeEvAsync.

freeEvAsync :: EvAsyncPtr -> IO ()

free() an EvAsync

mkAsyncCallback :: AsyncCallback -> IO (FunPtr AsyncCallback)

Wrap up an AsyncCallback so it can be delivered into C-land. This resource is not garbage-collected, you are responsible for freeing it with freeAsyncCallback.

Time functions

type EvTimestamp = CDouble

Libev timestamp values are C doubles containing the (floating) number of seconds since Jan 1, 1970.

evNow :: EvLoopPtr -> IO EvTimestamp

Fetch a the cached copy of the current time from a loop.

evTime :: IO EvTimestamp

Fetches the current time from the operating system. Usually evNow is preferred since it avoids a context switch by returning a cached value.

C utility functions

c_accept :: CInt -> IO CInt

Calls accept() and sets the socket non-blocking.