]> git.bitcoin.ninja Git - ldk-java/blob - c_sharp/src/org/ldk/structs/CustomMessageHandler.cs
Update CI references to LDK 0.0.124 drop stale memchr pins
[ldk-java] / c_sharp / src / org / ldk / structs / CustomMessageHandler.cs
1
2 using org.ldk.impl;
3 using org.ldk.enums;
4 using org.ldk.util;
5 using System;
6
7 namespace org { namespace ldk { namespace structs {
8
9
10
11 /** An implementation of CustomMessageHandler */
12 public interface CustomMessageHandlerInterface {
13         /**Handles the given message sent from `sender_node_id`, possibly producing messages for
14          * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
15          * to send.
16          */
17         Result_NoneLightningErrorZ handle_custom_message(Type msg, byte[] sender_node_id);
18         /**Returns the list of pending messages that were generated by the handler, clearing the list
19          * in the process. Each message is paired with the node id of the intended recipient. If no
20          * connection to the node exists, then the message is simply not sent.
21          */
22         TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg();
23         /**Indicates a peer disconnected.
24          */
25         void peer_disconnected(byte[] their_node_id);
26         /**Handle a peer connecting.
27          * 
28          * May return an `Err(())` if the features the peer supports are not sufficient to communicate
29          * with us. Implementors should be somewhat conservative about doing so, however, as other
30          * message handlers may still wish to communicate with this peer.
31          */
32         Result_NoneNoneZ peer_connected(byte[] their_node_id, Init msg, bool inbound);
33         /**Gets the node feature flags which this handler itself supports. All available handlers are
34          * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
35          * which are broadcasted in our [`NodeAnnouncement`] message.
36          * 
37          * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
38          */
39         NodeFeatures provided_node_features();
40         /**Gets the init feature flags which should be sent to the given peer. All available handlers
41          * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
42          * which are sent in our [`Init`] message.
43          * 
44          * [`Init`]: crate::ln::msgs::Init
45          */
46         InitFeatures provided_init_features(byte[] their_node_id);
47 }
48
49 /**
50  * A handler provided to [`PeerManager`] for reading and handling custom messages.
51  * 
52  * [BOLT 1] specifies a custom message type range for use with experimental or application-specific
53  * messages. `CustomMessageHandler` allows for user-defined handling of such types. See the
54  * [`lightning_custom_message`] crate for tools useful in composing more than one custom handler.
55  * 
56  * [BOLT 1]: https://github.com/lightning/bolts/blob/master/01-messaging.md
57  * [`lightning_custom_message`]: https://docs.rs/lightning_custom_message/latest/lightning_custom_message
58  */
59 public class CustomMessageHandler : CommonBase {
60         internal bindings.LDKCustomMessageHandler bindings_instance;
61         internal long instance_idx;
62
63         internal CustomMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
64         ~CustomMessageHandler() {
65                 if (ptr != 0) { bindings.CustomMessageHandler_free(ptr); }
66         }
67
68         private class LDKCustomMessageHandlerHolder { internal CustomMessageHandler held; }
69         private class LDKCustomMessageHandlerImpl : bindings.LDKCustomMessageHandler {
70                 internal LDKCustomMessageHandlerImpl(CustomMessageHandlerInterface arg, LDKCustomMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
71                 private CustomMessageHandlerInterface arg;
72                 private LDKCustomMessageHandlerHolder impl_holder;
73                 public long handle_custom_message(long _msg, long _sender_node_id) {
74                         Type ret_hu_conv = new Type(null, _msg);
75                         if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
76                         byte[] _sender_node_id_conv = InternalUtils.decodeUint8Array(_sender_node_id);
77                         Result_NoneLightningErrorZ ret = arg.handle_custom_message(ret_hu_conv, _sender_node_id_conv);
78                                 GC.KeepAlive(arg);
79                         long result = ret.clone_ptr();
80                         return result;
81                 }
82                 public long get_and_clear_pending_msg() {
83                         TwoTuple_PublicKeyTypeZ[] ret = arg.get_and_clear_pending_msg();
84                                 GC.KeepAlive(arg);
85                         long result = InternalUtils.encodeUint64Array(InternalUtils.mapArray(ret, ret_conv_25 => ret_conv_25.clone_ptr()));
86                         return result;
87                 }
88                 public void peer_disconnected(long _their_node_id) {
89                         byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
90                         arg.peer_disconnected(_their_node_id_conv);
91                                 GC.KeepAlive(arg);
92                 }
93                 public long peer_connected(long _their_node_id, long _msg, bool _inbound) {
94                         byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
95                         org.ldk.structs.Init _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.Init(null, _msg); }
96                         Result_NoneNoneZ ret = arg.peer_connected(_their_node_id_conv, _msg_hu_conv, _inbound);
97                                 GC.KeepAlive(arg);
98                         long result = ret.clone_ptr();
99                         return result;
100                 }
101                 public long provided_node_features() {
102                         NodeFeatures ret = arg.provided_node_features();
103                                 GC.KeepAlive(arg);
104                         long result = ret.clone_ptr();
105                         return result;
106                 }
107                 public long provided_init_features(long _their_node_id) {
108                         byte[] _their_node_id_conv = InternalUtils.decodeUint8Array(_their_node_id);
109                         InitFeatures ret = arg.provided_init_features(_their_node_id_conv);
110                                 GC.KeepAlive(arg);
111                         long result = ret.clone_ptr();
112                         return result;
113                 }
114         }
115
116         /** Creates a new instance of CustomMessageHandler from a given implementation */
117         public static CustomMessageHandler new_impl(CustomMessageHandlerInterface arg, CustomMessageReaderInterface customMessageReader_impl) {
118                 LDKCustomMessageHandlerHolder impl_holder = new LDKCustomMessageHandlerHolder();
119                 LDKCustomMessageHandlerImpl impl = new LDKCustomMessageHandlerImpl(arg, impl_holder);
120                 CustomMessageReader customMessageReader = CustomMessageReader.new_impl(customMessageReader_impl);
121                 long[] ptr_idx = bindings.LDKCustomMessageHandler_new(impl, customMessageReader.instance_idx);
122
123                 impl_holder.held = new CustomMessageHandler(null, ptr_idx[0]);
124                 impl_holder.held.instance_idx = ptr_idx[1];
125                 impl_holder.held.bindings_instance = impl;
126                 impl_holder.held.ptrs_to.AddLast(customMessageReader);
127                 return impl_holder.held;
128         }
129
130         /**
131          * Handles the given message sent from `sender_node_id`, possibly producing messages for
132          * [`CustomMessageHandler::get_and_clear_pending_msg`] to return and thus for [`PeerManager`]
133          * to send.
134          */
135         public Result_NoneLightningErrorZ handle_custom_message(org.ldk.structs.Type msg, byte[] sender_node_id) {
136                 long ret = bindings.CustomMessageHandler_handle_custom_message(this.ptr, msg.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(sender_node_id, 33)));
137                 GC.KeepAlive(this);
138                 GC.KeepAlive(msg);
139                 GC.KeepAlive(sender_node_id);
140                 if (ret >= 0 && ret <= 4096) { return null; }
141                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
142                 if (this != null) { this.ptrs_to.AddLast(msg); };
143                 return ret_hu_conv;
144         }
145
146         /**
147          * Returns the list of pending messages that were generated by the handler, clearing the list
148          * in the process. Each message is paired with the node id of the intended recipient. If no
149          * connection to the node exists, then the message is simply not sent.
150          */
151         public TwoTuple_PublicKeyTypeZ[] get_and_clear_pending_msg() {
152                 long ret = bindings.CustomMessageHandler_get_and_clear_pending_msg(this.ptr);
153                 GC.KeepAlive(this);
154                 if (ret >= 0 && ret <= 4096) { return null; }
155                 int ret_conv_25_len = InternalUtils.getArrayLength(ret);
156                 TwoTuple_PublicKeyTypeZ[] ret_conv_25_arr = new TwoTuple_PublicKeyTypeZ[ret_conv_25_len];
157                 for (int z = 0; z < ret_conv_25_len; z++) {
158                         long ret_conv_25 = InternalUtils.getU64ArrayElem(ret, z);
159                         TwoTuple_PublicKeyTypeZ ret_conv_25_hu_conv = new TwoTuple_PublicKeyTypeZ(null, ret_conv_25);
160                         if (ret_conv_25_hu_conv != null) { ret_conv_25_hu_conv.ptrs_to.AddLast(this); };
161                         ret_conv_25_arr[z] = ret_conv_25_hu_conv;
162                 }
163                 bindings.free_buffer(ret);
164                 return ret_conv_25_arr;
165         }
166
167         /**
168          * Indicates a peer disconnected.
169          */
170         public void peer_disconnected(byte[] their_node_id) {
171                 bindings.CustomMessageHandler_peer_disconnected(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)));
172                 GC.KeepAlive(this);
173                 GC.KeepAlive(their_node_id);
174         }
175
176         /**
177          * Handle a peer connecting.
178          * 
179          * May return an `Err(())` if the features the peer supports are not sufficient to communicate
180          * with us. Implementors should be somewhat conservative about doing so, however, as other
181          * message handlers may still wish to communicate with this peer.
182          */
183         public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init msg, bool inbound) {
184                 long ret = bindings.CustomMessageHandler_peer_connected(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)), msg.ptr, inbound);
185                 GC.KeepAlive(this);
186                 GC.KeepAlive(their_node_id);
187                 GC.KeepAlive(msg);
188                 GC.KeepAlive(inbound);
189                 if (ret >= 0 && ret <= 4096) { return null; }
190                 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
191                 if (this != null) { this.ptrs_to.AddLast(msg); };
192                 return ret_hu_conv;
193         }
194
195         /**
196          * Gets the node feature flags which this handler itself supports. All available handlers are
197          * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
198          * which are broadcasted in our [`NodeAnnouncement`] message.
199          * 
200          * [`NodeAnnouncement`]: crate::ln::msgs::NodeAnnouncement
201          */
202         public NodeFeatures provided_node_features() {
203                 long ret = bindings.CustomMessageHandler_provided_node_features(this.ptr);
204                 GC.KeepAlive(this);
205                 if (ret >= 0 && ret <= 4096) { return null; }
206                 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
207                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
208                 return ret_hu_conv;
209         }
210
211         /**
212          * Gets the init feature flags which should be sent to the given peer. All available handlers
213          * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
214          * which are sent in our [`Init`] message.
215          * 
216          * [`Init`]: crate::ln::msgs::Init
217          */
218         public InitFeatures provided_init_features(byte[] their_node_id) {
219                 long ret = bindings.CustomMessageHandler_provided_init_features(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)));
220                 GC.KeepAlive(this);
221                 GC.KeepAlive(their_node_id);
222                 if (ret >= 0 && ret <= 4096) { return null; }
223                 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
224                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
225                 return ret_hu_conv;
226         }
227
228 }
229 } } }