7 namespace org { namespace ldk { namespace structs {
11 /** An implementation of OnionMessageHandler */
12 public interface OnionMessageHandlerInterface {
13 /**Because much of the lightning network does not yet support forwarding onion messages, we
14 * may need to directly connect to a node which will forward a message for us. In such a case,
15 * this method will return the set of nodes which need connection by node_id and the
16 * corresponding socket addresses where they may accept incoming connections.
18 * Thus, this method should be polled regularly to detect messages await such a direct
21 TwoTuple_PublicKeyCVec_SocketAddressZZ[] get_and_clear_connections_needed();
22 /**Handle an incoming `onion_message` message from the given peer.
24 void handle_onion_message(byte[] peer_node_id, OnionMessage msg);
25 /**Returns the next pending onion message for the peer with the given node id.
27 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
29 OnionMessage next_onion_message_for_peer(byte[] peer_node_id);
30 /**Called when a connection is established with a peer. Can be used to track which peers
31 * advertise onion message support and are online.
33 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
34 * with us. Implementors should be somewhat conservative about doing so, however, as other
35 * message handlers may still wish to communicate with this peer.
37 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);
42 /**Performs actions that should happen roughly every ten seconds after startup. Allows handlers
43 * to drop any buffered onion messages intended for prospective peers.
45 void timer_tick_occurred();
46 /**Gets the node feature flags which this handler itself supports. All available handlers are
47 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
48 * which are broadcasted in our [`NodeAnnouncement`] message.
50 NodeFeatures provided_node_features();
51 /**Gets the init feature flags which should be sent to the given peer. All available handlers
52 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
53 * which are sent in our [`Init`] message.
55 * Note that this method is called before [`Self::peer_connected`].
57 InitFeatures provided_init_features(byte[] their_node_id);
61 * A handler for received [`OnionMessage`]s and for providing generated ones to send.
63 public class OnionMessageHandler : CommonBase {
64 internal bindings.LDKOnionMessageHandler bindings_instance;
65 internal long instance_idx;
67 internal OnionMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
68 ~OnionMessageHandler() {
69 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); }
72 private class LDKOnionMessageHandlerHolder { internal OnionMessageHandler held; }
73 private class LDKOnionMessageHandlerImpl : bindings.LDKOnionMessageHandler {
74 internal LDKOnionMessageHandlerImpl(OnionMessageHandlerInterface arg, LDKOnionMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
75 private OnionMessageHandlerInterface arg;
76 private LDKOnionMessageHandlerHolder impl_holder;
77 public long get_and_clear_connections_needed() {
78 TwoTuple_PublicKeyCVec_SocketAddressZZ[] ret = arg.get_and_clear_connections_needed();
80 long result = InternalUtils.encodeUint64Array(InternalUtils.mapArray(ret, ret_conv_40 => ret_conv_40 == null ? 0 : ret_conv_40.clone_ptr()));
83 public void handle_onion_message(long _peer_node_id, long _msg) {
84 byte[] _peer_node_id_conv = InternalUtils.decodeUint8Array(_peer_node_id);
85 org.ldk.structs.OnionMessage _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.OnionMessage(null, _msg); }
86 arg.handle_onion_message(_peer_node_id_conv, _msg_hu_conv);
89 public long next_onion_message_for_peer(long _peer_node_id) {
90 byte[] _peer_node_id_conv = InternalUtils.decodeUint8Array(_peer_node_id);
91 OnionMessage ret = arg.next_onion_message_for_peer(_peer_node_id_conv);
93 long result = ret == null ? 0 : ret.clone_ptr();
96 public long peer_connected(long _their_node_id, long _init, bool _inbound) {
97 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
98 org.ldk.structs.Init _init_hu_conv = null; if (_init < 0 || _init > 4096) { _init_hu_conv = new org.ldk.structs.Init(null, _init); }
99 Result_NoneNoneZ ret = arg.peer_connected(_their_node_id_conv, _init_hu_conv, _inbound);
101 long result = ret == null ? 0 : ret.clone_ptr();
104 public void peer_disconnected(long _their_node_id) {
105 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
106 arg.peer_disconnected(_their_node_id_conv);
109 public void timer_tick_occurred() {
110 arg.timer_tick_occurred();
113 public long provided_node_features() {
114 NodeFeatures ret = arg.provided_node_features();
116 long result = ret == null ? 0 : ret.clone_ptr();
119 public long provided_init_features(long _their_node_id) {
120 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
121 InitFeatures ret = arg.provided_init_features(_their_node_id_conv);
123 long result = ret == null ? 0 : ret.clone_ptr();
128 /** Creates a new instance of OnionMessageHandler from a given implementation */
129 public static OnionMessageHandler new_impl(OnionMessageHandlerInterface arg) {
130 LDKOnionMessageHandlerHolder impl_holder = new LDKOnionMessageHandlerHolder();
131 LDKOnionMessageHandlerImpl impl = new LDKOnionMessageHandlerImpl(arg, impl_holder);
132 long[] ptr_idx = bindings.LDKOnionMessageHandler_new(impl);
134 impl_holder.held = new OnionMessageHandler(null, ptr_idx[0]);
135 impl_holder.held.instance_idx = ptr_idx[1];
136 impl_holder.held.bindings_instance = impl;
137 return impl_holder.held;
141 * Because much of the lightning network does not yet support forwarding onion messages, we
142 * may need to directly connect to a node which will forward a message for us. In such a case,
143 * this method will return the set of nodes which need connection by node_id and the
144 * corresponding socket addresses where they may accept incoming connections.
146 * Thus, this method should be polled regularly to detect messages await such a direct
149 public TwoTuple_PublicKeyCVec_SocketAddressZZ[] get_and_clear_connections_needed() {
150 long ret = bindings.OnionMessageHandler_get_and_clear_connections_needed(this.ptr);
152 if (ret >= 0 && ret <= 4096) { return null; }
153 int ret_conv_40_len = InternalUtils.getArrayLength(ret);
154 TwoTuple_PublicKeyCVec_SocketAddressZZ[] ret_conv_40_arr = new TwoTuple_PublicKeyCVec_SocketAddressZZ[ret_conv_40_len];
155 for (int o = 0; o < ret_conv_40_len; o++) {
156 long ret_conv_40 = InternalUtils.getU64ArrayElem(ret, o);
157 TwoTuple_PublicKeyCVec_SocketAddressZZ ret_conv_40_hu_conv = new TwoTuple_PublicKeyCVec_SocketAddressZZ(null, ret_conv_40);
158 if (ret_conv_40_hu_conv != null) { ret_conv_40_hu_conv.ptrs_to.AddLast(this); };
159 ret_conv_40_arr[o] = ret_conv_40_hu_conv;
161 bindings.free_buffer(ret);
162 return ret_conv_40_arr;
166 * Handle an incoming `onion_message` message from the given peer.
168 public void handle_onion_message(byte[] peer_node_id, org.ldk.structs.OnionMessage msg) {
169 bindings.OnionMessageHandler_handle_onion_message(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(peer_node_id, 33)), msg == null ? 0 : msg.ptr);
171 GC.KeepAlive(peer_node_id);
173 if (this != null) { this.ptrs_to.AddLast(msg); };
177 * Returns the next pending onion message for the peer with the given node id.
179 * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
181 public OnionMessage next_onion_message_for_peer(byte[] peer_node_id) {
182 long ret = bindings.OnionMessageHandler_next_onion_message_for_peer(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(peer_node_id, 33)));
184 GC.KeepAlive(peer_node_id);
185 if (ret >= 0 && ret <= 4096) { return null; }
186 org.ldk.structs.OnionMessage ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OnionMessage(null, ret); }
187 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
192 * Called when a connection is established with a peer. Can be used to track which peers
193 * advertise onion message support and are online.
195 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
196 * with us. Implementors should be somewhat conservative about doing so, however, as other
197 * message handlers may still wish to communicate with this peer.
199 public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init, bool inbound) {
200 long ret = bindings.OnionMessageHandler_peer_connected(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)), init == null ? 0 : init.ptr, inbound);
202 GC.KeepAlive(their_node_id);
204 GC.KeepAlive(inbound);
205 if (ret >= 0 && ret <= 4096) { return null; }
206 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
207 if (this != null) { this.ptrs_to.AddLast(init); };
212 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
213 * drop and refuse to forward onion messages to this peer.
215 public void peer_disconnected(byte[] their_node_id) {
216 bindings.OnionMessageHandler_peer_disconnected(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)));
218 GC.KeepAlive(their_node_id);
222 * Performs actions that should happen roughly every ten seconds after startup. Allows handlers
223 * to drop any buffered onion messages intended for prospective peers.
225 public void timer_tick_occurred() {
226 bindings.OnionMessageHandler_timer_tick_occurred(this.ptr);
231 * Gets the node feature flags which this handler itself supports. All available handlers are
232 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
233 * which are broadcasted in our [`NodeAnnouncement`] message.
235 public NodeFeatures provided_node_features() {
236 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
238 if (ret >= 0 && ret <= 4096) { return null; }
239 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
240 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
245 * Gets the init feature flags which should be sent to the given peer. All available handlers
246 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
247 * which are sent in our [`Init`] message.
249 * Note that this method is called before [`Self::peer_connected`].
251 public InitFeatures provided_init_features(byte[] their_node_id) {
252 long ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)));
254 GC.KeepAlive(their_node_id);
255 if (ret >= 0 && ret <= 4096) { return null; }
256 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
257 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };