X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fldk%2Fstructs%2FPeerManager.java;h=e41024f8ed7b6397687d8a0748103959b8a56cee;hb=ff3dacec3be60f870d81f6df11bd9fff92aa6047;hp=285ac3bcfd8198c32c0f7e4c35f3586f2dc2285c;hpb=32692e00046b7ec16b6a2e20bd54800b2b48d4c0;p=ldk-java diff --git a/src/main/java/org/ldk/structs/PeerManager.java b/src/main/java/org/ldk/structs/PeerManager.java index 285ac3bc..e41024f8 100644 --- a/src/main/java/org/ldk/structs/PeerManager.java +++ b/src/main/java/org/ldk/structs/PeerManager.java @@ -4,6 +4,7 @@ import org.ldk.impl.bindings; import org.ldk.enums.*; import org.ldk.util.*; import java.util.Arrays; +import java.lang.ref.Reference; import javax.annotation.Nullable; @@ -43,9 +44,15 @@ public class PeerManager extends CommonBase { * cryptographically secure random bytes. */ public static PeerManager of(ChannelMessageHandler message_handler_chan_handler_arg, RoutingMessageHandler message_handler_route_handler_arg, byte[] our_node_secret, byte[] ephemeral_random_data, Logger logger, CustomMessageHandler custom_message_handler) { - 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), our_node_secret, ephemeral_random_data, logger == null ? 0 : logger.ptr, custom_message_handler == null ? 0 : custom_message_handler.ptr); - if (ret >= 0 && ret < 1024) { return null; } - PeerManager ret_hu_conv = new PeerManager(null, ret); + 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), InternalUtils.check_arr_len(our_node_secret, 32), InternalUtils.check_arr_len(ephemeral_random_data, 32), logger == null ? 0 : logger.ptr, custom_message_handler == null ? 0 : custom_message_handler.ptr); + Reference.reachabilityFence(message_handler_chan_handler_arg); + Reference.reachabilityFence(message_handler_route_handler_arg); + Reference.reachabilityFence(our_node_secret); + Reference.reachabilityFence(ephemeral_random_data); + Reference.reachabilityFence(logger); + Reference.reachabilityFence(custom_message_handler); + if (ret >= 0 && ret <= 4096) { return null; } + PeerManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new PeerManager(null, ret); } ret_hu_conv.ptrs_to.add(ret_hu_conv); ret_hu_conv.ptrs_to.add(message_handler_chan_handler_arg); ret_hu_conv.ptrs_to.add(message_handler_route_handler_arg); @@ -63,11 +70,18 @@ public class PeerManager extends CommonBase { */ public byte[][] get_peer_node_ids() { byte[][] ret = bindings.PeerManager_get_peer_node_ids(this.ptr); + Reference.reachabilityFence(this); return ret; } /** - * Indicates a new outbound connection has been established to a node with the given node_id. + * Indicates a new outbound connection has been established to a node with the given node_id + * and an optional remote network address. + * + * The remote network address adds the option to report a remote IP address back to a connecting + * peer using the init message. + * The user should pass the remote network address of the host they are connected to. + * * Note that if an Err is returned here you MUST NOT call socket_disconnected for the new * descriptor but must disconnect the connection immediately. * @@ -78,16 +92,25 @@ public class PeerManager extends CommonBase { * * [`socket_disconnected()`]: PeerManager::socket_disconnected */ - public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, SocketDescriptor descriptor) { - long ret = bindings.PeerManager_new_outbound_connection(this.ptr, their_node_id, descriptor == null ? 0 : descriptor.ptr); - if (ret >= 0 && ret < 1024) { return null; } + public Result_CVec_u8ZPeerHandleErrorZ new_outbound_connection(byte[] their_node_id, SocketDescriptor descriptor, Option_NetAddressZ remote_network_address) { + 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); + Reference.reachabilityFence(this); + Reference.reachabilityFence(their_node_id); + Reference.reachabilityFence(descriptor); + Reference.reachabilityFence(remote_network_address); + if (ret >= 0 && ret <= 4096) { return null; } Result_CVec_u8ZPeerHandleErrorZ ret_hu_conv = Result_CVec_u8ZPeerHandleErrorZ.constr_from_ptr(ret); this.ptrs_to.add(descriptor); return ret_hu_conv; } /** - * Indicates a new inbound connection has been established. + * Indicates a new inbound connection has been established to a node with an optional remote + * network address. + * + * The remote network address adds the option to report a remote IP address back to a connecting + * peer using the init message. + * The user should pass the remote network address of the host they are connected to. * * May refuse the connection by returning an Err, but will never write bytes to the remote end * (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT @@ -99,9 +122,12 @@ public class PeerManager extends CommonBase { * * [`socket_disconnected()`]: PeerManager::socket_disconnected */ - public Result_NonePeerHandleErrorZ new_inbound_connection(SocketDescriptor descriptor) { - long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor == null ? 0 : descriptor.ptr); - if (ret >= 0 && ret < 1024) { return null; } + public Result_NonePeerHandleErrorZ new_inbound_connection(SocketDescriptor descriptor, Option_NetAddressZ remote_network_address) { + long ret = bindings.PeerManager_new_inbound_connection(this.ptr, descriptor == null ? 0 : descriptor.ptr, remote_network_address.ptr); + Reference.reachabilityFence(this); + Reference.reachabilityFence(descriptor); + Reference.reachabilityFence(remote_network_address); + if (ret >= 0 && ret <= 4096) { return null; } Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret); this.ptrs_to.add(descriptor); return ret_hu_conv; @@ -123,7 +149,9 @@ public class PeerManager extends CommonBase { */ public Result_NonePeerHandleErrorZ write_buffer_space_avail(SocketDescriptor descriptor) { long ret = bindings.PeerManager_write_buffer_space_avail(this.ptr, descriptor == null ? 0 : descriptor.ptr); - if (ret >= 0 && ret < 1024) { return null; } + Reference.reachabilityFence(this); + Reference.reachabilityFence(descriptor); + if (ret >= 0 && ret <= 4096) { return null; } Result_NonePeerHandleErrorZ ret_hu_conv = Result_NonePeerHandleErrorZ.constr_from_ptr(ret); return ret_hu_conv; } @@ -146,7 +174,10 @@ public class PeerManager extends CommonBase { */ public Result_boolPeerHandleErrorZ read_event(SocketDescriptor peer_descriptor, byte[] data) { long ret = bindings.PeerManager_read_event(this.ptr, peer_descriptor == null ? 0 : peer_descriptor.ptr, data); - if (ret >= 0 && ret < 1024) { return null; } + Reference.reachabilityFence(this); + Reference.reachabilityFence(peer_descriptor); + Reference.reachabilityFence(data); + if (ret >= 0 && ret <= 4096) { return null; } Result_boolPeerHandleErrorZ ret_hu_conv = Result_boolPeerHandleErrorZ.constr_from_ptr(ret); return ret_hu_conv; } @@ -168,6 +199,7 @@ public class PeerManager extends CommonBase { */ public void process_events() { bindings.PeerManager_process_events(this.ptr); + Reference.reachabilityFence(this); } /** @@ -175,6 +207,8 @@ public class PeerManager extends CommonBase { */ public void socket_disconnected(SocketDescriptor descriptor) { bindings.PeerManager_socket_disconnected(this.ptr, descriptor == null ? 0 : descriptor.ptr); + Reference.reachabilityFence(this); + Reference.reachabilityFence(descriptor); } /** @@ -189,16 +223,29 @@ public class PeerManager extends CommonBase { * [`disconnect_socket`]: SocketDescriptor::disconnect_socket */ public void disconnect_by_node_id(byte[] node_id, boolean no_connection_possible) { - bindings.PeerManager_disconnect_by_node_id(this.ptr, node_id, no_connection_possible); + bindings.PeerManager_disconnect_by_node_id(this.ptr, InternalUtils.check_arr_len(node_id, 33), no_connection_possible); + Reference.reachabilityFence(this); + Reference.reachabilityFence(node_id); + Reference.reachabilityFence(no_connection_possible); + } + + /** + * Disconnects all currently-connected peers. This is useful on platforms where there may be + * an indication that TCP sockets have stalled even if we weren't around to time them out + * using regular ping/pongs. + */ + public void disconnect_all_peers() { + bindings.PeerManager_disconnect_all_peers(this.ptr); + Reference.reachabilityFence(this); } /** * Send pings to each peer and disconnect those which did not respond to the last round of * pings. * - * This may be called on any timescale you want, however, roughly once every five to ten - * seconds is preferred. The call rate determines both how often we send a ping to our peers - * and how much time they have to respond before we disconnect them. + * This may be called on any timescale you want, however, roughly once every ten seconds is + * preferred. The call rate determines both how often we send a ping to our peers and how much + * time they have to respond before we disconnect them. * * May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy * issues! @@ -207,6 +254,7 @@ public class PeerManager extends CommonBase { */ public void timer_tick_occurred() { bindings.PeerManager_timer_tick_occurred(this.ptr); + Reference.reachabilityFence(this); } }