X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-java;a=blobdiff_plain;f=c_sharp%2Fsrc%2Forg%2Fldk%2Fstructs%2FCustomMessageHandler.cs;h=fdec99ad4ae32e27b869972c021691fecd52c4ea;hp=32d39c27929b0de9114cc53b23a482e6b6d831b9;hb=8de7213fbf663ff60322896282dad51e8ab2f001;hpb=afc50e5d491a11364849383b75a8f939df703bac diff --git a/c_sharp/src/org/ldk/structs/CustomMessageHandler.cs b/c_sharp/src/org/ldk/structs/CustomMessageHandler.cs index 32d39c27..fdec99ad 100644 --- a/c_sharp/src/org/ldk/structs/CustomMessageHandler.cs +++ b/c_sharp/src/org/ldk/structs/CustomMessageHandler.cs @@ -1,3 +1,4 @@ + using org.ldk.impl; using org.ldk.enums; using org.ldk.util; @@ -5,6 +6,36 @@ using System; namespace org { namespace ldk { namespace structs { + + +/** An implementation of CustomMessageHandler */ +public interface CustomMessageHandlerInterface { + /**Handles the given message sent from `sender_node_id`, possibly producing messages for + * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`] + * to send. + */ + Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id); + /**Returns the list of pending messages that were generated by the handler, clearing the list + * in the process. Each message is paired with the node id of the intended recipient. If no + * connection to the node exists, then the message is simply not sent. + */ + TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg(); + /**Gets the node feature flags which this handler itself supports. All available handlers are + * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`] + * which are broadcasted in our [`NodeAnnouncement`] message. + * + * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement + */ + NodeFeatures provided_node_features(); + /**Gets the init feature flags which should be sent to the given peer. All available handlers + * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`] + * which are sent in our [`Init`] message. + * + * [`Init`]: crate::ln::msgs::Init + */ + InitFeatures provided_init_features(byte[] their_node_id); +} + /** * A handler provided to [`PeerManager`] for reading and handling custom messages. * @@ -16,64 +47,32 @@ namespace org { namespace ldk { namespace structs { * [`lightning_custom_message`]: https://docs.rs/lightning_custom_message/latest/lightning_custom_message */ public class CustomMessageHandler : CommonBase { - internal readonly bindings.LDKCustomMessageHandler bindings_instance; + internal bindings.LDKCustomMessageHandler bindings_instance; + internal long instance_idx; + internal CustomMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; } - private CustomMessageHandler(bindings.LDKCustomMessageHandler arg, bindings.LDKCustomMessageReader CustomMessageReader) : base(bindings.LDKCustomMessageHandler_new(arg, CustomMessageReader)) { - this.ptrs_to.AddLast(arg); - this.ptrs_to.AddLast(CustomMessageReader); - this.bindings_instance = arg; - } ~CustomMessageHandler() { if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); } } - public interface CustomMessageHandlerInterface { - /** - * Handles the given message sent from `sender_node_id`, possibly producing messages for - * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`] - * to send. - */ - Result_NoneLightningErrorZ handle_custom_message(Type _msg, byte[] _sender_node_id); - /** - * Returns the list of pending messages that were generated by the handler, clearing the list - * in the process. Each message is paired with the node id of the intended recipient. If no - * connection to the node exists, then the message is simply not sent. - */ - TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg(); - /** - * Gets the node feature flags which this handler itself supports. All available handlers are - * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`] - * which are broadcasted in our [`NodeAnnouncement`] message. - * - * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement - */ - NodeFeatures provided_node_features(); - /** - * Gets the init feature flags which should be sent to the given peer. All available handlers - * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`] - * which are sent in our [`Init`] message. - * - * [`Init`]: crate::ln::msgs::Init - */ - InitFeatures provided_init_features(byte[] _their_node_id); - } private class LDKCustomMessageHandlerHolder { internal CustomMessageHandler held; } private class LDKCustomMessageHandlerImpl : bindings.LDKCustomMessageHandler { internal LDKCustomMessageHandlerImpl(CustomMessageHandlerInterface arg, LDKCustomMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; } private CustomMessageHandlerInterface arg; private LDKCustomMessageHandlerHolder impl_holder; - public long handle_custom_message(long _msg, byte[] _sender_node_id) { + public long handle_custom_message(long _msg, long _sender_node_id) { Type ret_hu_conv = new Type(null, _msg); if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); }; - Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, _sender_node_id); + byte[] _sender_node_id_conv = InternalUtils.decodeUint8Array(_sender_node_id); + Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, _sender_node_id_conv); GC.KeepAlive(arg); long result = ret == null ? 0 : ret.clone_ptr(); return result; } - public long[] get_and_clear_pending_msg() { + public long get_and_clear_pending_msg() { TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg(); GC.KeepAlive(arg); - long[] result = ret != null ? InternalUtils.mapArray(ret, ret_conv_25 => ret_conv_25 == null ? 0 : ret_conv_25.clone_ptr()) : null; + long result = InternalUtils.encodeUint64Array(InternalUtils.mapArray(ret, ret_conv_25 => ret_conv_25 == null ? 0 : ret_conv_25.clone_ptr())); return result; } public long provided_node_features() { @@ -82,26 +81,27 @@ public class CustomMessageHandler : CommonBase { long result = ret == null ? 0 : ret.clone_ptr(); return result; } - public long provided_init_features(byte[] _their_node_id) { - InitFeatures ret = arg.provided_init_features(_their_node_id); + public long provided_init_features(long _their_node_id) { + byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id); + InitFeatures ret = arg.provided_init_features(_their_node_id_conv); GC.KeepAlive(arg); long result = ret == null ? 0 : ret.clone_ptr(); return result; } } - public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReader.CustomMessageReaderInterface CustomMessageReader_impl) { + + /** Creates a new instance of CustomMessageHandler from a given implementation */ + public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReaderInterface customMessageReader_impl) { LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder(); - impl_holder.held = new CustomMessageHandler(new LDKCustomMessageHandlerImpl(arg, impl_holder), CustomMessageReader.new_impl(CustomMessageReader_impl).bindings_instance); - return impl_holder.held; - } + LDKCustomMessageHandlerImpl impl = new LDKCustomMessageHandlerImpl(arg, impl_holder); + CustomMessageReader customMessageReader = CustomMessageReader.new_impl(customMessageReader_impl); + long[] ptr_idx = bindings.LDKCustomMessageHandler_new(impl, customMessageReader.instance_idx); - /** - * Gets the underlying CustomMessageReader. - */ - public CustomMessageReader get_custom_message_reader() { - CustomMessageReader res = new CustomMessageReader(null, bindings.LDKCustomMessageHandler_get_CustomMessageReader(this.ptr)); - this.ptrs_to.AddLast(res); - return res; + impl_holder.held = new CustomMessageHandler(null, ptr_idx[0]); + impl_holder.held.instance_idx = ptr_idx[1]; + impl_holder.held.bindings_instance = impl; + impl_holder.held.ptrs_to.AddLast(customMessageReader); + return impl_holder.held; } /** @@ -110,7 +110,7 @@ public class CustomMessageHandler : CommonBase { * to send. */ public Result_NoneLightningErrorZ handle_custom_message(org.ldk.structs.Type msg, byte[] sender_node_id) { - long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg.ptr, InternalUtils.check_arr_len(sender_node_id, 33)); + long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(sender_node_id, 33))); GC.KeepAlive(this); GC.KeepAlive(msg); GC.KeepAlive(sender_node_id); @@ -126,16 +126,18 @@ public class CustomMessageHandler : CommonBase { * connection to the node exists, then the message is simply not sent. */ public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() { - long[] ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr); + long ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr); GC.KeepAlive(this); - int ret_conv_25_len = ret.Length; + if (ret >= 0 && ret <= 4096) { return null; } + int ret_conv_25_len = InternalUtils.getArrayLength(ret); TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len]; for (int z = 0; z < ret_conv_25_len; z++) { - long ret_conv_25 = ret[z]; + long ret_conv_25 = InternalUtils.getU64ArrayElem(ret, z); TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25); if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.AddLast(this); }; ret_conv_25_arr[z] = ret_conv_25_hu_conv; } + bindings.free_buffer(ret); return ret_conv_25_arr; } @@ -163,7 +165,7 @@ public class CustomMessageHandler : CommonBase { * [`Init`]: crate::ln::msgs::Init */ public InitFeatures provided_init_features(byte[] their_node_id) { - long ret = bindings.CustomMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33)); + long ret = bindings.CustomMessageHandler_provided_init_features(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33))); GC.KeepAlive(this); GC.KeepAlive(their_node_id); if (ret >= 0 && ret <= 4096) { return null; }