@@ -49,12 +49,12 @@ pub struct MessageHandler<CM: Deref> where CM::Target: msgs::ChannelMessageHandl
49
49
/// You probably want to just extend an int and put a file descriptor in a struct and implement
50
50
/// send_data. Note that if you are using a higher-level net library that may close() itself, be
51
51
/// careful to ensure you don't have races whereby you might register a new connection with an fd
52
- /// the same as a yet-to-be-disconnect_event ()-ed.
52
+ /// the same as a yet-to-be-socket_disconnected ()-ed.
53
53
pub trait SocketDescriptor : cmp:: Eq + hash:: Hash + Clone {
54
54
/// Attempts to send some data from the given slice to the peer.
55
55
///
56
56
/// Returns the amount of data which was sent, possibly 0 if the socket has since disconnected.
57
- /// Note that in the disconnected case, a disconnect_event must still fire and further write
57
+ /// Note that in the disconnected case, a socket_disconnected must still fire and further write
58
58
/// attempts may occur until that time.
59
59
///
60
60
/// If the returned size is smaller than data.len(), a write_available event must
@@ -67,17 +67,18 @@ pub trait SocketDescriptor : cmp::Eq + hash::Hash + Clone {
67
67
/// *not* imply that further read events should be paused.
68
68
fn send_data ( & mut self , data : & [ u8 ] , resume_read : bool ) -> usize ;
69
69
/// Disconnect the socket pointed to by this SocketDescriptor. Once this function returns, no
70
- /// more calls to write_event , read_event or disconnect_event may be made with this descriptor.
71
- /// No disconnect_event should be generated as a result of this call, though obviously races
72
- /// may occur whereby disconnect_socket is called after a call to disconnect_event but prior to
73
- /// that event completing.
70
+ /// more calls to write_buffer_space_avail , read_event or socket_disconnected may be made with
71
+ /// this descriptor. No socket_disconnected call should be generated as a result of this call,
72
+ /// though obviously races may occur whereby disconnect_socket is called after a call to
73
+ /// socket_disconnected but prior to that event completing.
74
74
fn disconnect_socket ( & mut self ) ;
75
75
}
76
76
77
77
/// Error for PeerManager errors. If you get one of these, you must disconnect the socket and
78
- /// generate no further read/write_events for the descriptor, only triggering a single
79
- /// disconnect_event (unless it was provided in response to a new_*_connection event, in which case
80
- /// no such disconnect_event must be generated and the socket be silently disconencted).
78
+ /// generate no further read_event/write_buffer_space_avail calls for the descriptor, only
79
+ /// triggering a single socket_disconnected call (unless it was provided in response to a
80
+ /// new_*_connection event, in which case no such socket_disconnected() must be generated and the
81
+ /// socket be silently disconencted).
81
82
pub struct PeerHandleError {
82
83
/// Used to indicate that we probably can't make any future connections to this peer, implying
83
84
/// we should go ahead and force-close any channels we have with it.
@@ -201,7 +202,7 @@ macro_rules! encode_msg {
201
202
}
202
203
203
204
/// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
204
- /// PeerIds may repeat, but only after disconnect_event () has been called.
205
+ /// PeerIds may repeat, but only after socket_disconnected () has been called.
205
206
impl < Descriptor : SocketDescriptor , CM : Deref > PeerManager < Descriptor , CM > where CM :: Target : msgs:: ChannelMessageHandler {
206
207
/// Constructs a new PeerManager with the given message handlers and node_id secret key
207
208
/// ephemeral_random_data is used to derive per-connection ephemeral keys and must be
@@ -254,13 +255,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
254
255
}
255
256
256
257
/// Indicates a new outbound connection has been established to a node with the given node_id.
257
- /// Note that if an Err is returned here you MUST NOT call disconnect_event for the new
258
+ /// Note that if an Err is returned here you MUST NOT call socket_disconnected for the new
258
259
/// descriptor but must disconnect the connection immediately.
259
260
///
260
261
/// Returns a small number of bytes to send to the remote node (currently always 50).
261
262
///
262
- /// Panics if descriptor is duplicative with some other descriptor which has not yet has a
263
- /// disconnect_event .
263
+ /// Panics if descriptor is duplicative with some other descriptor which has not yet had a
264
+ /// socket_disconnected() .
264
265
pub fn new_outbound_connection ( & self , their_node_id : PublicKey , descriptor : Descriptor ) -> Result < Vec < u8 > , PeerHandleError > {
265
266
let mut peer_encryptor = PeerChannelEncryptor :: new_outbound ( their_node_id. clone ( ) , self . get_ephemeral_key ( ) ) ;
266
267
let res = peer_encryptor. get_act_one ( ) . to_vec ( ) ;
@@ -294,11 +295,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
294
295
///
295
296
/// May refuse the connection by returning an Err, but will never write bytes to the remote end
296
297
/// (outbound connector always speaks first). Note that if an Err is returned here you MUST NOT
297
- /// call disconnect_event for the new descriptor but must disconnect the connection
298
+ /// call socket_disconnected for the new descriptor but must disconnect the connection
298
299
/// immediately.
299
300
///
300
- /// Panics if descriptor is duplicative with some other descriptor which has not yet has a
301
- /// disconnect_event .
301
+ /// Panics if descriptor is duplicative with some other descriptor which has not yet had
302
+ /// socket_disconnected called .
302
303
pub fn new_inbound_connection ( & self , descriptor : Descriptor ) -> Result < ( ) , PeerHandleError > {
303
304
let peer_encryptor = PeerChannelEncryptor :: new_inbound ( & self . our_node_secret ) ;
304
305
let pending_read_buffer = [ 0 ; 50 ] . to_vec ( ) ; // Noise act one is 50 bytes
@@ -406,10 +407,11 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
406
407
///
407
408
/// Will most likely call send_data on the descriptor passed in (or the descriptor handed into
408
409
/// new_*\_connection) before returning. Thus, be very careful with reentrancy issues! The
409
- /// invariants around calling write_event in case a write did not fully complete must still
410
- /// hold - be ready to call write_event again if a write call generated here isn't sufficient!
411
- /// Panics if the descriptor was not previously registered in a new_\*_connection event.
412
- pub fn write_event ( & self , descriptor : & mut Descriptor ) -> Result < ( ) , PeerHandleError > {
410
+ /// invariants around calling write_buffer_space_avail in case a write did not fully complete
411
+ /// must still hold - be ready to call write_buffer_space_avail again if a write call generated
412
+ /// here isn't sufficient! Panics if the descriptor was not previously registered in a
413
+ /// new_\*_connection event.
414
+ pub fn write_buffer_space_avail ( & self , descriptor : & mut Descriptor ) -> Result < ( ) , PeerHandleError > {
413
415
let mut peers = self . peers . lock ( ) . unwrap ( ) ;
414
416
match peers. peers . get_mut ( descriptor) {
415
417
None => panic ! ( "Descriptor for write_event is not already known to PeerManager" ) ,
@@ -429,8 +431,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
429
431
/// Thus, however, you almost certainly want to call process_events() after any read_event to
430
432
/// generate send_data calls to handle responses.
431
433
///
432
- /// If Ok(true) is returned, further read_events should not be triggered until a write_event on
433
- /// this file descriptor has resume_read set (preventing DoS issues in the send buffer).
434
+ /// If Ok(true) is returned, further read_events should not be triggered until a send_data call
435
+ /// on this file descriptor has resume_read set (preventing DoS issues in the send buffer).
434
436
///
435
437
/// Panics if the descriptor was not previously registered in a new_*_connection event.
436
438
pub fn read_event ( & self , peer_descriptor : & mut Descriptor , data : Vec < u8 > ) -> Result < bool , PeerHandleError > {
@@ -1036,11 +1038,13 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
1036
1038
1037
1039
/// Indicates that the given socket descriptor's connection is now closed.
1038
1040
///
1039
- /// This must be called even if a PeerHandleError was given for a read_event or write_event,
1040
- /// but must NOT be called if a PeerHandleError was provided out of a new_\*\_connection event!
1041
+ /// This must only be called if the socket has been disconnected by the peer or your own
1042
+ /// decision to disconnect it and must NOT be called in any case where other parts of this
1043
+ /// library (eg PeerHandleError, explicit disconnect_socket calls) instruct you to disconnect
1044
+ /// the peer.
1041
1045
///
1042
1046
/// Panics if the descriptor was not previously registered in a successful new_*_connection event.
1043
- pub fn disconnect_event ( & self , descriptor : & Descriptor ) {
1047
+ pub fn socket_disconnected ( & self , descriptor : & Descriptor ) {
1044
1048
self . disconnect_event_internal ( descriptor, false ) ;
1045
1049
}
1046
1050
@@ -1073,10 +1077,12 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
1073
1077
let peers_needing_send = & mut peers. peers_needing_send ;
1074
1078
let node_id_to_descriptor = & mut peers. node_id_to_descriptor ;
1075
1079
let peers = & mut peers. peers ;
1080
+ let mut descriptors_needing_disconnect = Vec :: new ( ) ;
1076
1081
1077
1082
peers. retain ( |descriptor, peer| {
1078
1083
if peer. awaiting_pong {
1079
1084
peers_needing_send. remove ( descriptor) ;
1085
+ descriptors_needing_disconnect. push ( descriptor. clone ( ) ) ;
1080
1086
match peer. their_node_id {
1081
1087
Some ( node_id) => {
1082
1088
node_id_to_descriptor. remove ( & node_id) ;
@@ -1104,6 +1110,10 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
1104
1110
peer. awaiting_pong = true ;
1105
1111
true
1106
1112
} ) ;
1113
+
1114
+ for mut descriptor in descriptors_needing_disconnect. drain ( ..) {
1115
+ descriptor. disconnect_socket ( ) ;
1116
+ }
1107
1117
}
1108
1118
}
1109
1119
}
0 commit comments