Skip to content

Commit ae2ed10

Browse files
committed
refactor(net_crypto): add method to send kill packet
1 parent ed8401b commit ae2ed10

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/toxcore/net_crypto/mod.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,16 @@ impl NetCrypto {
239239
}
240240
}
241241

242+
/// Send kill packet to a connection if it's established or not confirmed.
243+
fn send_kill_packet(&self, connection: &mut CryptoConnection) -> impl Future<Item = (), Error = SendDataError> + Send {
244+
if connection.is_established() || connection.is_not_confirmed() {
245+
let packet_number = connection.send_array.buffer_end;
246+
Either::A(self.send_data_packet(connection, vec![PACKET_ID_KILL], packet_number))
247+
} else {
248+
Either::B(future::ok(()))
249+
}
250+
}
251+
242252
/// Kill a connection sending `PACKET_ID_KILL` packet and removing it from
243253
/// the connections list.
244254
pub fn kill_connection(&self, real_pk: PublicKey) -> impl Future<Item = (), Error = KillConnectionError> {
@@ -247,13 +257,8 @@ impl NetCrypto {
247257
self.clear_keys_by_addr(&connection);
248258
let status_future = self.send_connection_status(&connection, false)
249259
.map_err(|e| e.context(KillConnectionErrorKind::SendToConnectionStatus).into());
250-
let kill_future = if connection.is_established() || connection.is_not_confirmed() {
251-
let packet_number = connection.send_array.buffer_end;
252-
Either::A(self.send_data_packet(&mut connection, vec![PACKET_ID_KILL], packet_number)
253-
.map_err(|e| e.context(KillConnectionErrorKind::SendTo).into()))
254-
} else {
255-
Either::B(future::ok(()))
256-
};
260+
let kill_future = self.send_kill_packet(&mut connection)
261+
.map_err(|e| e.context(KillConnectionErrorKind::SendTo).into());
257262
Either::A(kill_future.join(status_future).map(|_| ()))
258263
} else {
259264
Either::B(future::err(KillConnectionErrorKind::NoConnection.into()))
@@ -518,13 +523,8 @@ impl NetCrypto {
518523
self.clear_keys_by_addr(&connection);
519524
let status_future = self.send_connection_status(&connection, false)
520525
.map_err(|e| e.context(HandlePacketErrorKind::SendToConnectionStatus).into());
521-
let kill_future = if connection.is_established() || connection.is_not_confirmed() {
522-
let packet_number = connection.send_array.buffer_end;
523-
Either::A(self.send_data_packet(&mut connection, vec![PACKET_ID_KILL], packet_number)
524-
.map_err(|e| e.context(HandlePacketErrorKind::SendTo).into()))
525-
} else {
526-
Either::B(future::ok(()))
527-
};
526+
let kill_future = self.send_kill_packet(&mut connection)
527+
.map_err(|e| e.context(HandlePacketErrorKind::SendTo).into());
528528
Either::A(kill_future.join(status_future).map(|_| ()))
529529
} else {
530530
// We received a handshake packet for an existent connection
@@ -923,8 +923,7 @@ impl NetCrypto {
923923
}
924924

925925
if connection.is_established() || connection.is_not_confirmed() {
926-
let packet_number = connection.send_array.buffer_end;
927-
futures.push(Box::new(self.send_data_packet(&mut connection, vec![PACKET_ID_KILL], packet_number)));
926+
futures.push(Box::new(self.send_kill_packet(&mut connection)));
928927
}
929928

930929
return false;

0 commit comments

Comments
 (0)