7 namespace org { namespace ldk { namespace structs {
11 /** An implementation of OnionMessageHandler */
12 public interface OnionMessageHandlerInterface {
13 /**Handle an incoming `onion_message` message from the given peer.
15 void handle_onion_message(byte[] peer_node_id, OnionMessage msg);
16 /**Returns the next pending onion message for the peer with the given node id.
18 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
20 OnionMessage next_onion_message_for_peer(byte[] peer_node_id);
21 /**Called when a connection is established with a peer. Can be used to track which peers
22 * advertise onion message support and are online.
24 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
25 * with us. Implementors should be somewhat conservative about doing so, however, as other
26 * message handlers may still wish to communicate with this peer.
28 Result_NoneNoneZ peer_connected(byte[] their_node_id, Init init, bool inbound);
29 /**Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
30 * drop and refuse to forward onion messages to this peer.
32 void peer_disconnected(byte[] their_node_id);
33 /**Gets the node feature flags which this handler itself supports. All available handlers are
34 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
35 * which are broadcasted in our [`NodeAnnouncement`] message.
37 NodeFeatures provided_node_features();
38 /**Gets the init feature flags which should be sent to the given peer. All available handlers
39 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
40 * which are sent in our [`Init`] message.
42 * Note that this method is called before [`Self::peer_connected`].
44 InitFeatures provided_init_features(byte[] their_node_id);
48 * A handler for received [`OnionMessage`]s and for providing generated ones to send.
50 public class OnionMessageHandler : CommonBase {
51 internal bindings.LDKOnionMessageHandler bindings_instance;
52 internal long instance_idx;
54 internal OnionMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
55 ~OnionMessageHandler() {
56 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); }
59 private class LDKOnionMessageHandlerHolder { internal OnionMessageHandler held; }
60 private class LDKOnionMessageHandlerImpl : bindings.LDKOnionMessageHandler {
61 internal LDKOnionMessageHandlerImpl(OnionMessageHandlerInterface arg, LDKOnionMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
62 private OnionMessageHandlerInterface arg;
63 private LDKOnionMessageHandlerHolder impl_holder;
64 public void handle_onion_message(long _peer_node_id, long _msg) {
65 byte[] _peer_node_id_conv = InternalUtils.decodeUint8Array(_peer_node_id);
66 org.ldk.structs.OnionMessage _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.OnionMessage(null, _msg); }
67 arg.handle_onion_message(_peer_node_id_conv, _msg_hu_conv);
70 public long next_onion_message_for_peer(long _peer_node_id) {
71 byte[] _peer_node_id_conv = InternalUtils.decodeUint8Array(_peer_node_id);
72 OnionMessage ret = arg.next_onion_message_for_peer(_peer_node_id_conv);
74 long result = ret == null ? 0 : ret.clone_ptr();
77 public long peer_connected(long _their_node_id, long _init, bool _inbound) {
78 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
79 org.ldk.structs.Init _init_hu_conv = null; if (_init < 0 || _init > 4096) { _init_hu_conv = new org.ldk.structs.Init(null, _init); }
80 Result_NoneNoneZ ret = arg.peer_connected(_their_node_id_conv, _init_hu_conv, _inbound);
82 long result = ret == null ? 0 : ret.clone_ptr();
85 public void peer_disconnected(long _their_node_id) {
86 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
87 arg.peer_disconnected(_their_node_id_conv);
90 public long provided_node_features() {
91 NodeFeatures ret = arg.provided_node_features();
93 long result = ret == null ? 0 : ret.clone_ptr();
96 public long provided_init_features(long _their_node_id) {
97 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
98 InitFeatures ret = arg.provided_init_features(_their_node_id_conv);
100 long result = ret == null ? 0 : ret.clone_ptr();
105 /** Creates a new instance of OnionMessageHandler from a given implementation */
106 public static OnionMessageHandler new_impl(OnionMessageHandlerInterface arg) {
107 LDKOnionMessageHandlerHolder impl_holder = new LDKOnionMessageHandlerHolder();
108 LDKOnionMessageHandlerImpl impl = new LDKOnionMessageHandlerImpl(arg, impl_holder);
109 long[] ptr_idx = bindings.LDKOnionMessageHandler_new(impl);
111 impl_holder.held = new OnionMessageHandler(null, ptr_idx[0]);
112 impl_holder.held.instance_idx = ptr_idx[1];
113 impl_holder.held.bindings_instance = impl;
114 return impl_holder.held;
118 * Handle an incoming `onion_message` message from the given peer.
120 public void handle_onion_message(byte[] peer_node_id, org.ldk.structs.OnionMessage msg) {
121 bindings.OnionMessageHandler_handle_onion_message(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(peer_node_id, 33)), msg == null ? 0 : msg.ptr);
123 GC.KeepAlive(peer_node_id);
125 if (this != null) { this.ptrs_to.AddLast(msg); };
129 * Returns the next pending onion message for the peer with the given node id.
131 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
133 public OnionMessage next_onion_message_for_peer(byte[] peer_node_id) {
134 long ret = bindings.OnionMessageHandler_next_onion_message_for_peer(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(peer_node_id, 33)));
136 GC.KeepAlive(peer_node_id);
137 if (ret >= 0 && ret <= 4096) { return null; }
138 org.ldk.structs.OnionMessage ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessage(null, ret); }
139 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
144 * Called when a connection is established with a peer. Can be used to track which peers
145 * advertise onion message support and are online.
147 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
148 * with us. Implementors should be somewhat conservative about doing so, however, as other
149 * message handlers may still wish to communicate with this peer.
151 public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init, bool inbound) {
152 long ret = bindings.OnionMessageHandler_peer_connected(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)), init == null ? 0 : init.ptr, inbound);
154 GC.KeepAlive(their_node_id);
156 GC.KeepAlive(inbound);
157 if (ret >= 0 && ret <= 4096) { return null; }
158 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
159 if (this != null) { this.ptrs_to.AddLast(init); };
164 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
165 * drop and refuse to forward onion messages to this peer.
167 public void peer_disconnected(byte[] their_node_id) {
168 bindings.OnionMessageHandler_peer_disconnected(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)));
170 GC.KeepAlive(their_node_id);
174 * Gets the node feature flags which this handler itself supports. All available handlers are
175 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
176 * which are broadcasted in our [`NodeAnnouncement`] message.
178 public NodeFeatures provided_node_features() {
179 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
181 if (ret >= 0 && ret <= 4096) { return null; }
182 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
183 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
188 * Gets the init feature flags which should be sent to the given peer. All available handlers
189 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
190 * which are sent in our [`Init`] message.
192 * Note that this method is called before [`Self::peer_connected`].
194 public InitFeatures provided_init_features(byte[] their_node_id) {
195 long ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)));
197 GC.KeepAlive(their_node_id);
198 if (ret >= 0 && ret <= 4096) { return null; }
199 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
200 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };