6 namespace org { namespace ldk { namespace structs {
9 * A trait to describe an object that can receive onion messages.
11 public class OnionMessageHandler : CommonBase {
12 internal readonly bindings.LDKOnionMessageHandler bindings_instance;
13 internal OnionMessageHandler(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
14 private OnionMessageHandler(bindings.LDKOnionMessageHandler arg, bindings.LDKOnionMessageProvider OnionMessageProvider) : base(bindings.LDKOnionMessageHandler_new(arg, OnionMessageProvider)) {
15 this.ptrs_to.AddLast(arg);
16 this.ptrs_to.AddLast(OnionMessageProvider);
17 this.bindings_instance = arg;
19 ~OnionMessageHandler() {
20 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); }
23 public interface OnionMessageHandlerInterface {
25 * Handle an incoming onion_message message from the given peer.
27 void handle_onion_message(byte[] _peer_node_id, OnionMessage _msg);
29 * Called when a connection is established with a peer. Can be used to track which peers
30 * advertise onion message support and are online.
32 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
33 * with us. Implementors should be somewhat conservative about doing so, however, as other
34 * message handlers may still wish to communicate with this peer.
36 Result_NoneNoneZ peer_connected(byte[] _their_node_id, Init _init);
38 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
39 * drop and refuse to forward onion messages to this peer.
41 * Note that in some rare cases this may be called without a corresponding
42 * [`Self::peer_connected`].
44 void peer_disconnected(byte[] _their_node_id, bool _no_connection_possible);
46 * Gets the node feature flags which this handler itself supports. All available handlers are
47 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
48 * which are broadcasted in our [`NodeAnnouncement`] message.
50 NodeFeatures provided_node_features();
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.
56 * Note that this method is called before [`Self::peer_connected`].
58 InitFeatures provided_init_features(byte[] _their_node_id);
60 private class LDKOnionMessageHandlerHolder { internal OnionMessageHandler held; }
61 private class LDKOnionMessageHandlerImpl : bindings.LDKOnionMessageHandler {
62 internal LDKOnionMessageHandlerImpl(OnionMessageHandlerInterface arg, LDKOnionMessageHandlerHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
63 private OnionMessageHandlerInterface arg;
64 private LDKOnionMessageHandlerHolder impl_holder;
65 public void handle_onion_message(byte[] _peer_node_id, long _msg) {
66 org.ldk.structs.OnionMessage _msg_hu_conv = null; if (_msg < 0 || _msg > 4096) { _msg_hu_conv = new org.ldk.structs.OnionMessage(null, _msg); }
67 arg.handle_onion_message(_peer_node_id, _msg_hu_conv);
70 public long peer_connected(byte[] _their_node_id, long _init) {
71 org.ldk.structs.Init _init_hu_conv = null; if (_init < 0 || _init > 4096) { _init_hu_conv = new org.ldk.structs.Init(null, _init); }
72 Result_NoneNoneZ ret = arg.peer_connected(_their_node_id, _init_hu_conv);
74 long result = ret == null ? 0 : ret.clone_ptr();
77 public void peer_disconnected(byte[] _their_node_id, bool _no_connection_possible) {
78 arg.peer_disconnected(_their_node_id, _no_connection_possible);
81 public long provided_node_features() {
82 NodeFeatures ret = arg.provided_node_features();
84 long result = ret == null ? 0 : ret.clone_ptr();
87 public long provided_init_features(byte[] _their_node_id) {
88 InitFeatures ret = arg.provided_init_features(_their_node_id);
90 long result = ret == null ? 0 : ret.clone_ptr();
94 public static OnionMessageHandler new_impl(OnionMessageHandlerInterface arg, OnionMessageProvider.OnionMessageProviderInterface OnionMessageProvider_impl) {
95 LDKOnionMessageHandlerHolder impl_holder = new LDKOnionMessageHandlerHolder();
96 impl_holder.held = new OnionMessageHandler(new LDKOnionMessageHandlerImpl(arg, impl_holder), OnionMessageProvider.new_impl(OnionMessageProvider_impl).bindings_instance);
97 return impl_holder.held;
101 * Gets the underlying OnionMessageProvider.
103 public OnionMessageProvider get_onion_message_provider() {
104 OnionMessageProvider res = new OnionMessageProvider(null, bindings.LDKOnionMessageHandler_get_OnionMessageProvider(this.ptr));
105 this.ptrs_to.AddLast(res);
110 * Handle an incoming onion_message message from the given peer.
112 public void handle_onion_message(byte[] peer_node_id, org.ldk.structs.OnionMessage msg) {
113 bindings.OnionMessageHandler_handle_onion_message(this.ptr, InternalUtils.check_arr_len(peer_node_id, 33), msg == null ? 0 : msg.ptr);
115 GC.KeepAlive(peer_node_id);
117 if (this != null) { this.ptrs_to.AddLast(msg); };
121 * Called when a connection is established with a peer. Can be used to track which peers
122 * advertise onion message support and are online.
124 * May return an `Err(())` if the features the peer supports are not sufficient to communicate
125 * with us. Implementors should be somewhat conservative about doing so, however, as other
126 * message handlers may still wish to communicate with this peer.
128 public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init) {
129 long ret = bindings.OnionMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr);
131 GC.KeepAlive(their_node_id);
133 if (ret >= 0 && ret <= 4096) { return null; }
134 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
135 if (this != null) { this.ptrs_to.AddLast(init); };
140 * Indicates a connection to the peer failed/an existing connection was lost. Allows handlers to
141 * drop and refuse to forward onion messages to this peer.
143 * Note that in some rare cases this may be called without a corresponding
144 * [`Self::peer_connected`].
146 public void peer_disconnected(byte[] their_node_id, bool no_connection_possible) {
147 bindings.OnionMessageHandler_peer_disconnected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), no_connection_possible);
149 GC.KeepAlive(their_node_id);
150 GC.KeepAlive(no_connection_possible);
154 * Gets the node feature flags which this handler itself supports. All available handlers are
155 * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
156 * which are broadcasted in our [`NodeAnnouncement`] message.
158 public NodeFeatures provided_node_features() {
159 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
161 if (ret >= 0 && ret <= 4096) { return null; }
162 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
163 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
168 * Gets the init feature flags which should be sent to the given peer. All available handlers
169 * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
170 * which are sent in our [`Init`] message.
172 * Note that this method is called before [`Self::peer_connected`].
174 public InitFeatures provided_init_features(byte[] their_node_id) {
175 long ret = bindings.OnionMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
177 GC.KeepAlive(their_node_id);
178 if (ret >= 0 && ret <= 4096) { return null; }
179 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
180 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };