6 namespace org { namespace ldk { namespace structs {
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`].
13 * Locks are taken internally, so you must never assume that reentrancy from a
14 * [`SocketDescriptor`] call back into [`PeerManager`] methods will not deadlock.
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.
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.
28 * [`read_event`]: PeerManager::read_event
30 public class PeerManager : CommonBase {
31 internal PeerManager(object _dummy, long ptr) : base(ptr) { }
33 if (ptr != 0) { bindings.PeerManager_free(ptr); }
37 * Constructs a new `PeerManager` with the given message handlers.
39 * `ephemeral_random_data` is used to derive per-connection ephemeral keys and must be
40 * cryptographically secure random bytes.
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.
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.encodeUint8Array(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);
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); };
70 * Get a list of tuples mapping from node id to network addresses for peers which have
71 * completed the initial handshake.
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
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`].
81 public TwoTuple_PublicKeyCOption_SocketAddressZZ[] get_peer_node_ids() {
82 long ret = bindings.PeerManager_get_peer_node_ids(this.ptr);
84 if (ret >= 0 && ret <= 4096) { return null; }
85 int ret_conv_43_len = InternalUtils.getArrayLength(ret);
86 TwoTuple_PublicKeyCOption_SocketAddressZZ[] ret_conv_43_arr = new TwoTuple_PublicKeyCOption_SocketAddressZZ[ret_conv_43_len];
87 for (int r = 0; r < ret_conv_43_len; r++) {
88 long ret_conv_43 = InternalUtils.getU64ArrayElem(ret, r);
89 TwoTuple_PublicKeyCOption_SocketAddressZZ ret_conv_43_hu_conv = new TwoTuple_PublicKeyCOption_SocketAddressZZ(null, ret_conv_43);
90 if (ret_conv_43_hu_conv != null) { ret_conv_43_hu_conv.ptrs_to.AddLast(this); };
91 ret_conv_43_arr[r] = ret_conv_43_hu_conv;
93 bindings.free_buffer(ret);
94 return ret_conv_43_arr;
98 * Indicates a new outbound connection has been established to a node with the given `node_id`
99 * and an optional remote network address.
101 * The remote network address adds the option to report a remote IP address back to a connecting
102 * peer using the init message.
103 * The user should pass the remote network address of the host they are connected to.
105 * If an `Err` is returned here you must disconnect the connection immediately.
107 * Returns a small number of bytes to send to the remote node (currently always 50).
109 * Panics if descriptor is duplicative with some other descriptor which has not yet been
110 * [`socket_disconnected`].
112 * [`socket_disconnected`]: PeerManager::socket_disconnected
114 public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_SocketAddressZ remote_network_address) {
115 long ret = bindings.PeerManager_new_outbound_connection(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(their_node_id, 33)), descriptor.ptr, remote_network_address.ptr);
117 GC.KeepAlive(their_node_id);
118 GC.KeepAlive(descriptor);
119 GC.KeepAlive(remote_network_address);
120 if (ret >= 0 && ret <= 4096) { return null; }
121 Result_CVec_u8ZPeerHandleErrorZ ret_hu_conv = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret);
122 if (this != null) { this.ptrs_to.AddLast(descriptor); };
123 if (this != null) { this.ptrs_to.AddLast(remote_network_address); };
128 * Indicates a new inbound connection has been established to a node with an optional remote
131 * The remote network address adds the option to report a remote IP address back to a connecting
132 * peer using the init message.
133 * The user should pass the remote network address of the host they are connected to.
135 * May refuse the connection by returning an Err, but will never write bytes to the remote end
136 * (outbound connector always speaks first). If an `Err` is returned here you must disconnect
137 * the connection immediately.
139 * Panics if descriptor is duplicative with some other descriptor which has not yet been
140 * [`socket_disconnected`].
142 * [`socket_disconnected`]: PeerManager::socket_disconnected
144 public Result_NonePeerHandleErrorZ new_inbound_connection(org.ldk.structs.SocketDescriptor descriptor, org.ldk.structs.Option_SocketAddressZ remote_network_address) {
145 long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor.ptr, remote_network_address.ptr);
147 GC.KeepAlive(descriptor);
148 GC.KeepAlive(remote_network_address);
149 if (ret >= 0 && ret <= 4096) { return null; }
150 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
151 if (this != null) { this.ptrs_to.AddLast(descriptor); };
152 if (this != null) { this.ptrs_to.AddLast(remote_network_address); };
157 * Indicates that there is room to write data to the given socket descriptor.
159 * May return an Err to indicate that the connection should be closed.
161 * May call [`send_data`] on the descriptor passed in (or an equal descriptor) before
162 * returning. Thus, be very careful with reentrancy issues! The invariants around calling
163 * [`write_buffer_space_avail`] in case a write did not fully complete must still hold - be
164 * ready to call [`write_buffer_space_avail`] again if a write call generated here isn't
167 * [`send_data`]: SocketDescriptor::send_data
168 * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
170 public Result_NonePeerHandleErrorZ write_buffer_space_avail(org.ldk.structs.SocketDescriptor descriptor) {
171 long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor.ptr);
173 GC.KeepAlive(descriptor);
174 if (ret >= 0 && ret <= 4096) { return null; }
175 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
180 * Indicates that data was read from the given socket descriptor.
182 * May return an Err to indicate that the connection should be closed.
184 * Will *not* call back into [`send_data`] on any descriptors to avoid reentrancy complexity.
185 * Thus, however, you should call [`process_events`] after any `read_event` to generate
186 * [`send_data`] calls to handle responses.
188 * If `Ok(true)` is returned, further read_events should not be triggered until a
189 * [`send_data`] call on this descriptor has `resume_read` set (preventing DoS issues in the
192 * In order to avoid processing too many messages at once per peer, `data` should be on the
195 * [`send_data`]: SocketDescriptor::send_data
196 * [`process_events`]: PeerManager::process_events
198 public Result_boolPeerHandleErrorZ read_event(org.ldk.structs.SocketDescriptor peer_descriptor, byte[] data) {
199 long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor.ptr, InternalUtils.encodeUint8Array(data));
201 GC.KeepAlive(peer_descriptor);
203 if (ret >= 0 && ret <= 4096) { return null; }
204 Result_boolPeerHandleErrorZ ret_hu_conv = Result_boolPeerHandleErrorZ.constr_from_ptr(ret);
209 * Checks for any events generated by our handlers and processes them. Includes sending most
210 * response messages as well as messages generated by calls to handler functions directly (eg
211 * functions like [`ChannelManager::process_pending_htlc_forwards`] or [`send_payment`]).
213 * May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
216 * You don't have to call this function explicitly if you are using [`lightning-net-tokio`]
217 * or one of the other clients provided in our language bindings.
219 * Note that if there are any other calls to this function waiting on lock(s) this may return
220 * without doing any work. All available events that need handling will be handled before the
221 * other calls return.
223 * [`send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
224 * [`ChannelManager::process_pending_htlc_forwards`]: crate::ln::channelmanager::ChannelManager::process_pending_htlc_forwards
225 * [`send_data`]: SocketDescriptor::send_data
227 public void process_events() {
228 bindings.PeerManager_process_events(this.ptr);
233 * Indicates that the given socket descriptor's connection is now closed.
235 public void socket_disconnected(org.ldk.structs.SocketDescriptor descriptor) {
236 bindings.PeerManager_socket_disconnected(this.ptr, descriptor.ptr);
238 GC.KeepAlive(descriptor);
242 * Disconnect a peer given its node id.
244 * If a peer is connected, this will call [`disconnect_socket`] on the descriptor for the
245 * peer. Thus, be very careful about reentrancy issues.
247 * [`disconnect_socket`]: SocketDescriptor::disconnect_socket
249 public void disconnect_by_node_id(byte[] node_id) {
250 bindings.PeerManager_disconnect_by_node_id(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(node_id, 33)));
252 GC.KeepAlive(node_id);
256 * Disconnects all currently-connected peers. This is useful on platforms where there may be
257 * an indication that TCP sockets have stalled even if we weren't around to time them out
258 * using regular ping/pongs.
260 public void disconnect_all_peers() {
261 bindings.PeerManager_disconnect_all_peers(this.ptr);
266 * Send pings to each peer and disconnect those which did not respond to the last round of
269 * This may be called on any timescale you want, however, roughly once every ten seconds is
270 * preferred. The call rate determines both how often we send a ping to our peers and how much
271 * time they have to respond before we disconnect them.
273 * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
276 * [`send_data`]: SocketDescriptor::send_data
278 public void timer_tick_occurred() {
279 bindings.PeerManager_timer_tick_occurred(this.ptr);
284 * Generates a signed node_announcement from the given arguments, sending it to all connected
285 * peers. Note that peers will likely ignore this message unless we have at least one public
286 * channel which has at least six confirmations on-chain.
288 * `rgb` is a node \"color\" and `alias` is a printable human-readable string to describe this
289 * node to humans. They carry no in-protocol meaning.
291 * `addresses` represent the set (possibly empty) of socket addresses on which this node
292 * accepts incoming connections. These will be included in the node_announcement, publicly
293 * tying these addresses together and to this node. If you wish to preserve user privacy,
294 * addresses should likely contain only Tor Onion addresses.
296 * Panics if `addresses` is absurdly large (more than 100).
298 * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events
300 public void broadcast_node_announcement(byte[] rgb, byte[] alias, SocketAddress[] addresses) {
301 bindings.PeerManager_broadcast_node_announcement(this.ptr, InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(rgb, 3)), InternalUtils.encodeUint8Array(InternalUtils.check_arr_len(alias, 32)), InternalUtils.encodeUint64Array(InternalUtils.mapArray(addresses, addresses_conv_15 => addresses_conv_15.ptr)));
305 GC.KeepAlive(addresses);
306 foreach (SocketAddress addresses_conv_15 in addresses) { if (this != null) { this.ptrs_to.AddLast(addresses_conv_15); }; };