X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;ds=sidebyside;f=c_sharp%2Fsrc%2Forg%2Fldk%2Fstructs%2FCustomOnionMessageHandler.cs;h=95565d2b41eba4899753cad507c989389ca28b51;hb=592c64eb658d92e314a2b983cea16cecf3882474;hp=d3b989ff326e827a169a6f8c73a837e621056324;hpb=8aa8a96fc6d8fcdd3fbb419b4b4c12482af14938;p=ldk-java diff --git a/c_sharp/src/org/ldk/structs/CustomOnionMessageHandler.cs b/c_sharp/src/org/ldk/structs/CustomOnionMessageHandler.cs index d3b989ff..95565d2b 100644 --- a/c_sharp/src/org/ldk/structs/CustomOnionMessageHandler.cs +++ b/c_sharp/src/org/ldk/structs/CustomOnionMessageHandler.cs @@ -1,3 +1,4 @@ + using org.ldk.impl; using org.ldk.enums; using org.ldk.util; @@ -5,6 +6,27 @@ using System; namespace org { namespace ldk { namespace structs { + + +/** An implementation of CustomOnionMessageHandler */ +public interface CustomOnionMessageHandlerInterface { + /**Called with the custom message that was received, returning a response to send, if any. + * + * The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`]. + */ + Option_OnionMessageContentsZ handle_custom_message(OnionMessageContents msg); + /**Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the + * message type is unknown. + */ + Result_COption_OnionMessageContentsZDecodeErrorZ read_custom_message(long message_type, byte[] buffer); + /**Releases any [`Self::CustomMessage`]s that need to be sent. + * + * Typically, this is used for messages initiating a message flow rather than in response to + * another message. The latter should use the return value of [`Self::handle_custom_message`]. + */ + ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[] release_pending_custom_messages(); +} + /** * Handler for custom onion messages. If you are using [`SimpleArcOnionMessenger`], * [`SimpleRefOnionMessenger`], or prefer to ignore inbound custom onion messages, @@ -18,73 +40,106 @@ namespace org { namespace ldk { namespace structs { * [`CustomMessage`]: Self::CustomMessage */ public class CustomOnionMessageHandler : CommonBase { - internal readonly bindings.LDKCustomOnionMessageHandler bindings_instance; + internal bindings.LDKCustomOnionMessageHandler bindings_instance; + internal long instance_idx; + internal CustomOnionMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; } - private CustomOnionMessageHandler(bindings.LDKCustomOnionMessageHandler arg) : base(bindings.LDKCustomOnionMessageHandler_new(arg)) { - this.ptrs_to.AddLast(arg); - this.bindings_instance = arg; - } ~CustomOnionMessageHandler() { if (ptr != 0) { bindings.CustomOnionMessageHandler_free(ptr); } } - public interface CustomOnionMessageHandlerInterface { - /** - * Called with the custom message that was received. - */ - void handle_custom_message(CustomOnionMessageContents _msg); - /** - * Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the - * message type is unknown. - */ - Result_COption_CustomOnionMessageContentsZDecodeErrorZ read_custom_message(long _message_type, byte[] _buffer); - } private class LDKCustomOnionMessageHandlerHolder { internal CustomOnionMessageHandler held; } private class LDKCustomOnionMessageHandlerImpl : bindings.LDKCustomOnionMessageHandler { internal LDKCustomOnionMessageHandlerImpl(CustomOnionMessageHandlerInterface arg, LDKCustomOnionMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; } private CustomOnionMessageHandlerInterface arg; private LDKCustomOnionMessageHandlerHolder impl_holder; - public void handle_custom_message(long _msg) { - CustomOnionMessageContents ret_hu_conv = new CustomOnionMessageContents(null, _msg); + public long handle_custom_message(long _msg) { + OnionMessageContents ret_hu_conv = new OnionMessageContents(null, _msg); if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); }; - arg.handle_custom_message(ret_hu_conv); + Option_OnionMessageContentsZ ret = arg.handle_custom_message(ret_hu_conv); + GC.KeepAlive(arg); + long result = ret.clone_ptr(); + if (impl_holder.held != null) { impl_holder.held.ptrs_to.AddLast(ret); }; + return result; + } + public long read_custom_message(long _message_type, long _buffer) { + byte[] _buffer_conv = InternalUtils.decodeUint8Array(_buffer); + Result_COption_OnionMessageContentsZDecodeErrorZ ret = arg.read_custom_message(_message_type, _buffer_conv); GC.KeepAlive(arg); + long result = ret.clone_ptr(); + return result; } - public long read_custom_message(long _message_type, byte[] _buffer) { - Result_COption_CustomOnionMessageContentsZDecodeErrorZ ret = arg.read_custom_message(_message_type, _buffer); + public long release_pending_custom_messages() { + ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[] ret = arg.release_pending_custom_messages(); GC.KeepAlive(arg); - long result = ret == null ? 0 : ret.clone_ptr(); + long result = InternalUtils.encodeUint64Array(InternalUtils.mapArray(ret, ret_conv_56 => ret_conv_56.clone_ptr())); return result; } } + + /** Creates a new instance of CustomOnionMessageHandler from a given implementation */ public static CustomOnionMessageHandler new_impl(CustomOnionMessageHandlerInterface arg) { LDKCustomOnionMessageHandlerHolder impl_holder = new LDKCustomOnionMessageHandlerHolder(); - impl_holder.held = new CustomOnionMessageHandler(new LDKCustomOnionMessageHandlerImpl(arg, impl_holder)); + LDKCustomOnionMessageHandlerImpl impl = new LDKCustomOnionMessageHandlerImpl(arg, impl_holder); + long[] ptr_idx = bindings.LDKCustomOnionMessageHandler_new(impl); + + impl_holder.held = new CustomOnionMessageHandler(null, ptr_idx[0]); + impl_holder.held.instance_idx = ptr_idx[1]; + impl_holder.held.bindings_instance = impl; return impl_holder.held; } + /** - * Called with the custom message that was received. + * Called with the custom message that was received, returning a response to send, if any. + * + * The returned [`Self::CustomMessage`], if any, is enqueued to be sent by [`OnionMessenger`]. */ - public void handle_custom_message(org.ldk.structs.CustomOnionMessageContents msg) { - bindings.CustomOnionMessageHandler_handle_custom_message(this.ptr, msg == null ? 0 : msg.ptr); + public Option_OnionMessageContentsZ handle_custom_message(org.ldk.structs.OnionMessageContents msg) { + long ret = bindings.CustomOnionMessageHandler_handle_custom_message(this.ptr, msg.ptr); GC.KeepAlive(this); GC.KeepAlive(msg); + if (ret >= 0 && ret <= 4096) { return null; } + org.ldk.structs.Option_OnionMessageContentsZ ret_hu_conv = org.ldk.structs.Option_OnionMessageContentsZ.constr_from_ptr(ret); + if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); }; if (this != null) { this.ptrs_to.AddLast(msg); }; + return ret_hu_conv; } /** * Read a custom message of type `message_type` from `buffer`, returning `Ok(None)` if the * message type is unknown. */ - public Result_COption_CustomOnionMessageContentsZDecodeErrorZ read_custom_message(long message_type, byte[] buffer) { - long ret = bindings.CustomOnionMessageHandler_read_custom_message(this.ptr, message_type, buffer); + public Result_COption_OnionMessageContentsZDecodeErrorZ read_custom_message(long message_type, byte[] buffer) { + long ret = bindings.CustomOnionMessageHandler_read_custom_message(this.ptr, message_type, InternalUtils.encodeUint8Array(buffer)); GC.KeepAlive(this); GC.KeepAlive(message_type); GC.KeepAlive(buffer); if (ret >= 0 && ret <= 4096) { return null; } - Result_COption_CustomOnionMessageContentsZDecodeErrorZ ret_hu_conv = Result_COption_CustomOnionMessageContentsZDecodeErrorZ.constr_from_ptr(ret); + Result_COption_OnionMessageContentsZDecodeErrorZ ret_hu_conv = Result_COption_OnionMessageContentsZDecodeErrorZ.constr_from_ptr(ret); return ret_hu_conv; } + /** + * Releases any [`Self::CustomMessage`]s that need to be sent. + * + * Typically, this is used for messages initiating a message flow rather than in response to + * another message. The latter should use the return value of [`Self::handle_custom_message`]. + */ + public ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[] release_pending_custom_messages() { + long ret = bindings.CustomOnionMessageHandler_release_pending_custom_messages(this.ptr); + GC.KeepAlive(this); + if (ret >= 0 && ret <= 4096) { return null; } + int ret_conv_56_len = InternalUtils.getArrayLength(ret); + ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[] ret_conv_56_arr = new ThreeTuple_OnionMessageContentsDestinationBlindedPathZ[ret_conv_56_len]; + for (int e = 0; e < ret_conv_56_len; e++) { + long ret_conv_56 = InternalUtils.getU64ArrayElem(ret, e); + ThreeTuple_OnionMessageContentsDestinationBlindedPathZ ret_conv_56_hu_conv = new ThreeTuple_OnionMessageContentsDestinationBlindedPathZ(null, ret_conv_56); + if (ret_conv_56_hu_conv != null) { ret_conv_56_hu_conv.ptrs_to.AddLast(this); }; + ret_conv_56_arr[e] = ret_conv_56_hu_conv; + } + bindings.free_buffer(ret); + return ret_conv_56_arr; + } + } } } }