X-Git-Url: http://nitlanguage.org diff --git a/lib/libevent.nit b/lib/libevent.nit index 8c65323..2dfb046 100644 --- a/lib/libevent.nit +++ b/lib/libevent.nit @@ -67,6 +67,14 @@ in "C" `{ } #endif +#ifdef EventCallback_incr_ref + // Callback forwarded to 'EventCallback.callback' + static void signal_cb(evutil_socket_t fd, short events, void *data) + { + EventCallback handler = data; + EventCallback_callback(handler, events); + } +#endif `} # Structure to hold information and state for a Libevent dispatch loop. @@ -82,19 +90,58 @@ extern class NativeEventBase `{ struct event_base * `} # Has `self` been correctly initialized? fun is_valid: Bool do return not address_is_null + # Reinitialize the event base after a fork + # + # Some event mechanisms do not survive across fork. + # The event base needs to be reinitialized with the `reinit` method. + # + # Returns `true` if some events could not be re-added. + fun reinit: Bool `{ return event_reinit(self); `} + # Event dispatching loop # # This loop will run the event base until either there are no more added - # events, or until something calls `exit_loop`. + # events, or until something calls `loopexit`. fun dispatch `{ event_base_dispatch(self); `} # Exit the event loop # # TODO support timer - fun exit_loop `{ event_base_loopexit(self, NULL); `} + fun loopexit `{ event_base_loopexit(self, NULL); `} + + redef fun free `{ event_base_free(self); `} +end + +# Event, libevent's basic unit of operation +extern class NativeEvent `{ struct event * `} + + # Add to the set of pending events + # + # TODO support timeout + fun add `{ event_add(self, NULL); `} + + # Remove from the set of monitored events + fun del `{ event_del(self); `} + + redef fun free `{ event_free(self); `} +end + +# Signal event +extern class NativeEvSignal + super NativeEvent - # Destroy this instance - fun destroy `{ event_base_free(self); `} + new (base: NativeEventBase, signal: Int, handler: EventCallback) + import EventCallback.callback `{ + EventCallback_incr_ref(handler); + return evsignal_new(base, signal, signal_cb, handler); + `} +end + +# Receiver of event callbacks +interface EventCallback + + # Callback on an event + fun callback(events: Int) do end end # Spawned to manage a specific connection @@ -145,7 +192,7 @@ class Connection do var evbuffer = bev.input_buffer var len = evbuffer.length - var buf = new NativeString(len) + var buf = new CString(len) evbuffer.remove(buf, len) var str = buf.to_s_with_length(len) read_callback str @@ -260,7 +307,7 @@ fun evutil_socket_error: Int `{ `} # Convert an error code from `evutil_socket_error` to a string -fun evutil_socket_error_to_string(error_code: Int): NativeString `{ +fun evutil_socket_error_to_string(error_code: Int): CString `{ return evutil_socket_error_to_string(error_code); `} @@ -305,7 +352,7 @@ extern class NativeBufferEvent `{ struct bufferevent * `} # Set callbacks to `read_callback_native`, `write_callback` and `event_callback` of `conn` fun setcb(conn: Connection) import Connection.read_callback_native, - Connection.write_callback, Connection.event_callback, NativeString `{ + Connection.write_callback, Connection.event_callback, CString `{ Connection_incr_ref(conn); bufferevent_setcb(self, (bufferevent_data_cb)c_read_cb, @@ -314,7 +361,7 @@ extern class NativeBufferEvent `{ struct bufferevent * `} `} # Write `length` bytes of `line` - fun write(line: NativeString, length: Int): Int `{ + fun write(line: CString, length: Int): Int `{ return bufferevent_write(self, line, length); `} @@ -345,7 +392,7 @@ extern class NativeEvBuffer `{ struct evbuffer * `} fun length: Int `{ return evbuffer_get_length(self); `} # Read data from an evbuffer and drain the bytes read - fun remove(buffer: NativeString, len: Int) `{ + fun remove(buffer: CString, len: Int) `{ evbuffer_remove(self, buffer, len); `} end @@ -371,7 +418,7 @@ end # A listener acting on an interface and port, spawns `Connection` on new connections extern class ConnectionListener `{ struct evconnlistener * `} - private new bind_to(base: NativeEventBase, address: NativeString, port: Int, factory: ConnectionFactory) + private new bind_to(base: NativeEventBase, address: CString, port: Int, factory: ConnectionFactory) import ConnectionFactory.accept_connection, error_callback `{ struct sockaddr_in sin; @@ -426,7 +473,7 @@ class ConnectionFactory # Human representation of remote client address var addr_len = 46 # Longest possible IPv6 address + null byte - var addr_buf = new NativeString(addr_len) + 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" @@ -454,7 +501,7 @@ class ConnectionFactory end # Put string representation of source `address` into `buf` - private fun addrin_to_address(address: Pointer, buf: NativeString, buf_len: Int): NativeString `{ + 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) { @@ -468,3 +515,24 @@ class ConnectionFactory return NULL; `} end + +# Enable some relatively expensive debugging checks that would normally be turned off +fun enable_debug_mode `{ event_enable_debug_mode(); `} + +# Use Windows builtin locking and thread ID functions +fun use_windows_threads: Bool `{ +#ifdef EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED + return evthread_use_windows_threads(); +#else + return -1; +#endif +`} + +# Use Pthreads locking and thread ID functions +fun use_pthreads: Bool `{ +#ifdef EVTHREAD_USE_PTHREADS_IMPLEMENTED + return evthread_use_pthreads(); +#else + return -1; +#endif +`}