6 namespace org { namespace ldk { namespace structs {
9 * A trait indicating an object may generate events.
11 * Events are processed by passing an [`EventHandler`] to [`process_pending_events`].
13 * Implementations of this trait may also feature an async version of event handling, as shown with
14 * [`ChannelManager::process_pending_events_async`] and
15 * [`ChainMonitor::process_pending_events_async`].
19 * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
20 * event since the last invocation.
22 * In order to ensure no [`Event`]s are lost, implementors of this trait will persist [`Event`]s
23 * and replay any unhandled events on startup. An [`Event`] is considered handled when
24 * [`process_pending_events`] returns, thus handlers MUST fully handle [`Event`]s and persist any
25 * relevant changes to disk *before* returning.
27 * Further, because an application may crash between an [`Event`] being handled and the
28 * implementor of this trait being re-serialized, [`Event`] handling must be idempotent - in
29 * effect, [`Event`]s may be replayed.
31 * Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
32 * consult the provider's documentation on the implication of processing events and how a handler
33 * may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and
34 * [`ChainMonitor::process_pending_events`]).
36 * (C-not implementable) As there is likely no reason for a user to implement this trait on their
39 * [`process_pending_events`]: Self::process_pending_events
40 * [`handle_event`]: EventHandler::handle_event
41 * [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events
42 * [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events
43 * [`ChannelManager::process_pending_events_async`]: crate::ln::channelmanager::ChannelManager::process_pending_events_async
44 * [`ChainMonitor::process_pending_events_async`]: crate::chain::chainmonitor::ChainMonitor::process_pending_events_async
46 public class EventsProvider : CommonBase {
47 internal readonly bindings.LDKEventsProvider bindings_instance;
48 internal EventsProvider(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
49 private EventsProvider(bindings.LDKEventsProvider arg) : base(bindings.LDKEventsProvider_new(arg)) {
50 this.ptrs_to.AddLast(arg);
51 this.bindings_instance = arg;
54 if (ptr != 0) { bindings.EventsProvider_free(ptr); }
57 public interface EventsProviderInterface {
59 * Processes any events generated since the last call using the given event handler.
61 * See the trait-level documentation for requirements.
63 void process_pending_events(EventHandler _handler);
65 private class LDKEventsProviderHolder { internal EventsProvider held; }
66 private class LDKEventsProviderImpl : bindings.LDKEventsProvider {
67 internal LDKEventsProviderImpl(EventsProviderInterface arg, LDKEventsProviderHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
68 private EventsProviderInterface arg;
69 private LDKEventsProviderHolder impl_holder;
70 public void process_pending_events(long _handler) {
71 EventHandler ret_hu_conv = new EventHandler(null, _handler);
72 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
73 arg.process_pending_events(ret_hu_conv);
77 public static EventsProvider new_impl(EventsProviderInterface arg) {
78 LDKEventsProviderHolder impl_holder = new LDKEventsProviderHolder();
79 impl_holder.held = new EventsProvider(new LDKEventsProviderImpl(arg, impl_holder));
80 return impl_holder.held;
83 * Processes any events generated since the last call using the given event handler.
85 * See the trait-level documentation for requirements.
87 public void process_pending_events(org.ldk.structs.EventHandler handler) {
88 bindings.EventsProvider_process_pending_events(this.ptr, handler == null ? 0 : handler.ptr);
90 GC.KeepAlive(handler);
91 if (this != null) { this.ptrs_to.AddLast(handler); };