891c162aec664bf76610e8c4c9068ddd9a5e5000
[ldk-java] / c_sharp / src / org / ldk / structs / OnionMessageHandler.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 trait to describe an object that can receive onion messages.
10  */
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;
18         }
19         ~OnionMessageHandler() {
20                 if (ptr != 0) { bindings.OnionMessageHandler_free(ptr); }
21         }
22
23         public interface OnionMessageHandlerInterface {
24                 /**
25                  * Handle an incoming onion_message message from the given peer.
26                  */
27                 void handle_onion_message(byte[] _peer_node_id, OnionMessage _msg);
28                 /**
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.
31                  * 
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.
35                  */
36                 Result_NoneNoneZ peer_connected(byte[] _their_node_id, Init _init);
37                 /**
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.
40                  * 
41                  * Note that in some rare cases this may be called without a corresponding
42                  * [`Self::peer_connected`].
43                  */
44                 void peer_disconnected(byte[] _their_node_id, bool _no_connection_possible);
45                 /**
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.
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                  * Note that this method is called before [`Self::peer_connected`].
57                  */
58                 InitFeatures provided_init_features(byte[] _their_node_id);
59         }
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);
68                                 GC.KeepAlive(arg);
69                 }
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);
73                                 GC.KeepAlive(arg);
74                         long result = ret == null ? 0 : ret.clone_ptr();
75                         return result;
76                 }
77                 public void peer_disconnected(byte[] _their_node_id, bool _no_connection_possible) {
78                         arg.peer_disconnected(_their_node_id, _no_connection_possible);
79                                 GC.KeepAlive(arg);
80                 }
81                 public long provided_node_features() {
82                         NodeFeatures ret = arg.provided_node_features();
83                                 GC.KeepAlive(arg);
84                         long result = ret == null ? 0 : ret.clone_ptr();
85                         return result;
86                 }
87                 public long provided_init_features(byte[] _their_node_id) {
88                         InitFeatures ret = arg.provided_init_features(_their_node_id);
89                                 GC.KeepAlive(arg);
90                         long result = ret == null ? 0 : ret.clone_ptr();
91                         return result;
92                 }
93         }
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;
98         }
99
100         /**
101          * Gets the underlying OnionMessageProvider.
102          */
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);
106                 return res;
107         }
108
109         /**
110          * Handle an incoming onion_message message from the given peer.
111          */
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);
114                 GC.KeepAlive(this);
115                 GC.KeepAlive(peer_node_id);
116                 GC.KeepAlive(msg);
117                 if (this != null) { this.ptrs_to.AddLast(msg); };
118         }
119
120         /**
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.
123          * 
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.
127          */
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);
130                 GC.KeepAlive(this);
131                 GC.KeepAlive(their_node_id);
132                 GC.KeepAlive(init);
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); };
136                 return ret_hu_conv;
137         }
138
139         /**
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.
142          * 
143          * Note that in some rare cases this may be called without a corresponding
144          * [`Self::peer_connected`].
145          */
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);
148                 GC.KeepAlive(this);
149                 GC.KeepAlive(their_node_id);
150                 GC.KeepAlive(no_connection_possible);
151         }
152
153         /**
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.
157          */
158         public NodeFeatures provided_node_features() {
159                 long ret = bindings.OnionMessageHandler_provided_node_features(this.ptr);
160                 GC.KeepAlive(this);
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); };
164                 return ret_hu_conv;
165         }
166
167         /**
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.
171          * 
172          * Note that this method is called before [`Self::peer_connected`].
173          */
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));
176                 GC.KeepAlive(this);
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); };
181                 return ret_hu_conv;
182         }
183
184 }
185 } } }