X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fln%2Fpeer_handler.rs;h=c0a61bc6e9c050ff317ec531cdcd0d25e329ba0d;hb=68c220f7ee4fa0b6b4a300f56cd30b9b02ebfbdf;hp=c9d1e4835f9e1d68b864cc14e14830c2980a288d;hpb=033f4f3b2750dcd5afc8b158d7474242b29b24f3;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/ln/peer_handler.rs b/lightning-c-bindings/src/lightning/ln/peer_handler.rs index c9d1e48..c0a61bc 100644 --- a/lightning-c-bindings/src/lightning/ln/peer_handler.rs +++ b/lightning-c-bindings/src/lightning/ln/peer_handler.rs @@ -522,16 +522,28 @@ pub struct SocketDescriptor { /// Calculate a succinct non-cryptographic hash for an object given its this_arg pointer. /// This is used, for example, for inclusion of this object in a hash map. pub hash: extern "C" fn (this_arg: *const c_void) -> u64, - /// Creates a copy of the object pointed to by this_arg, for a copy of this SocketDescriptor. - /// Note that the ultimate copy of the SocketDescriptor will have all function pointers the same as the original. - /// May be NULL if no action needs to be taken, the this_arg pointer will be copied into the new SocketDescriptor. - pub clone: Option *mut c_void>, + /// Called, if set, after this SocketDescriptor has been cloned into a duplicate object. + /// The new SocketDescriptor is provided, and should be mutated as needed to perform a + /// deep copy of the object pointed to by this_arg or avoid any double-freeing. + pub cloned: Option, /// Frees any resources associated with this object given its this_arg pointer. /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed. pub free: Option, } unsafe impl Send for SocketDescriptor {} unsafe impl Sync for SocketDescriptor {} +#[no_mangle] +pub(crate) extern "C" fn SocketDescriptor_clone_fields(orig: &SocketDescriptor) -> SocketDescriptor { + SocketDescriptor { + this_arg: orig.this_arg, + send_data: Clone::clone(&orig.send_data), + disconnect_socket: Clone::clone(&orig.disconnect_socket), + eq: Clone::clone(&orig.eq), + hash: Clone::clone(&orig.hash), + cloned: Clone::clone(&orig.cloned), + free: Clone::clone(&orig.free), + } +} impl std::cmp::Eq for SocketDescriptor {} impl std::cmp::PartialEq for SocketDescriptor { fn eq(&self, o: &Self) -> bool { (self.eq)(self.this_arg, o) } @@ -542,15 +554,9 @@ impl std::hash::Hash for SocketDescriptor { #[no_mangle] /// Creates a copy of a SocketDescriptor pub extern "C" fn SocketDescriptor_clone(orig: &SocketDescriptor) -> SocketDescriptor { - SocketDescriptor { - this_arg: if let Some(f) = orig.clone { (f)(orig.this_arg) } else { orig.this_arg }, - send_data: Clone::clone(&orig.send_data), - disconnect_socket: Clone::clone(&orig.disconnect_socket), - eq: Clone::clone(&orig.eq), - hash: Clone::clone(&orig.hash), - clone: Clone::clone(&orig.clone), - free: Clone::clone(&orig.free), - } + let mut res = SocketDescriptor_clone_fields(orig); + if let Some(f) = orig.cloned { (f)(&mut res) }; + res } impl Clone for SocketDescriptor { fn clone(&self) -> Self { @@ -875,9 +881,12 @@ pub extern "C" fn PeerManager_disconnect_by_node_id(this_arg: &PeerManager, mut unsafe { &*this_arg.inner }.disconnect_by_node_id(node_id.into_rust(), no_connection_possible) } -/// This function should be called roughly once every 30 seconds. -/// It will send pings to each peer and disconnect those which did not respond to the last -/// round of pings. +/// 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. /// /// May call [`send_data`] on all [`SocketDescriptor`]s. Thus, be very careful with reentrancy /// issues!