]> git.bitcoin.ninja Git - ldk-java/blob - c_sharp/src/org/ldk/structs/EventsProvider.cs
Update CI references to LDK 0.0.124 drop stale memchr pins
[ldk-java] / c_sharp / src / org / ldk / structs / EventsProvider.cs
1
2 using org.ldk.impl;
3 using org.ldk.enums;
4 using org.ldk.util;
5 using System;
6
7 namespace org { namespace ldk { namespace structs {
8
9
10
11 /** An implementation of EventsProvider */
12 public interface EventsProviderInterface {
13         /**Processes any events generated since the last call using the given event handler.
14          * 
15          * See the trait-level documentation for requirements.
16          */
17         void process_pending_events(EventHandler handler);
18 }
19
20 /**
21  * A trait indicating an object may generate events.
22  * 
23  * Events are processed by passing an [`EventHandler`] to [`process_pending_events`].
24  * 
25  * Implementations of this trait may also feature an async version of event handling, as shown with
26  * [`ChannelManager::process_pending_events_async`] and
27  * [`ChainMonitor::process_pending_events_async`].
28  * 
29  * # Requirements
30  * 
31  * When using this trait, [`process_pending_events`] will call [`handle_event`] for each pending
32  * event since the last invocation.
33  * 
34  * In order to ensure no [`Event`]s are lost, implementors of this trait will persist [`Event`]s
35  * and replay any unhandled events on startup. An [`Event`] is considered handled when
36  * [`process_pending_events`] returns `Ok(())`, thus handlers MUST fully handle [`Event`]s and
37  * persist any relevant changes to disk *before* returning `Ok(())`. In case of an error (e.g.,
38  * persistence failure) implementors should return `Err(ReplayEvent())`, signalling to the
39  * [`EventsProvider`] to replay unhandled events on the next invocation (generally immediately).
40  * Note that some events might not be replayed, please refer to the documentation for
41  * the individual [`Event`] variants for more detail.
42  * 
43  * Further, because an application may crash between an [`Event`] being handled and the
44  * implementor of this trait being re-serialized, [`Event`] handling must be idempotent - in
45  * effect, [`Event`]s may be replayed.
46  * 
47  * Note, handlers may call back into the provider and thus deadlocking must be avoided. Be sure to
48  * consult the provider's documentation on the implication of processing events and how a handler
49  * may safely use the provider (e.g., see [`ChannelManager::process_pending_events`] and
50  * [`ChainMonitor::process_pending_events`]).
51  * 
52  * (C-not implementable) As there is likely no reason for a user to implement this trait on their
53  * own type(s).
54  * 
55  * [`process_pending_events`]: Self::process_pending_events
56  * [`handle_event`]: EventHandler::handle_event
57  * [`ChannelManager::process_pending_events`]: crate::ln::channelmanager::ChannelManager#method.process_pending_events
58  * [`ChainMonitor::process_pending_events`]: crate::chain::chainmonitor::ChainMonitor#method.process_pending_events
59  * [`ChannelManager::process_pending_events_async`]: crate::ln::channelmanager::ChannelManager::process_pending_events_async
60  * [`ChainMonitor::process_pending_events_async`]: crate::chain::chainmonitor::ChainMonitor::process_pending_events_async
61  */
62 public class EventsProvider : CommonBase {
63         internal bindings.LDKEventsProvider bindings_instance;
64         internal long instance_idx;
65
66         internal EventsProvider(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
67         ~EventsProvider() {
68                 if (ptr != 0) { bindings.EventsProvider_free(ptr); }
69         }
70
71         private class LDKEventsProviderHolder { internal EventsProvider held; }
72         private class LDKEventsProviderImpl : bindings.LDKEventsProvider {
73                 internal LDKEventsProviderImpl(EventsProviderInterface arg, LDKEventsProviderHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
74                 private EventsProviderInterface arg;
75                 private LDKEventsProviderHolder impl_holder;
76                 public void process_pending_events(long _handler) {
77                         EventHandler ret_hu_conv = new EventHandler(null, _handler);
78                         if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
79                         arg.process_pending_events(ret_hu_conv);
80                                 GC.KeepAlive(arg);
81                 }
82         }
83
84         /** Creates a new instance of EventsProvider from a given implementation */
85         public static EventsProvider new_impl(EventsProviderInterface arg) {
86                 LDKEventsProviderHolder impl_holder = new LDKEventsProviderHolder();
87                 LDKEventsProviderImpl impl = new LDKEventsProviderImpl(arg, impl_holder);
88                 long[] ptr_idx = bindings.LDKEventsProvider_new(impl);
89
90                 impl_holder.held = new EventsProvider(null, ptr_idx[0]);
91                 impl_holder.held.instance_idx = ptr_idx[1];
92                 impl_holder.held.bindings_instance = impl;
93                 return impl_holder.held;
94         }
95
96         /**
97          * Processes any events generated since the last call using the given event handler.
98          * 
99          * See the trait-level documentation for requirements.
100          */
101         public void process_pending_events(org.ldk.structs.EventHandler handler) {
102                 bindings.EventsProvider_process_pending_events(this.ptr, handler.ptr);
103                 GC.KeepAlive(this);
104                 GC.KeepAlive(handler);
105                 if (this != null) { this.ptrs_to.AddLast(handler); };
106         }
107
108 }
109 } } }