7 namespace org { namespace ldk { namespace structs {
11 /** An implementation of CustomOnionMessageHandler */
12 public interface CustomOnionMessageHandlerInterface {
13 /**Called with the custom message that was received, returning a response to send, if any.
15 * The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
17 Option_OnionMessageContentsZ handle_custom_message(OnionMessageContents msg);
18 /**Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
19 * message type is unknown.
21 Result_COption_OnionMessageContentsZDecodeErrorZ read_custom_message(long message_type, byte[] buffer);
22 /**Releases any [`Self::CustomMessage`]s that need to be sent.
24 * Typically, this is used for messages initiating a message flow rather than in response to
25 * another message. The latter should use the return value of [`Self::handle_custom_message`].
27 ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[] release_pending_custom_messages();
31 * Handler for custom onion messages. If you are using [`SimpleArcOnionMessenger`],
32 * [`SimpleRefOnionMessenger`], or prefer to ignore inbound custom onion messages,
33 * [`IgnoringMessageHandler`] must be provided to [`OnionMessenger::new`]. Otherwise, a custom
34 * implementation of this trait must be provided, with [`CustomMessage`] specifying the supported
37 * See [`OnionMessenger`] for example usage.
39 * [`IgnoringMessageHandler`]: crate::ln::peer_handler::IgnoringMessageHandler
40 * [`CustomMessage`]: Self::CustomMessage
42 public class CustomOnionMessageHandler : CommonBase {
43 internal bindings.LDKCustomOnionMessageHandler bindings_instance;
44 internal long instance_idx;
46 internal CustomOnionMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
47 ~CustomOnionMessageHandler() {
48 if (ptr != 0) { bindings.CustomOnionMessageHandler_free(ptr); }
51 private class LDKCustomOnionMessageHandlerHolder { internal CustomOnionMessageHandler held; }
52 private class LDKCustomOnionMessageHandlerImpl : bindings.LDKCustomOnionMessageHandler {
53 internal LDKCustomOnionMessageHandlerImpl(CustomOnionMessageHandlerInterface arg, LDKCustomOnionMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
54 private CustomOnionMessageHandlerInterface arg;
55 private LDKCustomOnionMessageHandlerHolder impl_holder;
56 public long handle_custom_message(long _msg) {
57 OnionMessageContents ret_hu_conv = new OnionMessageContents(null, _msg);
58 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
59 Option_OnionMessageContentsZ ret = arg.handle_custom_message(ret_hu_conv);
61 long result = ret == null ? 0 : ret.clone_ptr();
62 if (impl_holder.held != null) { impl_holder.held.ptrs_to.AddLast(ret); };
65 public long read_custom_message(long _message_type, long _buffer) {
66 byte[] _buffer_conv = InternalUtils.decodeUint8Array(_buffer);
67 Result_COption_OnionMessageContentsZDecodeErrorZ ret = arg.read_custom_message(_message_type, _buffer_conv);
69 long result = ret == null ? 0 : ret.clone_ptr();
72 public long release_pending_custom_messages() {
73 ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[] ret = arg.release_pending_custom_messages();
75 long result = InternalUtils.encodeUint64Array(InternalUtils.mapArray(ret, ret_conv_56 => ret_conv_56 == null ? 0 : ret_conv_56.clone_ptr()));
80 /** Creates a new instance of CustomOnionMessageHandler from a given implementation */
81 public static CustomOnionMessageHandler new_impl(CustomOnionMessageHandlerInterface arg) {
82 LDKCustomOnionMessageHandlerHolder impl_holder = new LDKCustomOnionMessageHandlerHolder();
83 LDKCustomOnionMessageHandlerImpl impl = new LDKCustomOnionMessageHandlerImpl(arg, impl_holder);
84 long[] ptr_idx = bindings.LDKCustomOnionMessageHandler_new(impl);
86 impl_holder.held = new CustomOnionMessageHandler(null, ptr_idx[0]);
87 impl_holder.held.instance_idx = ptr_idx[1];
88 impl_holder.held.bindings_instance = impl;
89 return impl_holder.held;
93 * Called with the custom message that was received, returning a response to send, if any.
95 * The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`].
97 public Option_OnionMessageContentsZ handle_custom_message(org.ldk.structs.OnionMessageContents msg) {
98 long ret = bindings.CustomOnionMessageHandler_handle_custom_message(this.ptr, msg.ptr);
101 if (ret >= 0 && ret <= 4096) { return null; }
102 org.ldk.structs.Option_OnionMessageContentsZ ret_hu_conv = org.ldk.structs.Option_OnionMessageContentsZ.constr_from_ptr(ret);
103 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
104 if (this != null) { this.ptrs_to.AddLast(msg); };
109 * Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the
110 * message type is unknown.
112 public Result_COption_OnionMessageContentsZDecodeErrorZ read_custom_message(long message_type, byte[] buffer) {
113 long ret = bindings.CustomOnionMessageHandler_read_custom_message(this.ptr, message_type, InternalUtils.encodeUint8Array(buffer));
115 GC.KeepAlive(message_type);
116 GC.KeepAlive(buffer);
117 if (ret >= 0 && ret <= 4096) { return null; }
118 Result_COption_OnionMessageContentsZDecodeErrorZ ret_hu_conv = Result_COption_OnionMessageContentsZDecodeErrorZ.constr_from_ptr(ret);
123 * Releases any [`Self::CustomMessage`]s that need to be sent.
125 * Typically, this is used for messages initiating a message flow rather than in response to
126 * another message. The latter should use the return value of [`Self::handle_custom_message`].
128 public ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[] release_pending_custom_messages() {
129 long ret = bindings.CustomOnionMessageHandler_release_pending_custom_messages(this.ptr);
131 if (ret >= 0 && ret <= 4096) { return null; }
132 int ret_conv_56_len = InternalUtils.getArrayLength(ret);
133 ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[] ret_conv_56_arr = new ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[ret_conv_56_len];
134 for (int e = 0; e < ret_conv_56_len; e++) {
135 long ret_conv_56 = InternalUtils.getU64ArrayElem(ret, e);
136 ThreeTuple_OnionMessageContentsDestinationBlindedPathZ ret_conv_56_hu_conv = new ThreeTuple_OnionMessageContentsDestinationBlindedPathZ(null, ret_conv_56);
137 if (ret_conv_56_hu_conv != null) { ret_conv_56_hu_conv.ptrs_to.AddLast(this); };
138 ret_conv_56_arr[e] = ret_conv_56_hu_conv;
140 bindings.free_buffer(ret);
141 return ret_conv_56_arr;