fend is an event framework utilising LuaJIT's ffi.
It currently has poll and epoll based dispatchers.
The processed C headers in include/ are from a linux x64 system; they may not be suitable for you.
Note: Most functions can raise errors.
Just place the fend directory in your lua module search path
This module wraps raw file descriptors (usually ints) in a structure to attach methods.
-
my_file = file.wrap ( fd , no_close )
Wraps the fd in a fend 'file' structure.
fdmay be a numeric file descritor or a lua file object.
no_closewill skip closing the file on object collection -
my_file:getfd ( )
returns the raw file descriptor -
my_file:set_blocking ( bool )
sets theO_NONBLOCKflag
A non-blocking luasocket-like interface
-
sock = socket.new_tcp ( domain )
Creates a new socket with the given domain (wheredomainis anAF_*; these can conveniently be found in an addrinfo structure:addrinfo.ai_family(see DNS section)) -
sock:getfile ( )Returns the file descriptor for the socket -
sock:getfd ( )
Shortcut forsock:getfile():getfd() -
sock:set_option ( level , option , val , size )
Callssetsockoptwith the given parameters
ifvalis a number or boolean it will be converted to an int
sizeis optional, if not passed will besizeof(val) -
sock:set_socket_option ( option , val , size )
Looks up the stringoptionand callssock:set_option
eg,option = "REUSEADDR" -
error_string , errno = sock:get_error ( )Retrives the error on the socket Returnsnilif there is no error -
sock:bind ( addrinfo )
sock:bind ( sockaddr , sockaddr_len )
Binds the socket to the interface given inaddrinfoorsockaddr , sockaddr_len -
sock:listen ( backlog )
Sets the socket to listen with the given backlog (defaults to 128) -
sock:accept ( with_sockaddr )
Accepts a new client onsock.
Can returnnilif no client is available.
The returned socket will haveO_NONBLOCKset.
Ifwith_sockaddris true, the call will return asockaddrand length as extra return values -
sock:shutdown ( )
Aliased as sock:_shutdown to prevent internal conflicts with the SSL object -
n_read = sock:recv ( buff , len , flags )
n_read = sock:receive ( buff , len , flags )
buffshould be a user allocated buffer of lengthlen.
flagsdefaults to0.
Returns the number of bytes read. -
n_read = sock:peek ( buff , len , flags )
Same assock:recv, but the MSG_PEEK flag is set -
n_bytes = sock:send ( buff , len , flags )
Sends buff to the connected peer
Ifbuffis not aconst char*then it will havetostringcalled on it.
Iflenis not given, it is the length of the string.
flagsdefaults to0.
Returns the number of bytes written. -
sock:getpeername ( )
Returns asockaddrand it's length representing the connected peer
Provides a luasec-like interface (even supporting the same context types)
require "fend.ssl_handshake".handshake ( sock , dispatcher , callback )
Will do an ssl handshake on the given socket, and when complete, call your callback.
Provides dns and host based utilities.
-
dns.lookup ( hostname , port , hints )
Completes a blocking lookup, returning anaddrinfostructure hints can beNULLor anaddrinfostructure -
dns.lookup_async ( hostname , port , hints , dispatcher , callback )
Does a non-blocking dns lookup, and when ready.
Will call callbackcbwith anaddrinfostructure.
Returns an object with methods:
wait: blocks until the lookup is completed (or untiltimeout). returns boolean indicating success/failure
-
examples/all.lua [dispatcher]
is a demonstration of every module working together
dispatchercan be eitherpollorepoll -
examples/http_client.lua
is a basic http client
the module returns a table with functions:
request ( url , options , dispatcher , callback )
The include directory contains a partial replication of processed files from the /usr/include directory.