7 namespace org { namespace ldk { namespace structs {
11 /** An implementation of CustomMessageHandler */
12 public interface CustomMessageHandlerInterface {
13 /**Handles the given message sent from `sender_node_id`, possibly producing messages for
14 * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
17 Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id);
18 /**Returns the list of pending messages that were generated by the handler, clearing the list
19 * in the process. Each message is paired with the node id of the intended recipient. If no
20 * connection to the node exists, then the message is simply not sent.
22 TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg();
23 /**Indicates a peer disconnected.
25 void peer_disconnected(byte[] their_node_id);
26 /**Handle a peer connecting.
28 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
29 * with us. Implementors should be somewhat conservative about doing so, however, as other
30 * message handlers may still wish to communicate with this peer.
32 Result_NoneNoneZ peer_connected(byte[] their_node_id, Init msg, bool inbound);
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 * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
39 NodeFeatures provided_node_features();
40 /**Gets the init feature flags which should be sent to the given peer. All available handlers
41 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
42 * which are sent in our [`Init`] message.
44 * [`Init`]: crate::ln::msgs::Init
46 InitFeatures provided_init_features(byte[] their_node_id);
50 * A handler provided to [`PeerManager`] for reading and handling custom messages.
52 * [BOLT 1] specifies a custom message type range for use with experimental or application-specific
53 * messages. `CustomMessageHandler` allows for user-defined handling of such types. See the
54 * [`lightning_custom_message`] crate for tools useful in composing more than one custom handler.
56 * [BOLT 1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
57 * [`lightning_custom_message`]: https://docs.rs/lightning_custom_message/latest/lightning_custom_message
59 public class CustomMessageHandler : CommonBase {
60 internal bindings.LDKCustomMessageHandler bindings_instance;
61 internal long instance_idx;
63 internal CustomMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
64 ~CustomMessageHandler() {
65 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); }
68 private class LDKCustomMessageHandlerHolder { internal CustomMessageHandler held; }
69 private class LDKCustomMessageHandlerImpl : bindings.LDKCustomMessageHandler {
70 internal LDKCustomMessageHandlerImpl(CustomMessageHandlerInterface arg, LDKCustomMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
71 private CustomMessageHandlerInterface arg;
72 private LDKCustomMessageHandlerHolder impl_holder;
73 public long handle_custom_message(long _msg, long _sender_node_id) {
74 Type ret_hu_conv = new Type(null, _msg);
75 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
76 byte[] _sender_node_id_conv = InternalUtils.decodeUint8Array(_sender_node_id);
77 Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, _sender_node_id_conv);
79 long result = ret.clone_ptr();
82 public long get_and_clear_pending_msg() {
83 TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
85 long result = InternalUtils.encodeUint64Array(InternalUtils.mapArray(ret, ret_conv_25 => ret_conv_25.clone_ptr()));
88 public void peer_disconnected(long _their_node_id) {
89 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
90 arg.peer_disconnected(_their_node_id_conv);
93 public long peer_connected(long _their_node_id, long _msg, bool _inbound) {
94 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
95 org.ldk.structs.Init _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.Init(null, _msg); }
96 Result_NoneNoneZ ret = arg.peer_connected(_their_node_id_conv, _msg_hu_conv, _inbound);
98 long result = ret.clone_ptr();
101 public long provided_node_features() {
102 NodeFeatures ret = arg.provided_node_features();
104 long result = ret.clone_ptr();
107 public long provided_init_features(long _their_node_id) {
108 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
109 InitFeatures ret = arg.provided_init_features(_their_node_id_conv);
111 long result = ret.clone_ptr();
116 /** Creates a new instance of CustomMessageHandler from a given implementation */
117 public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReaderInterface customMessageReader_impl) {
118 LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder();
119 LDKCustomMessageHandlerImpl impl = new LDKCustomMessageHandlerImpl(arg, impl_holder);
120 CustomMessageReader customMessageReader = CustomMessageReader.new_impl(customMessageReader_impl);
121 long[] ptr_idx = bindings.LDKCustomMessageHandler_new(impl, customMessageReader.instance_idx);
123 impl_holder.held = new CustomMessageHandler(null, ptr_idx[0]);
124 impl_holder.held.instance_idx = ptr_idx[1];
125 impl_holder.held.bindings_instance = impl;
126 impl_holder.held.ptrs_to.AddLast(customMessageReader);
127 return impl_holder.held;
131 * Handles the given message sent from `sender_node_id`, possibly producing messages for
132 * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
135 public Result_NoneLightningErrorZ handle_custom_message(org.ldk.structs.Type msg, byte[] sender_node_id) {
136 long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(sender_node_id, 33)));
139 GC.KeepAlive(sender_node_id);
140 if (ret >= 0 && ret <= 4096) { return null; }
141 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
142 if (this != null) { this.ptrs_to.AddLast(msg); };
147 * Returns the list of pending messages that were generated by the handler, clearing the list
148 * in the process. Each message is paired with the node id of the intended recipient. If no
149 * connection to the node exists, then the message is simply not sent.
151 public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
152 long ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
154 if (ret >= 0 && ret <= 4096) { return null; }
155 int ret_conv_25_len = InternalUtils.getArrayLength(ret);
156 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len];
157 for (int z = 0; z < ret_conv_25_len; z++) {
158 long ret_conv_25 = InternalUtils.getU64ArrayElem(ret, z);
159 TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
160 if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.AddLast(this); };
161 ret_conv_25_arr[z] = ret_conv_25_hu_conv;
163 bindings.free_buffer(ret);
164 return ret_conv_25_arr;
168 * Indicates a peer disconnected.
170 public void peer_disconnected(byte[] their_node_id) {
171 bindings.CustomMessageHandler_peer_disconnected(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)));
173 GC.KeepAlive(their_node_id);
177 * Handle a peer connecting.
179 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
180 * with us. Implementors should be somewhat conservative about doing so, however, as other
181 * message handlers may still wish to communicate with this peer.
183 public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init msg, bool inbound) {
184 long ret = bindings.CustomMessageHandler_peer_connected(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)), msg.ptr, inbound);
186 GC.KeepAlive(their_node_id);
188 GC.KeepAlive(inbound);
189 if (ret >= 0 && ret <= 4096) { return null; }
190 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
191 if (this != null) { this.ptrs_to.AddLast(msg); };
196 * Gets the node feature flags which this handler itself supports. All available handlers are
197 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
198 * which are broadcasted in our [`NodeAnnouncement`] message.
200 * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
202 public NodeFeatures provided_node_features() {
203 long ret = bindings.CustomMessageHandler_provided_node_features(this.ptr);
205 if (ret >= 0 && ret <= 4096) { return null; }
206 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
207 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
212 * Gets the init feature flags which should be sent to the given peer. All available handlers
213 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
214 * which are sent in our [`Init`] message.
216 * [`Init`]: crate::ln::msgs::Init
218 public InitFeatures provided_init_features(byte[] their_node_id) {
219 long ret = bindings.CustomMessageHandler_provided_init_features(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)));
221 GC.KeepAlive(their_node_id);
222 if (ret >= 0 && ret <= 4096) { return null; }
223 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
224 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };