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