[C#] Drop reference to `assert` which makes building on Win hard
[ldk-java] / c_sharp / src / org / ldk / structs / PeerManager.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 /**
10  * A PeerManager manages a set of peers, described by their [`SocketDescriptor`] and marshalls
11  * socket events into messages which it passes on to its [`MessageHandler`].
12  * 
13  * Locks are taken internally, so you must never assume that reentrancy from a
14  * [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
15  * 
16  * Calls to [`read_event`] will decode relevant messages and pass them to the
17  * [`ChannelMessageHandler`], likely doing message processing in-line. Thus, the primary form of
18  * parallelism in Rust-Lightning is in calls to [`read_event`]. Note, however, that calls to any
19  * [`PeerManager`] functions related to the same connection must occur only in serial, making new
20  * calls only after previous ones have returned.
21  * 
22  * Rather than using a plain [`PeerManager`], it is preferable to use either a [`SimpleArcPeerManager`]
23  * a [`SimpleRefPeerManager`], for conciseness. See their documentation for more details, but
24  * essentially you should default to using a [`SimpleRefPeerManager`], and use a
25  * [`SimpleArcPeerManager`] when you require a `PeerManager` with a static lifetime, such as when
26  * you're using lightning-net-tokio.
27  * 
28  * [`read_event`]: PeerManager::read_event
29  */
30 public class PeerManager : CommonBase {
31         internal PeerManager(object _dummy, long ptr) : base(ptr) { }
32         ~PeerManager() {
33                 if (ptr != 0) { bindings.PeerManager_free(ptr); }
34         }
35
36         /**
37          * Constructs a new `PeerManager` with the given message handlers.
38          * 
39          * `ephemeral_random_data` is used to derive per-connection ephemeral keys and must be
40          * cryptographically secure random bytes.
41          * 
42          * `current_time` is used as an always-increasing counter that survives across restarts and is
43          * incremented irregularly internally. In general it is best to simply use the current UNIX
44          * timestamp, however if it is not available a persistent counter that increases once per
45          * minute should suffice.
46          */
47         public static PeerManager of(ChannelMessageHandler message_handler_chan_handler_arg, RoutingMessageHandler message_handler_route_handler_arg, OnionMessageHandler message_handler_onion_message_handler_arg, CustomMessageHandler message_handler_custom_message_handler_arg, int current_time, byte[] ephemeral_random_data, org.ldk.structs.Logger logger, org.ldk.structs.NodeSigner node_signer) {
48                 long ret = bindings.PeerManager_new(bindings.MessageHandler_new(message_handler_chan_handler_arg.ptr, message_handler_route_handler_arg.ptr, message_handler_onion_message_handler_arg.ptr, message_handler_custom_message_handler_arg.ptr), current_time, InternalUtils.check_arr_len(ephemeral_random_data, 32), logger.ptr, node_signer.ptr);
49                 GC.KeepAlive(message_handler_chan_handler_arg);
50                 GC.KeepAlive(message_handler_route_handler_arg);
51                 GC.KeepAlive(message_handler_onion_message_handler_arg);
52                 GC.KeepAlive(message_handler_custom_message_handler_arg);
53                 GC.KeepAlive(current_time);
54                 GC.KeepAlive(ephemeral_random_data);
55                 GC.KeepAlive(logger);
56                 GC.KeepAlive(node_signer);
57                 if (ret >= 0 && ret <= 4096) { return null; }
58                 org.ldk.structs.PeerManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.PeerManager(null, ret); }
59                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
60                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(message_handler_chan_handler_arg); };
61                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(message_handler_route_handler_arg); };
62                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(message_handler_onion_message_handler_arg); };
63                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(message_handler_custom_message_handler_arg); };
64                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
65                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(node_signer); };
66                 return ret_hu_conv;
67         }
68
69         /**
70          * Get a list of tuples mapping from node id to network addresses for peers which have
71          * completed the initial handshake.
72          * 
73          * For outbound connections, the [`PublicKey`] will be the same as the `their_node_id` parameter
74          * passed in to [`Self::new_outbound_connection`], however entries will only appear once the initial
75          * handshake has completed and we are sure the remote peer has the private key for the given
76          * [`PublicKey`].
77          * 
78          * The returned `Option`s will only be `Some` if an address had been previously given via
79          * [`Self::new_outbound_connection`] or [`Self::new_inbound_connection`].
80          */
81         public TwoTuple_PublicKeyCOption_NetAddressZZ[] get_peer_node_ids() {
82                 long[] ret = bindings.PeerManager_get_peer_node_ids(this.ptr);
83                 GC.KeepAlive(this);
84                 int ret_conv_40_len = ret.Length;
85                 TwoTuple_PublicKeyCOption_NetAddressZZ[] ret_conv_40_arr = new TwoTuple_PublicKeyCOption_NetAddressZZ[ret_conv_40_len];
86                 for (int o = 0; o < ret_conv_40_len; o++) {
87                         long ret_conv_40 = ret[o];
88                         TwoTuple_PublicKeyCOption_NetAddressZZ ret_conv_40_hu_conv = new TwoTuple_PublicKeyCOption_NetAddressZZ(null, ret_conv_40);
89                         if (ret_conv_40_hu_conv != null) { ret_conv_40_hu_conv.ptrs_to.AddLast(this); };
90                         ret_conv_40_arr[o] = ret_conv_40_hu_conv;
91                 }
92                 return ret_conv_40_arr;
93         }
94
95         /**
96          * Indicates a new outbound connection has been established to a node with the given `node_id`
97          * and an optional remote network address.
98          * 
99          * The remote network address adds the option to report a remote IP address back to a connecting
100          * peer using the init message.
101          * The user should pass the remote network address of the host they are connected to.
102          * 
103          * If an `Err` is returned here you must disconnect the connection immediately.
104          * 
105          * Returns a small number of bytes to send to the remote node (currently always 50).
106          * 
107          * Panics if descriptor is duplicative with some other descriptor which has not yet been
108          * [`socket_disconnected`].
109          * 
110          * [`socket_disconnected`]: PeerManager::socket_disconnected
111          */
112         public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_NetAddressZ remote_network_address) {
113                 long ret = bindings.PeerManager_new_outbound_connection(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), descriptor.ptr, remote_network_address.ptr);
114                 GC.KeepAlive(this);
115                 GC.KeepAlive(their_node_id);
116                 GC.KeepAlive(descriptor);
117                 GC.KeepAlive(remote_network_address);
118                 if (ret >= 0 && ret <= 4096) { return null; }
119                 Result_CVec_u8ZPeerHandleErrorZ ret_hu_conv = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret);
120                 if (this != null) { this.ptrs_to.AddLast(descriptor); };
121                 if (this != null) { this.ptrs_to.AddLast(remote_network_address); };
122                 return ret_hu_conv;
123         }
124
125         /**
126          * Indicates a new inbound connection has been established to a node with an optional remote
127          * network address.
128          * 
129          * The remote network address adds the option to report a remote IP address back to a connecting
130          * peer using the init message.
131          * The user should pass the remote network address of the host they are connected to.
132          * 
133          * May refuse the connection by returning an Err, but will never write bytes to the remote end
134          * (outbound connector always speaks first). If an `Err` is returned here you must disconnect
135          * the connection immediately.
136          * 
137          * Panics if descriptor is duplicative with some other descriptor which has not yet been
138          * [`socket_disconnected`].
139          * 
140          * [`socket_disconnected`]: PeerManager::socket_disconnected
141          */
142         public Result_NonePeerHandleErrorZ new_inbound_connection(org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_NetAddressZ remote_network_address) {
143                 long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor.ptr, remote_network_address.ptr);
144                 GC.KeepAlive(this);
145                 GC.KeepAlive(descriptor);
146                 GC.KeepAlive(remote_network_address);
147                 if (ret >= 0 && ret <= 4096) { return null; }
148                 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
149                 if (this != null) { this.ptrs_to.AddLast(descriptor); };
150                 if (this != null) { this.ptrs_to.AddLast(remote_network_address); };
151                 return ret_hu_conv;
152         }
153
154         /**
155          * Indicates that there is room to write data to the given socket descriptor.
156          * 
157          * May return an Err to indicate that the connection should be closed.
158          * 
159          * May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
160          * returning. Thus, be very careful with reentrancy issues! The invariants around calling
161          * [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
162          * ready to call [`write_buffer_space_avail`] again if a write call generated here isn't
163          * sufficient!
164          * 
165          * [`send_data`]: SocketDescriptor::send_data
166          * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
167          */
168         public Result_NonePeerHandleErrorZ write_buffer_space_avail(org.ldk.structs.SocketDescriptor descriptor) {
169                 long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor.ptr);
170                 GC.KeepAlive(this);
171                 GC.KeepAlive(descriptor);
172                 if (ret >= 0 && ret <= 4096) { return null; }
173                 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
174                 return ret_hu_conv;
175         }
176
177         /**
178          * Indicates that data was read from the given socket descriptor.
179          * 
180          * May return an Err to indicate that the connection should be closed.
181          * 
182          * Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity.
183          * Thus, however, you should call [`process_events`] after any `read_event` to generate
184          * [`send_data`] calls to handle responses.
185          * 
186          * If `Ok(true)` is returned, further read_events should not be triggered until a
187          * [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
188          * send buffer).
189          * 
190          * In order to avoid processing too many messages at once per peer, `data` should be on the
191          * order of 4KiB.
192          * 
193          * [`send_data`]: SocketDescriptor::send_data
194          * [`process_events`]: PeerManager::process_events
195          */
196         public Result_boolPeerHandleErrorZ read_event(org.ldk.structs.SocketDescriptor peer_descriptor, byte[] data) {
197                 long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor.ptr, data);
198                 GC.KeepAlive(this);
199                 GC.KeepAlive(peer_descriptor);
200                 GC.KeepAlive(data);
201                 if (ret >= 0 && ret <= 4096) { return null; }
202                 Result_boolPeerHandleErrorZ ret_hu_conv = Result_boolPeerHandleErrorZ.constr_from_ptr(ret);
203                 return ret_hu_conv;
204         }
205
206         /**
207          * Checks for any events generated by our handlers and processes them. Includes sending most
208          * response messages as well as messages generated by calls to handler functions directly (eg
209          * functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
210          * 
211          * May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
212          * issues!
213          * 
214          * You don't have to call this function explicitly if you are using [`lightning-net-tokio`]
215          * or one of the other clients provided in our language bindings.
216          * 
217          * Note that if there are any other calls to this function waiting on lock(s) this may return
218          * without doing any work. All available events that need handling will be handled before the
219          * other calls return.
220          * 
221          * [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
222          * [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
223          * [`send_data`]: SocketDescriptor::send_data
224          */
225         public void process_events() {
226                 bindings.PeerManager_process_events(this.ptr);
227                 GC.KeepAlive(this);
228         }
229
230         /**
231          * Indicates that the given socket descriptor's connection is now closed.
232          */
233         public void socket_disconnected(org.ldk.structs.SocketDescriptor descriptor) {
234                 bindings.PeerManager_socket_disconnected(this.ptr, descriptor.ptr);
235                 GC.KeepAlive(this);
236                 GC.KeepAlive(descriptor);
237         }
238
239         /**
240          * Disconnect a peer given its node id.
241          * 
242          * If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
243          * peer. Thus, be very careful about reentrancy issues.
244          * 
245          * [`disconnect_socket`]: SocketDescriptor::disconnect_socket
246          */
247         public void disconnect_by_node_id(byte[] node_id) {
248                 bindings.PeerManager_disconnect_by_node_id(this.ptr, InternalUtils.check_arr_len(node_id, 33));
249                 GC.KeepAlive(this);
250                 GC.KeepAlive(node_id);
251         }
252
253         /**
254          * Disconnects all currently-connected peers. This is useful on platforms where there may be
255          * an indication that TCP sockets have stalled even if we weren't around to time them out
256          * using regular ping/pongs.
257          */
258         public void disconnect_all_peers() {
259                 bindings.PeerManager_disconnect_all_peers(this.ptr);
260                 GC.KeepAlive(this);
261         }
262
263         /**
264          * Send pings to each peer and disconnect those which did not respond to the last round of
265          * pings.
266          * 
267          * This may be called on any timescale you want, however, roughly once every ten seconds is
268          * preferred. The call rate determines both how often we send a ping to our peers and how much
269          * time they have to respond before we disconnect them.
270          * 
271          * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
272          * issues!
273          * 
274          * [`send_data`]: SocketDescriptor::send_data
275          */
276         public void timer_tick_occurred() {
277                 bindings.PeerManager_timer_tick_occurred(this.ptr);
278                 GC.KeepAlive(this);
279         }
280
281         /**
282          * Generates a signed node_announcement from the given arguments, sending it to all connected
283          * peers. Note that peers will likely ignore this message unless we have at least one public
284          * channel which has at least six confirmations on-chain.
285          * 
286          * `rgb` is a node \"color\" and `alias` is a printable human-readable string to describe this
287          * node to humans. They carry no in-protocol meaning.
288          * 
289          * `addresses` represent the set (possibly empty) of socket addresses on which this node
290          * accepts incoming connections. These will be included in the node_announcement, publicly
291          * tying these addresses together and to this node. If you wish to preserve user privacy,
292          * addresses should likely contain only Tor Onion addresses.
293          * 
294          * Panics if `addresses` is absurdly large (more than 100).
295          * 
296          * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events
297          */
298         public void broadcast_node_announcement(byte[] rgb, byte[] alias, NetAddress[] addresses) {
299                 bindings.PeerManager_broadcast_node_announcement(this.ptr, InternalUtils.check_arr_len(rgb, 3), InternalUtils.check_arr_len(alias, 32), addresses != null ? InternalUtils.mapArray(addresses, addresses_conv_12 => addresses_conv_12.ptr) : null);
300                 GC.KeepAlive(this);
301                 GC.KeepAlive(rgb);
302                 GC.KeepAlive(alias);
303                 GC.KeepAlive(addresses);
304                 foreach (NetAddress addresses_conv_12 in addresses) { if (this != null) { this.ptrs_to.AddLast(addresses_conv_12); }; };
305         }
306
307 }
308 } } }