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 /**Gets the node feature flags which this handler itself supports. All available handlers are
24 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
25 * which are broadcasted in our [`NodeAnnouncement`] message.
27 * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
29 NodeFeatures provided_node_features();
30 /**Gets the init feature flags which should be sent to the given peer. All available handlers
31 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
32 * which are sent in our [`Init`] message.
34 * [`Init`]: crate::ln::msgs::Init
36 InitFeatures provided_init_features(byte[] their_node_id);
40 * A handler provided to [`PeerManager`] for reading and handling custom messages.
42 * [BOLT 1] specifies a custom message type range for use with experimental or application-specific
43 * messages. `CustomMessageHandler` allows for user-defined handling of such types. See the
44 * [`lightning_custom_message`] crate for tools useful in composing more than one custom handler.
46 * [BOLT 1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
47 * [`lightning_custom_message`]: https://docs.rs/lightning_custom_message/latest/lightning_custom_message
49 public class CustomMessageHandler : CommonBase {
50 internal bindings.LDKCustomMessageHandler bindings_instance;
51 internal long instance_idx;
53 internal CustomMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
54 ~CustomMessageHandler() {
55 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); }
58 private class LDKCustomMessageHandlerHolder { internal CustomMessageHandler held; }
59 private class LDKCustomMessageHandlerImpl : bindings.LDKCustomMessageHandler {
60 internal LDKCustomMessageHandlerImpl(CustomMessageHandlerInterface arg, LDKCustomMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
61 private CustomMessageHandlerInterface arg;
62 private LDKCustomMessageHandlerHolder impl_holder;
63 public long handle_custom_message(long _msg, long _sender_node_id) {
64 Type ret_hu_conv = new Type(null, _msg);
65 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
66 byte[] _sender_node_id_conv = InternalUtils.decodeUint8Array(_sender_node_id);
67 Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, _sender_node_id_conv);
69 long result = ret.clone_ptr();
72 public long get_and_clear_pending_msg() {
73 TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
75 long result = InternalUtils.encodeUint64Array(InternalUtils.mapArray(ret, ret_conv_25 => ret_conv_25.clone_ptr()));
78 public long provided_node_features() {
79 NodeFeatures ret = arg.provided_node_features();
81 long result = ret.clone_ptr();
84 public long provided_init_features(long _their_node_id) {
85 byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
86 InitFeatures ret = arg.provided_init_features(_their_node_id_conv);
88 long result = ret.clone_ptr();
93 /** Creates a new instance of CustomMessageHandler from a given implementation */
94 public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReaderInterface customMessageReader_impl) {
95 LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder();
96 LDKCustomMessageHandlerImpl impl = new LDKCustomMessageHandlerImpl(arg, impl_holder);
97 CustomMessageReader customMessageReader = CustomMessageReader.new_impl(customMessageReader_impl);
98 long[] ptr_idx = bindings.LDKCustomMessageHandler_new(impl, customMessageReader.instance_idx);
100 impl_holder.held = new CustomMessageHandler(null, ptr_idx[0]);
101 impl_holder.held.instance_idx = ptr_idx[1];
102 impl_holder.held.bindings_instance = impl;
103 impl_holder.held.ptrs_to.AddLast(customMessageReader);
104 return impl_holder.held;
108 * Handles the given message sent from `sender_node_id`, possibly producing messages for
109 * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
112 public Result_NoneLightningErrorZ handle_custom_message(org.ldk.structs.Type msg, byte[] sender_node_id) {
113 long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(sender_node_id, 33)));
116 GC.KeepAlive(sender_node_id);
117 if (ret >= 0 && ret <= 4096) { return null; }
118 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
119 if (this != null) { this.ptrs_to.AddLast(msg); };
124 * Returns the list of pending messages that were generated by the handler, clearing the list
125 * in the process. Each message is paired with the node id of the intended recipient. If no
126 * connection to the node exists, then the message is simply not sent.
128 public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
129 long ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
131 if (ret >= 0 && ret <= 4096) { return null; }
132 int ret_conv_25_len = InternalUtils.getArrayLength(ret);
133 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len];
134 for (int z = 0; z < ret_conv_25_len; z++) {
135 long ret_conv_25 = InternalUtils.getU64ArrayElem(ret, z);
136 TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
137 if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.AddLast(this); };
138 ret_conv_25_arr[z] = ret_conv_25_hu_conv;
140 bindings.free_buffer(ret);
141 return ret_conv_25_arr;
145 * Gets the node feature flags which this handler itself supports. All available handlers are
146 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
147 * which are broadcasted in our [`NodeAnnouncement`] message.
149 * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
151 public NodeFeatures provided_node_features() {
152 long ret = bindings.CustomMessageHandler_provided_node_features(this.ptr);
154 if (ret >= 0 && ret <= 4096) { return null; }
155 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
156 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
161 * Gets the init feature flags which should be sent to the given peer. All available handlers
162 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
163 * which are sent in our [`Init`] message.
165 * [`Init`]: crate::ln::msgs::Init
167 public InitFeatures provided_init_features(byte[] their_node_id) {
168 long ret = bindings.CustomMessageHandler_provided_init_features(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)));
170 GC.KeepAlive(their_node_id);
171 if (ret >= 0 && ret <= 4096) { return null; }
172 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
173 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };