Skip to content

Commit 6878bf1

Browse files
authored
A send_ping method for measuring latency (#576)
1 parent e491140 commit 6878bf1

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

russh/src/client/encrypted.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,9 @@ impl Session {
744744
Some(GlobalRequestResponse::Keepalive) => {
745745
// ignore keepalives
746746
}
747+
Some(GlobalRequestResponse::Ping(return_channel)) => {
748+
let _ = return_channel.send(());
749+
}
747750
Some(GlobalRequestResponse::NoMoreSessions) => {
748751
debug!("[email protected] requests success");
749752
}
@@ -783,6 +786,9 @@ impl Session {
783786
Some(GlobalRequestResponse::Keepalive) => {
784787
// ignore keepalives
785788
}
789+
Some(GlobalRequestResponse::Ping(return_channel)) => {
790+
let _ = return_channel.send(());
791+
}
786792
Some(GlobalRequestResponse::NoMoreSessions) => {
787793
warn!("[email protected] requests failure");
788794
}

russh/src/client/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ pub enum Msg {
200200
Keepalive {
201201
want_reply: bool,
202202
},
203+
Ping {
204+
reply_channel: oneshot::Sender<()>,
205+
},
203206
NoMoreSessions {
204207
want_reply: bool,
205208
},
@@ -839,6 +842,19 @@ impl<H: Handler> Handle<H> {
839842
.map_err(|_| Error::SendError)
840843
}
841844

845+
/// Send a keepalive/ping package to the remote peer, and wait for the reply/pong.
846+
pub async fn send_ping(&self) -> Result<(), Error> {
847+
let (sender, receiver) = oneshot::channel();
848+
self.sender
849+
.send(Msg::Ping {
850+
reply_channel: sender,
851+
})
852+
.await
853+
.map_err(|_| Error::SendError)?;
854+
let _ = receiver.await;
855+
Ok(())
856+
}
857+
842858
/// Send a no-more-sessions request to the remote peer.
843859
pub async fn no_more_sessions(&self, want_reply: bool) -> Result<(), Error> {
844860
self.sender
@@ -1394,6 +1410,9 @@ impl Session {
13941410
Msg::Keepalive { want_reply } => {
13951411
let _ = self.send_keepalive(want_reply);
13961412
}
1413+
Msg::Ping { reply_channel } => {
1414+
let _ = self.send_ping(reply_channel);
1415+
}
13971416
Msg::NoMoreSessions { want_reply } => {
13981417
let _ = self.no_more_sessions(want_reply);
13991418
}

russh/src/client/session.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,19 @@ impl Session {
416416
Ok(())
417417
}
418418

419+
pub fn send_ping(&mut self, reply_channel: oneshot::Sender<()>) -> Result<(), crate::Error> {
420+
self.open_global_requests
421+
.push_back(crate::session::GlobalRequestResponse::Ping(reply_channel));
422+
if let Some(ref mut enc) = self.common.encrypted {
423+
push_packet!(enc.write, {
424+
msg::GLOBAL_REQUEST.encode(&mut enc.write)?;
425+
"[email protected]".encode(&mut enc.write)?;
426+
(true as u8).encode(&mut enc.write)?;
427+
});
428+
}
429+
Ok(())
430+
}
431+
419432
pub fn no_more_sessions(&mut self, want_reply: bool) -> Result<(), crate::Error> {
420433
self.open_global_requests
421434
.push_back(crate::session::GlobalRequestResponse::NoMoreSessions);

russh/src/session.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ pub(crate) struct NewKeys {
582582
pub(crate) enum GlobalRequestResponse {
583583
/// request was for Keepalive, ignore result
584584
Keepalive,
585+
/// request was for Keepalive but with notification of the result
586+
Ping(oneshot::Sender<()>),
585587
/// request was for NoMoreSessions, disallow additional sessions
586588
NoMoreSessions,
587589
/// request was for TcpIpForward, sends Some(port) for success or None for failure

0 commit comments

Comments
 (0)