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.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_NetAddressZZ[] get_peer_node_ids() {
82 long[] ret = bindings.PeerManager_get_peer_node_ids(this.ptr);
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;
92 return ret_conv_40_arr;
96 * Indicates a new outbound connection has been established to a node with the given `node_id`
97 * and an optional remote network address.
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.
103 * If an `Err` is returned here you must disconnect the connection immediately.
105 * Returns a small number of bytes to send to the remote node (currently always 50).
107 * Panics if descriptor is duplicative with some other descriptor which has not yet been
108 * [`socket_disconnected`].
110 * [`socket_disconnected`]: PeerManager::socket_disconnected
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);
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); };
126 * Indicates a new inbound connection has been established to a node with an optional remote
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.
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.
137 * Panics if descriptor is duplicative with some other descriptor which has not yet been
138 * [`socket_disconnected`].
140 * [`socket_disconnected`]: PeerManager::socket_disconnected
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);
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); };
155 * Indicates that there is room to write data to the given socket descriptor.
157 * May return an Err to indicate that the connection should be closed.
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
165 * [`send_data`]: SocketDescriptor::send_data
166 * [`write_buffer_space_avail`]: PeerManager::write_buffer_space_avail
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);
171 GC.KeepAlive(descriptor);
172 if (ret >= 0 && ret <= 4096) { return null; }
173 Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret);
178 * Indicates that data was read from the given socket descriptor.
180 * May return an Err to indicate that the connection should be closed.
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.
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
190 * In order to avoid processing too many messages at once per peer, `data` should be on the
193 * [`send_data`]: SocketDescriptor::send_data
194 * [`process_events`]: PeerManager::process_events
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);
199 GC.KeepAlive(peer_descriptor);
201 if (ret >= 0 && ret <= 4096) { return null; }
202 Result_boolPeerHandleErrorZ ret_hu_conv = Result_boolPeerHandleErrorZ.constr_from_ptr(ret);
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`]).
211 * May call [`send_data`] on [`SocketDescriptor`]s. Thus, be very careful with reentrancy
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.
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.
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
225 public void process_events() {
226 bindings.PeerManager_process_events(this.ptr);
231 * Indicates that the given socket descriptor's connection is now closed.
233 public void socket_disconnected(org.ldk.structs.SocketDescriptor descriptor) {
234 bindings.PeerManager_socket_disconnected(this.ptr, descriptor.ptr);
236 GC.KeepAlive(descriptor);
240 * Disconnect a peer given its node id.
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.
245 * [`disconnect_socket`]: SocketDescriptor::disconnect_socket
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));
250 GC.KeepAlive(node_id);
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.
258 public void disconnect_all_peers() {
259 bindings.PeerManager_disconnect_all_peers(this.ptr);
264 * Send pings to each peer and disconnect those which did not respond to the last round of
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.
271 * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy
274 * [`send_data`]: SocketDescriptor::send_data
276 public void timer_tick_occurred() {
277 bindings.PeerManager_timer_tick_occurred(this.ptr);
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.
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.
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.
294 * Panics if `addresses` is absurdly large (more than 100).
296 * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events
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);
303 GC.KeepAlive(addresses);
304 foreach (NetAddress addresses_conv_12 in addresses) { if (this != null) { this.ptrs_to.AddLast(addresses_conv_12); }; };