6 namespace org { namespace ldk { namespace structs {
9 * A handler provided to [`PeerManager`] for reading and handling custom messages.
11 * [BOLT 1] specifies a custom message type range for use with experimental or application-specific
12 * messages. `CustomMessageHandler` allows for user-defined handling of such types. See the
13 * [`lightning_custom_message`] crate for tools useful in composing more than one custom handler.
15 * [BOLT 1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
16 * [`lightning_custom_message`]: https://docs.rs/lightning_custom_message/latest/lightning_custom_message
18 public class CustomMessageHandler : CommonBase {
19 internal readonly bindings.LDKCustomMessageHandler bindings_instance;
20 internal CustomMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
21 private CustomMessageHandler(bindings.LDKCustomMessageHandler arg, bindings.LDKCustomMessageReader CustomMessageReader) : base(bindings.LDKCustomMessageHandler_new(arg, CustomMessageReader)) {
22 this.ptrs_to.AddLast(arg);
23 this.ptrs_to.AddLast(CustomMessageReader);
24 this.bindings_instance = arg;
26 ~CustomMessageHandler() {
27 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); }
30 public interface CustomMessageHandlerInterface {
32 * Handles the given message sent from `sender_node_id`, possibly producing messages for
33 * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
36 Result_NoneLightningErrorZ handle_custom_message(Type _msg, byte[] _sender_node_id);
38 * Returns the list of pending messages that were generated by the handler, clearing the list
39 * in the process. Each message is paired with the node id of the intended recipient. If no
40 * connection to the node exists, then the message is simply not sent.
42 TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg();
44 * Gets the node feature flags which this handler itself supports. All available handlers are
45 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
46 * which are broadcasted in our [`NodeAnnouncement`] message.
48 * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
50 NodeFeatures provided_node_features();
52 * Gets the init feature flags which should be sent to the given peer. All available handlers
53 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
54 * which are sent in our [`Init`] message.
56 * [`Init`]: crate::ln::msgs::Init
58 InitFeatures provided_init_features(byte[] _their_node_id);
60 private class LDKCustomMessageHandlerHolder { internal CustomMessageHandler held; }
61 private class LDKCustomMessageHandlerImpl : bindings.LDKCustomMessageHandler {
62 internal LDKCustomMessageHandlerImpl(CustomMessageHandlerInterface arg, LDKCustomMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
63 private CustomMessageHandlerInterface arg;
64 private LDKCustomMessageHandlerHolder impl_holder;
65 public long handle_custom_message(long _msg, byte[] _sender_node_id) {
66 Type ret_hu_conv = new Type(null, _msg);
67 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
68 Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, _sender_node_id);
70 long result = ret == null ? 0 : ret.clone_ptr();
73 public long[] get_and_clear_pending_msg() {
74 TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
76 long[] result = ret != null ? InternalUtils.mapArray(ret, ret_conv_25 => ret_conv_25 == null ? 0 : ret_conv_25.clone_ptr()) : null;
79 public long provided_node_features() {
80 NodeFeatures ret = arg.provided_node_features();
82 long result = ret == null ? 0 : ret.clone_ptr();
85 public long provided_init_features(byte[] _their_node_id) {
86 InitFeatures ret = arg.provided_init_features(_their_node_id);
88 long result = ret == null ? 0 : ret.clone_ptr();
92 public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReader.CustomMessageReaderInterface CustomMessageReader_impl) {
93 LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder();
94 impl_holder.held = new CustomMessageHandler(new LDKCustomMessageHandlerImpl(arg, impl_holder), CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance);
95 return impl_holder.held;
99 * Gets the underlying CustomMessageReader.
101 public CustomMessageReader get_custom_message_reader() {
102 CustomMessageReader res = new CustomMessageReader(null, bindings.LDKCustomMessageHandler_get_CustomMessageReader(this.ptr));
103 this.ptrs_to.AddLast(res);
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.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 int ret_conv_25_len = ret.Length;
132 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len];
133 for (int z = 0; z < ret_conv_25_len; z++) {
134 long ret_conv_25 = ret[z];
135 TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
136 if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.AddLast(this); };
137 ret_conv_25_arr[z] = ret_conv_25_hu_conv;
139 return ret_conv_25_arr;
143 * Gets the node feature flags which this handler itself supports. All available handlers are
144 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
145 * which are broadcasted in our [`NodeAnnouncement`] message.
147 * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
149 public NodeFeatures provided_node_features() {
150 long ret = bindings.CustomMessageHandler_provided_node_features(this.ptr);
152 if (ret >= 0 && ret <= 4096) { return null; }
153 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
154 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
159 * Gets the init feature flags which should be sent to the given peer. All available handlers
160 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
161 * which are sent in our [`Init`] message.
163 * [`Init`]: crate::ln::msgs::Init
165 public InitFeatures provided_init_features(byte[] their_node_id) {
166 long ret = bindings.CustomMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
168 GC.KeepAlive(their_node_id);
169 if (ret >= 0 && ret <= 4096) { return null; }
170 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
171 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };