Spawned to manage a specific connection

Introduced properties

fun close_requested: Bool

libevent :: Connection :: close_requested

Closing this connection has been requested, but may not yet be closed
protected fun close_requested=(close_requested: Bool)

libevent :: Connection :: close_requested=

Closing this connection has been requested, but may not yet be closed
fun closed: Bool

libevent :: Connection :: closed

This connection is closed
protected fun closed=(closed: Bool)

libevent :: Connection :: closed=

This connection is closed
init defaultinit(native_buffer_event: NativeBufferEvent)

libevent :: Connection :: defaultinit

fun event_callback(events: Int): Bool

libevent :: Connection :: event_callback

Callback method on events: EOF, user-defined timeout and unrecoverable errors
fun force_close

libevent :: Connection :: force_close

Force closing this connection and freeing native_buffer_event
fun native_buffer_event: NativeBufferEvent

libevent :: Connection :: native_buffer_event

The native libevent linked to self
protected fun native_buffer_event=(native_buffer_event: NativeBufferEvent)

libevent :: Connection :: native_buffer_event=

The native libevent linked to self
fun read_callback(content: String)

libevent :: Connection :: read_callback

Callback method when data is available to read
fun write_callback

libevent :: Connection :: write_callback

Callback method on a write event
fun write_file(path: String)

libevent :: Connection :: write_file

Write a file to the connection

Redefined properties

redef type SELF: Connection

libevent $ Connection :: SELF

Type of this instance, automatically specialized in every class
redef fun close

libevent $ Connection :: close

Close this connection if possible, otherwise mark it to be closed later
redef fun write(str: Text)

libevent $ Connection :: write

Write a string to the connection
redef fun write_byte(byte: Int)

libevent $ Connection :: write_byte

Write a single byte
redef fun write_bytes_from_cstring(ns: CString, len: Int)

libevent $ Connection :: write_bytes_from_cstring

Write len bytes from ns

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 big_endian: Bool

binary :: BinaryStream :: big_endian

Use the big-endian convention? otherwise use little-endian.
fun big_endian=(big_endian: Bool)

binary :: BinaryStream :: big_endian=

Use the big-endian convention? otherwise use little-endian.
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.
abstract fun close

core :: Stream :: close

close the stream
fun close_requested: Bool

libevent :: Connection :: close_requested

Closing this connection has been requested, but may not yet be closed
protected fun close_requested=(close_requested: Bool)

libevent :: Connection :: close_requested=

Closing this connection has been requested, but may not yet be closed
fun closed: Bool

libevent :: Connection :: closed

This connection is closed
protected fun closed=(closed: Bool)

libevent :: Connection :: closed=

This connection is closed
fun codec: Codec

core :: Stream :: codec

Codec used to transform raw data to text
fun codec=(c: Codec)

core :: Stream :: codec=

Change the codec for this stream.
init defaultinit(native_buffer_event: NativeBufferEvent)

libevent :: Connection :: defaultinit

fun event_callback(events: Int): Bool

libevent :: Connection :: event_callback

Callback method on events: EOF, user-defined timeout and unrecoverable errors
fun finish

core :: Stream :: finish

Post-work hook.
fun force_close

libevent :: Connection :: force_close

Force closing this connection and freeing native_buffer_event
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.
abstract fun is_writable: Bool

core :: Writer :: is_writable

Can the stream be used to write
fun last_error: nullable IOError

core :: Stream :: last_error

Error produced by the file stream
protected fun last_error=(last_error: nullable IOError)

core :: Stream :: last_error=

Error produced by the file stream
protected fun lookahead: CString

core :: Stream :: lookahead

Lookahead buffer for codecs
protected fun lookahead=(lookahead: CString)

core :: Stream :: lookahead=

Lookahead buffer for codecs
protected fun lookahead_capacity: Int

core :: Stream :: lookahead_capacity

Capacity of the lookahead
protected fun lookahead_capacity=(lookahead_capacity: Int)

core :: Stream :: lookahead_capacity=

Capacity of the lookahead
protected fun lookahead_length: Int

core :: Stream :: lookahead_length

Current occupation of the lookahead
protected fun lookahead_length=(lookahead_length: Int)

core :: Stream :: lookahead_length=

Current occupation of the lookahead
fun native_buffer_event: NativeBufferEvent

libevent :: Connection :: native_buffer_event

The native libevent linked to self
protected fun native_buffer_event=(native_buffer_event: NativeBufferEvent)

libevent :: Connection :: native_buffer_event=

The native libevent linked to self
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 read_callback(content: String)

libevent :: Connection :: read_callback

Callback method when data is available to read
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun serialize_msgpack(value: nullable Serializable, plain: nullable Bool)

core :: Writer :: serialize_msgpack

Serialize value in MessagePack format
protected fun set_codec(codec: Codec)

core :: Stream :: set_codec

Codec used to transform raw data to text
fun start

core :: Stream :: start

Pre-work hook.
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.
abstract fun write(s: Text)

core :: Writer :: write

Write a string
fun write_bits(bits: Bool...)

core :: Writer :: write_bits

Write up to 8 Bool in a byte
fun write_block(text: Text)

core :: Writer :: write_block

Write the length as a 64 bits integer, then the content of text
fun write_bool(value: Bool)

core :: Writer :: write_bool

Write a boolean value on a byte, using 0 for false and 1 for true
protected fun write_buffer: CString

core :: Stream :: write_buffer

Buffer for writing data to a stream
protected fun write_buffer=(write_buffer: CString)

core :: Stream :: write_buffer=

Buffer for writing data to a stream
abstract fun write_byte(value: Int)

core :: Writer :: write_byte

Write a single byte
fun write_bytes(s: Bytes)

core :: Writer :: write_bytes

Write bytes from s
abstract fun write_bytes_from_cstring(ns: CString, len: Int)

core :: Writer :: write_bytes_from_cstring

Write len bytes from ns
fun write_callback

libevent :: Connection :: write_callback

Callback method on a write event
fun write_char(c: Char)

core :: Writer :: write_char

Write a single char
fun write_double(value: Float)

core :: Writer :: write_double

Write a floating point value on 64 bits
fun write_file(path: String)

libevent :: Connection :: write_file

Write a file to the connection
fun write_float(value: Float)

core :: Writer :: write_float

Write a floating point value on 32 bits
fun write_int64(value: Int)

core :: Writer :: write_int64

Write value as a signed integer on 64 bits
fun write_msgpack_array(len: Int)

core :: Writer :: write_msgpack_array

Write an array header for len items in the shortest possible MessagePack array format
fun write_msgpack_array16(len: Int)

core :: Writer :: write_msgpack_array16

Write an array header for len items, max of 0xFFFF items
fun write_msgpack_array32(len: Int)

core :: Writer :: write_msgpack_array32

Write an array header for len items, max of 0xFFFF_FFFF items
fun write_msgpack_bin(data: Bytes)

core :: Writer :: write_msgpack_bin

Write data in the shortest possible MessagePack bin format
fun write_msgpack_bin16(data: Bytes)

core :: Writer :: write_msgpack_bin16

Write data in bin16 format, max of 0xFFFF bytes
fun write_msgpack_bin32(data: Bytes)

core :: Writer :: write_msgpack_bin32

Write data in bin32 format, max of 0xFFFF_FFFF bytes
fun write_msgpack_bin8(data: Bytes)

core :: Writer :: write_msgpack_bin8

Write data in bin8 format, max of 0xFF bytes
fun write_msgpack_bool(bool: Bool)

core :: Writer :: write_msgpack_bool

Write bool in MessagePack format
fun write_msgpack_double(value: Float)

core :: Writer :: write_msgpack_double

Write value as a MessagePack double
fun write_msgpack_ext(typ: Int, bytes: Bytes)

core :: Writer :: write_msgpack_ext

Write an application-specific extension for typ and bytes in the shortest possible MessagePack ext format
fun write_msgpack_ext16(typ: Int, len: Int)

core :: Writer :: write_msgpack_ext16

Write the header for an application-specific extension of len data bytes
fun write_msgpack_ext32(typ: Int, len: Int)

core :: Writer :: write_msgpack_ext32

Write the header for an application-specific extension of len data bytes
fun write_msgpack_ext8(typ: Int, len: Int)

core :: Writer :: write_msgpack_ext8

Write the header for an application-specific extension of len data bytes
fun write_msgpack_fixarray(len: Int)

core :: Writer :: write_msgpack_fixarray

Write an array header for len items, max of 0x0F items
fun write_msgpack_fixext1(typ: Int)

core :: Writer :: write_msgpack_fixext1

Write the header for an application-specific extension of one data byte
fun write_msgpack_fixext16(typ: Int)

core :: Writer :: write_msgpack_fixext16

Write the header for an application-specific extension of 16 data bytes
fun write_msgpack_fixext2(typ: Int)

core :: Writer :: write_msgpack_fixext2

Write the header for an application-specific extension of two data bytes
fun write_msgpack_fixext4(typ: Int)

core :: Writer :: write_msgpack_fixext4

Write the header for an application-specific extension of 4 data bytes
fun write_msgpack_fixext8(typ: Int)

core :: Writer :: write_msgpack_fixext8

Write the header for an application-specific extension of 8 data bytes
fun write_msgpack_fixint(value: Int)

core :: Writer :: write_msgpack_fixint

Write value as a single byte with metadata
fun write_msgpack_fixmap(len: Int)

core :: Writer :: write_msgpack_fixmap

Write a map header for len key/value pairs, max of 0x0F pairs
fun write_msgpack_fixstr(text: Text)

core :: Writer :: write_msgpack_fixstr

Write text in fixstr format, max of 0x1F bytes
fun write_msgpack_float(value: Float)

core :: Writer :: write_msgpack_float

Write value as a MessagePack float (losing precision)
fun write_msgpack_int(value: Int)

core :: Writer :: write_msgpack_int

Write the integer value either as the shortest possible MessagePack int
fun write_msgpack_int16(value: Int)

core :: Writer :: write_msgpack_int16

Write value over two signed bytes, following 1 metadata byte
fun write_msgpack_int32(value: Int)

core :: Writer :: write_msgpack_int32

Write value over 4 signed bytes, following 1 metadata byte
fun write_msgpack_int64(value: Int)

core :: Writer :: write_msgpack_int64

Write value over 8 signed bytes, following 1 metadata byte
fun write_msgpack_int8(value: Int)

core :: Writer :: write_msgpack_int8

Write value over one signed byte, following 1 metadata byte
fun write_msgpack_map(len: Int)

core :: Writer :: write_msgpack_map

Write a map header for len keys/value pairs in the shortest possible MessagePack map format
fun write_msgpack_map16(len: Int)

core :: Writer :: write_msgpack_map16

Write a map header for len key/value pairs, max of 0xFFFF pairs
fun write_msgpack_map32(len: Int)

core :: Writer :: write_msgpack_map32

Write a map header for len key/value pairs, max of 0xFFFF_FFFF pairs
fun write_msgpack_null

core :: Writer :: write_msgpack_null

Write null, or nil, in MessagePack format
fun write_msgpack_str(text: Text)

core :: Writer :: write_msgpack_str

Write text in the shortest possible MessagePack format
fun write_msgpack_str16(text: Text)

core :: Writer :: write_msgpack_str16

Write text in str16 format, max of 0xFFFF bytes
fun write_msgpack_str32(text: Text)

core :: Writer :: write_msgpack_str32

Write text in str32 format, max of 0xFFFF_FFFF bytes
fun write_msgpack_str8(text: Text)

core :: Writer :: write_msgpack_str8

Write text in str8 format, max of 0xFF bytes
fun write_msgpack_uint16(value: Int)

core :: Writer :: write_msgpack_uint16

Write value over two unsigned bytes, following 1 metadata byte
fun write_msgpack_uint32(value: Int)

core :: Writer :: write_msgpack_uint32

Write value over 4 unsigned bytes, following 1 metadata byte
fun write_msgpack_uint64(value: Int)

core :: Writer :: write_msgpack_uint64

Write value over 8 unsigned bytes, following 1 metadata byte
fun write_msgpack_uint8(value: Int)

core :: Writer :: write_msgpack_uint8

Write value over one unsigned byte, following 1 metadata byte
fun write_string(text: Text)

core :: Writer :: write_string

Write text as a null terminated string
package_diagram libevent::Connection Connection core::Writer Writer libevent::Connection->core::Writer binary::BinaryStream BinaryStream core::Writer->binary::BinaryStream ...binary::BinaryStream ... ...binary::BinaryStream->binary::BinaryStream nitcorn::HTTPConnection HTTPConnection nitcorn::HTTPConnection->libevent::Connection libevent::EchoConnection EchoConnection libevent::EchoConnection->libevent::Connection libevent::TestConnection TestConnection libevent::TestConnection->libevent::Connection nitcorn::HttpServer HttpServer nitcorn::HttpServer->nitcorn::HTTPConnection nitcorn::HttpServer... ... nitcorn::HttpServer...->nitcorn::HttpServer

Ancestors

abstract class BinaryStream

binary :: BinaryStream

A stream of binary data
interface Object

core :: Object

The root of the class hierarchy.
abstract class Stream

core :: Stream

Any kind of stream to read/write/both to or from a source

Parents

abstract class Writer

core :: Writer

A Stream that can be written to

Children

class EchoConnection

libevent :: EchoConnection

Connection echoing data received from clients back at them
class HTTPConnection

nitcorn :: HTTPConnection

Connection rebuilding HTTP requests

Descendants

class HttpServer

nitcorn :: HttpServer

A server handling a single connection

Class definitions

libevent $ Connection
# Spawned to manage a specific connection
class Connection
	super Writer

	# Closing this connection has been requested, but may not yet be `closed`
	var close_requested = false

	# This connection is closed
	var closed = false

	# The native libevent linked to `self`
	var native_buffer_event: NativeBufferEvent

	# Close this connection if possible, otherwise mark it to be closed later
	redef fun close
	do
		if closed then return

		var i = native_buffer_event.input_buffer
		var o = native_buffer_event.output_buffer
		if i.length > 0 or o.length > 0 then
			close_requested = true
		else
			force_close
		end
	end

	# Force closing this connection and freeing `native_buffer_event`
	fun force_close
	do
		if closed then return

		native_buffer_event.free
		closed = true
	end

	# Callback method on a write event
	fun write_callback
	do
		if close_requested then close
	end

	private fun read_callback_native(bev: NativeBufferEvent)
	do
		var evbuffer = bev.input_buffer
		var len = evbuffer.length
		var buf = new CString(len)
		evbuffer.remove(buf, len)
		var str = buf.to_s_with_length(len)
		read_callback str
	end

	# Callback method when data is available to read
	fun read_callback(content: String)
	do
		if close_requested then close
	end

	# Callback method on events: EOF, user-defined timeout and unrecoverable errors
	#
	# Returns `true` if the native handles to `self` can be released.
	fun event_callback(events: Int): Bool
	do
		if events & bev_event_error != 0 or events & bev_event_eof != 0 then
			if events & bev_event_error != 0 then
				var sock_err = evutil_socket_error
				# Ignore some normal errors and print the others for debugging
				if sock_err == 110 then
					# Connection timed out (ETIMEDOUT)
				else if sock_err == 104 then
					# Connection reset by peer (ECONNRESET)
				else
					print_error "libevent error event: {evutil_socket_error_to_string(sock_err)} ({sock_err})"
				end
			end
			force_close
			return true
		end

		return false
	end

	# Write a string to the connection
	redef fun write(str)
	do
		if close_requested then return
		native_buffer_event.write(str.to_cstring, str.byte_length)
	end

	redef fun write_byte(byte)
	do
		if close_requested then return
		native_buffer_event.write_byte(byte)
	end

	redef fun write_bytes_from_cstring(ns, len)
	do
		if close_requested then return
		native_buffer_event.write(ns, len)
	end

	# Write a file to the connection
	#
	# If `not path.file_exists`, the method returns.
	fun write_file(path: String)
	do
		if close_requested then return

		var file = new FileReader.open(path)
		if file.last_error != null then
			var error = new IOError("Failed to open file at '{path}'")
			error.cause = file.last_error
			self.last_error = error
			file.close
			return
		end

		var stat = file.file_stat
		if stat == null then
			last_error = new IOError("Failed to stat file at '{path}'")
			file.close
			return
		end

		var err = native_buffer_event.output_buffer.add_file(file.fd, 0, stat.size)
		if err then
			last_error = new IOError("Failed to add file at '{path}'")
			file.close
		end
	end
end
lib/libevent/libevent.nit:150,1--280,3