Factory to listen on sockets and create new Connection

Introduced properties

fun accept_connection(listener: ConnectionListener, fd: Int, addrin: Pointer, socklen: Int)

libevent :: ConnectionFactory :: accept_connection

Accept a connection on listener
fun bind_tcp(address: String, port: Int): nullable ConnectionListener

libevent :: ConnectionFactory :: bind_tcp

Listen on the TCP socket at address:port for new connections
fun bind_unix(path: String): nullable ConnectionListener

libevent :: ConnectionFactory :: bind_unix

Listen on a UNIX domain socket for new connections
fun event_base: NativeEventBase

libevent :: ConnectionFactory :: event_base

The NativeEventBase for the dispatch loop of this factory
protected fun event_base=(event_base: NativeEventBase)

libevent :: ConnectionFactory :: event_base=

The NativeEventBase for the dispatch loop of this factory
fun spawn_connection(buffer_event: NativeBufferEvent, address: String): Connection

libevent :: ConnectionFactory :: spawn_connection

Create a new Connection object for buffer_event

Redefined properties

redef type SELF: ConnectionFactory

libevent $ ConnectionFactory :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun accept_connection(listener: ConnectionListener, fd: Int, addrin: Pointer, socklen: Int)

libevent :: ConnectionFactory :: accept_connection

Accept a connection on listener
fun bind_tcp(address: String, port: Int): nullable ConnectionListener

libevent :: ConnectionFactory :: bind_tcp

Listen on the TCP socket at address:port for new connections
fun bind_unix(path: String): nullable ConnectionListener

libevent :: ConnectionFactory :: bind_unix

Listen on a UNIX domain socket for new connections
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun event_base: NativeEventBase

libevent :: ConnectionFactory :: event_base

The NativeEventBase for the dispatch loop of this factory
protected fun event_base=(event_base: NativeEventBase)

libevent :: ConnectionFactory :: event_base=

The NativeEventBase for the dispatch loop of this factory
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun spawn_connection(buffer_event: NativeBufferEvent, address: String): Connection

libevent :: ConnectionFactory :: spawn_connection

Create a new Connection object for buffer_event
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram libevent::ConnectionFactory ConnectionFactory core::Object Object libevent::ConnectionFactory->core::Object nitcorn::HttpFactory HttpFactory nitcorn::HttpFactory->libevent::ConnectionFactory libevent::MyFactory MyFactory libevent::MyFactory->libevent::ConnectionFactory libevent::TestConnectionFactory TestConnectionFactory libevent::TestConnectionFactory->libevent::ConnectionFactory

Parents

interface Object

core :: Object

The root of the class hierarchy.

Children

class HttpFactory

nitcorn :: HttpFactory

Factory to create HttpServer instances, and hold the libevent base handler
class MyFactory

libevent :: MyFactory

Factory creating instances of EchoConnection to handle new connections

Class definitions

libevent $ ConnectionFactory
# 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