Connection
libevent :: ConnectionFactory :: accept_connection
Accept a connection onlistener
libevent :: ConnectionFactory :: bind_tcp
Listen on the TCP socket ataddress
:port
for new connections
libevent :: ConnectionFactory :: bind_unix
Listen on a UNIX domain socket for new connectionslibevent :: ConnectionFactory :: event_base
TheNativeEventBase
for the dispatch loop of this factory
libevent :: ConnectionFactory :: event_base=
TheNativeEventBase
for the dispatch loop of this factory
libevent :: ConnectionFactory :: spawn_connection
Create a newConnection
object for buffer_event
libevent $ ConnectionFactory :: SELF
Type of this instance, automatically specialized in every classlibevent :: ConnectionFactory :: accept_connection
Accept a connection onlistener
libevent :: ConnectionFactory :: bind_tcp
Listen on the TCP socket ataddress
:port
for new connections
libevent :: ConnectionFactory :: bind_unix
Listen on a UNIX domain socket for new connectionscore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Object :: defaultinit
libevent :: ConnectionFactory :: event_base
TheNativeEventBase
for the dispatch loop of this factory
libevent :: ConnectionFactory :: event_base=
TheNativeEventBase
for the dispatch loop of this factory
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: output_class_name
Display class name on stdout (debug only).libevent :: ConnectionFactory :: spawn_connection
Create a newConnection
object for buffer_event
HttpServer
instances, and hold the libevent base handler
# Factory to listen on sockets and create new `Connection`
class ConnectionFactory
# The `NativeEventBase` for the dispatch loop of this factory
var event_base: NativeEventBase
# Accept a connection on `listener`
#
# By default, it creates a new NativeBufferEvent and calls `spawn_connection`.
fun accept_connection(listener: ConnectionListener, fd: Int, addrin: Pointer, socklen: Int)
do
var base = listener.base
var bev = new NativeBufferEvent.socket(base, fd, bev_opt_close_on_free)
# Human representation of remote client address
var addr_len = 46 # Longest possible IPv6 address + null byte
var addr_buf = new CString(addr_len)
addr_buf = addrin_to_address(addrin, addr_buf, addr_len)
var addr = if addr_buf.address_is_null then
"Unknown address"
else addr_buf.to_s
var conn = spawn_connection(bev, addr)
bev.enable ev_read|ev_write
bev.setcb conn
end
# Create a new `Connection` object for `buffer_event`
fun spawn_connection(buffer_event: NativeBufferEvent, address: String): Connection
do
return new Connection(buffer_event)
end
# Listen on the TCP socket at `address`:`port` for new connections
#
# On new connections, libevent callbacks `spawn_connection`.
fun bind_tcp(address: String, port: Int): nullable ConnectionListener
do
var listener = new ConnectionListener.bind_tcp(
event_base, address.to_cstring, port, self)
if listener.address_is_null then
print_error "libevent warning: Opening {address}:{port} failed, " +
evutil_socket_error_to_string(evutil_socket_error).to_s
return null
end
return listener
end
# Listen on a UNIX domain socket for new connections
#
# On new connections, libevent callbacks `spawn_connection`.
fun bind_unix(path: String): nullable ConnectionListener
do
# Delete the socket if it already exists
var stat = path.file_stat
if stat != null and stat.is_sock then path.file_delete
var listener = new ConnectionListener.bind_unix(
event_base, path.to_cstring, self)
if listener.address_is_null then
print_error "libevent warning: Opening UNIX domain socket {path} failed, " +
evutil_socket_error_to_string(evutil_socket_error).to_s
return null
end
return listener
end
# Put a human readable string representation of `address` into `buf`
private fun addrin_to_address(address: Pointer, buf: CString, buf_len: Int): CString `{
struct sockaddr *addrin = (struct sockaddr*)address;
if (addrin->sa_family == AF_INET) {
struct in_addr *src = &((struct sockaddr_in*)addrin)->sin_addr;
return (char *)inet_ntop(addrin->sa_family, src, buf, buf_len);
}
else if (addrin->sa_family == AF_INET6) {
struct in6_addr *src = &((struct sockaddr_in6*)addrin)->sin6_addr;
return (char *)inet_ntop(addrin->sa_family, src, buf, buf_len);
}
else if (addrin->sa_family == AF_UNIX) {
struct sockaddr_un *src = (struct sockaddr_un*)addrin;
char *path = src->sun_path;
if (path == NULL) return "Unnamed UNIX domain socket";
if (path[0] == '\0') return "Abstract UNIX domain socket";
return path;
}
return NULL;
`}
end
lib/libevent/libevent.nit:481,1--574,3