6 namespace org { namespace ldk { namespace structs {
9 * A trait to describe an object that can receive onion messages.
11 public class OnionMessageHandler : CommonBase {
12 internal readonly bindings.LDKOnionMessageHandler bindings_instance;
13 internal OnionMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
14 private OnionMessageHandler(bindings.LDKOnionMessageHandler arg, bindings.LDKOnionMessageProvider OnionMessageProvider) : base(bindings.LDKOnionMessageHandler_new(arg, OnionMessageProvider)) {
15 this.ptrs_to.AddLast(arg);
16 this.ptrs_to.AddLast(OnionMessageProvider);
17 this.bindings_instance = arg;
19 ~OnionMessageHandler() {
20 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); }
23 public interface OnionMessageHandlerInterface {
25 * Handle an incoming `onion_message` message from the given peer.
27 void handle_onion_message(byte[] _peer_node_id, OnionMessage _msg);
29 * Called when a connection is established with a peer. Can be used to track which peers
30 * advertise onion message support and are online.
32 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
33 * with us. Implementors should be somewhat conservative about doing so, however, as other
34 * message handlers may still wish to communicate with this peer.
36 Result_NoneNoneZ peer_connected(byte[] _their_node_id, Init _init, bool _inbound);
38 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
39 * drop and refuse to forward onion messages to this peer.
41 void peer_disconnected(byte[] _their_node_id);
43 * Gets the node feature flags which this handler itself supports. All available handlers are
44 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
45 * which are broadcasted in our [`NodeAnnouncement`] message.
47 NodeFeatures provided_node_features();
49 * Gets the init feature flags which should be sent to the given peer. All available handlers
50 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
51 * which are sent in our [`Init`] message.
53 * Note that this method is called before [`Self::peer_connected`].
55 InitFeatures provided_init_features(byte[] _their_node_id);
57 private class LDKOnionMessageHandlerHolder { internal OnionMessageHandler held; }
58 private class LDKOnionMessageHandlerImpl : bindings.LDKOnionMessageHandler {
59 internal LDKOnionMessageHandlerImpl(OnionMessageHandlerInterface arg, LDKOnionMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
60 private OnionMessageHandlerInterface arg;
61 private LDKOnionMessageHandlerHolder impl_holder;
62 public void handle_onion_message(byte[] _peer_node_id, long _msg) {
63 org.ldk.structs.OnionMessage _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.OnionMessage(null, _msg); }
64 arg.handle_onion_message(_peer_node_id, _msg_hu_conv);
67 public long peer_connected(byte[] _their_node_id, long _init, bool _inbound) {
68 org.ldk.structs.Init _init_hu_conv = null; if (_init < 0 || _init > 4096) { _init_hu_conv = new org.ldk.structs.Init(null, _init); }
69 Result_NoneNoneZ ret = arg.peer_connected(_their_node_id, _init_hu_conv, _inbound);
71 long result = ret == null ? 0 : ret.clone_ptr();
74 public void peer_disconnected(byte[] _their_node_id) {
75 arg.peer_disconnected(_their_node_id);
78 public long provided_node_features() {
79 NodeFeatures ret = arg.provided_node_features();
81 long result = ret == null ? 0 : ret.clone_ptr();
84 public long provided_init_features(byte[] _their_node_id) {
85 InitFeatures ret = arg.provided_init_features(_their_node_id);
87 long result = ret == null ? 0 : ret.clone_ptr();
91 public static OnionMessageHandler new_impl(OnionMessageHandlerInterface arg, OnionMessageProvider.OnionMessageProviderInterface OnionMessageProvider_impl) {
92 LDKOnionMessageHandlerHolder impl_holder = new LDKOnionMessageHandlerHolder();
93 impl_holder.held = new OnionMessageHandler(new LDKOnionMessageHandlerImpl(arg, impl_holder), OnionMessageProvider.new_impl(OnionMessageProvider_impl).bindings_instance);
94 return impl_holder.held;
98 * Gets the underlying OnionMessageProvider.
100 public OnionMessageProvider get_onion_message_provider() {
101 OnionMessageProvider res = new OnionMessageProvider(null, bindings.LDKOnionMessageHandler_get_OnionMessageProvider(this.ptr));
102 this.ptrs_to.AddLast(res);
107 * Handle an incoming `onion_message` message from the given peer.
109 public void handle_onion_message(byte[] peer_node_id, org.ldk.structs.OnionMessage msg) {
110 bindings.OnionMessageHandler_handle_onion_message(this.ptr, InternalUtils.check_arr_len(peer_node_id, 33), msg == null ? 0 : msg.ptr);
112 GC.KeepAlive(peer_node_id);
114 if (this != null) { this.ptrs_to.AddLast(msg); };
118 * Called when a connection is established with a peer. Can be used to track which peers
119 * advertise onion message support and are online.
121 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
122 * with us. Implementors should be somewhat conservative about doing so, however, as other
123 * message handlers may still wish to communicate with this peer.
125 public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init, bool inbound) {
126 long ret = bindings.OnionMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr, inbound);
128 GC.KeepAlive(their_node_id);
130 GC.KeepAlive(inbound);
131 if (ret >= 0 && ret <= 4096) { return null; }
132 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
133 if (this != null) { this.ptrs_to.AddLast(init); };
138 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
139 * drop and refuse to forward onion messages to this peer.
141 public void peer_disconnected(byte[] their_node_id) {
142 bindings.OnionMessageHandler_peer_disconnected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
144 GC.KeepAlive(their_node_id);
148 * Gets the node feature flags which this handler itself supports. All available handlers are
149 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
150 * which are broadcasted in our [`NodeAnnouncement`] message.
152 public NodeFeatures provided_node_features() {
153 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
155 if (ret >= 0 && ret <= 4096) { return null; }
156 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
157 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
162 * Gets the init feature flags which should be sent to the given peer. All available handlers
163 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
164 * which are sent in our [`Init`] message.
166 * Note that this method is called before [`Self::peer_connected`].
168 public InitFeatures provided_init_features(byte[] their_node_id) {
169 long ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
171 GC.KeepAlive(their_node_id);
172 if (ret >= 0 && ret <= 4096) { return null; }
173 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
174 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };