32d39c27929b0de9114cc53b23a482e6b6d831b9
[ldk-java] / c_sharp / src / org / ldk / structs / CustomMessageHandler.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8 /**
9  * A handler provided to [`PeerManager`] for reading and handling custom messages.
10  * 
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.
14  * 
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
17  */
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;
25         }
26         ~CustomMessageHandler() {
27                 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); }
28         }
29
30         public interface CustomMessageHandlerInterface {
31                 /**
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`]
34                  * to send.
35                  */
36                 Result_NoneLightningErrorZ handle_custom_message(Type _msg, byte[] _sender_node_id);
37                 /**
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.
41                  */
42                 TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg();
43                 /**
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.
47                  * 
48                  * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
49                  */
50                 NodeFeatures provided_node_features();
51                 /**
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.
55                  * 
56                  * [`Init`]: crate::ln::msgs::Init
57                  */
58                 InitFeatures provided_init_features(byte[] _their_node_id);
59         }
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);
69                                 GC.KeepAlive(arg);
70                         long result = ret == null ? 0 : ret.clone_ptr();
71                         return result;
72                 }
73                 public long[] get_and_clear_pending_msg() {
74                         TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
75                                 GC.KeepAlive(arg);
76                         long[] result = ret != null ? InternalUtils.mapArray(ret, ret_conv_25 => ret_conv_25 == null ? 0 : ret_conv_25.clone_ptr()) : null;
77                         return result;
78                 }
79                 public long provided_node_features() {
80                         NodeFeatures ret = arg.provided_node_features();
81                                 GC.KeepAlive(arg);
82                         long result = ret == null ? 0 : ret.clone_ptr();
83                         return result;
84                 }
85                 public long provided_init_features(byte[] _their_node_id) {
86                         InitFeatures ret = arg.provided_init_features(_their_node_id);
87                                 GC.KeepAlive(arg);
88                         long result = ret == null ? 0 : ret.clone_ptr();
89                         return result;
90                 }
91         }
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;
96         }
97
98         /**
99          * Gets the underlying CustomMessageReader.
100          */
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);
104                 return res;
105         }
106
107         /**
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`]
110          * to send.
111          */
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));
114                 GC.KeepAlive(this);
115                 GC.KeepAlive(msg);
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); };
120                 return ret_hu_conv;
121         }
122
123         /**
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.
127          */
128         public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
129                 long[] ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
130                 GC.KeepAlive(this);
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;
138                 }
139                 return ret_conv_25_arr;
140         }
141
142         /**
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.
146          * 
147          * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
148          */
149         public NodeFeatures provided_node_features() {
150                 long ret = bindings.CustomMessageHandler_provided_node_features(this.ptr);
151                 GC.KeepAlive(this);
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); };
155                 return ret_hu_conv;
156         }
157
158         /**
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.
162          * 
163          * [`Init`]: crate::ln::msgs::Init
164          */
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));
167                 GC.KeepAlive(this);
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); };
172                 return ret_hu_conv;
173         }
174
175 }
176 } } }