From b35b5577d40d27a65cefc62fbd1e15549eee42ef Mon Sep 17 00:00:00 2001
From: Matt Johnston <matt@ucc.asn.au>
Date: Mon, 27 Jun 2022 23:58:22 +0800
Subject: [PATCH] Move behaviour into runner

---
 async/src/async_door.rs | 15 ++++++---------
 async/src/client.rs     |  4 ++--
 sshproto/src/runner.rs  | 10 +++++++---
 3 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/async/src/async_door.rs b/async/src/async_door.rs
index fbb996c..3db916a 100644
--- a/async/src/async_door.rs
+++ b/async/src/async_door.rs
@@ -19,18 +19,15 @@ use core::ops::DerefMut;
 use std::sync::Arc;
 
 use door_sshproto as door;
-use door::{Behaviour, AsyncCliBehaviour, Runner, Result, Event, ChanEvent};
-// use door_sshproto::client::*;
+use door::{Runner, Result, Event, ChanEvent};
 
 use pretty_hex::PrettyHex;
 
 pub(crate) struct Inner<'a> {
     pub runner: Runner<'a>,
-    // TODO: perhaps behaviour can move to runner? unsure of lifetimes.
-    behaviour: Behaviour<'a>,
 
-    pub(crate) chan_read_wakers: HashMap<(u32, Option<u32>), Waker>,
-    pub(crate) chan_write_wakers: HashMap<(u32, Option<u32>), Waker>,
+    pub chan_read_wakers: HashMap<(u32, Option<u32>), Waker>,
+    pub chan_write_wakers: HashMap<(u32, Option<u32>), Waker>,
 }
 
 pub struct AsyncDoor<'a> {
@@ -43,10 +40,10 @@ pub struct AsyncDoor<'a> {
 }
 
 impl<'a> AsyncDoor<'a> {
-    pub fn new(runner: Runner<'a>, behaviour: Behaviour<'a>) -> Self {
+    pub fn new(runner: Runner<'a>) -> Self {
         let chan_read_wakers = HashMap::new();
         let chan_write_wakers = HashMap::new();
-        let inner = Arc::new(Mutex::new(Inner { runner, behaviour,
+        let inner = Arc::new(Mutex::new(Inner { runner,
             chan_read_wakers, chan_write_wakers }));
         let progress_notify = Arc::new(TokioNotify::new());
         Self { inner, progress_notify }
@@ -70,7 +67,7 @@ impl<'a> AsyncDoor<'a> {
         let res = {
             let mut inner = self.inner.lock().await;
             let inner = inner.deref_mut();
-            let ev = inner.runner.progress(&mut inner.behaviour).await?;
+            let ev = inner.runner.progress().await?;
             let r = if let Some(ev) = ev {
                 let r = match ev {
                     Event::Channel(ChanEvent::Eof { num }) => {
diff --git a/async/src/client.rs b/async/src/client.rs
index ef5a565..463441c 100644
--- a/async/src/client.rs
+++ b/async/src/client.rs
@@ -31,9 +31,9 @@ pub struct SSHClient<'a> {
 
 impl<'a> SSHClient<'a> {
     pub fn new(buf: &'a mut [u8], behaviour: Box<dyn AsyncCliBehaviour+Send>) -> Result<Self> {
-        let runner = Runner::new_client(buf)?;
         let b = Behaviour::new_async_client(behaviour);
-        let door = AsyncDoor::new(runner, b);
+        let runner = Runner::new_client(buf, b)?;
+        let door = AsyncDoor::new(runner);
         Ok(Self {
             door
         })
diff --git a/sshproto/src/runner.rs b/sshproto/src/runner.rs
index 0135831..50a2993 100644
--- a/sshproto/src/runner.rs
+++ b/sshproto/src/runner.rs
@@ -18,6 +18,8 @@ use channel::ChanEventMaker;
 pub struct Runner<'a> {
     conn: Conn<'a>,
 
+    behaviour: Behaviour<'a>,
+
     /// Binary packet handling to and from the network buffer
     traffic: Traffic<'a>,
 
@@ -32,6 +34,7 @@ impl<'a> Runner<'a> {
     /// `iobuf` must be sized to fit the largest SSH packet allowed.
     pub fn new_client(
         iobuf: &'a mut [u8],
+        behaviour: Behaviour<'a>,
     ) -> Result<Runner<'a>, Error> {
         let conn = Conn::new_client()?;
         let runner = Runner {
@@ -40,6 +43,7 @@ impl<'a> Runner<'a> {
             keys: KeyState::new_cleartext(),
             output_waker: None,
             input_waker: None,
+            behaviour,
         };
 
         Ok(runner)
@@ -69,7 +73,7 @@ impl<'a> Runner<'a> {
     /// Optionally returns `Event` which provides channel or session
     /// event to the application.
     /// [`done_payload()`] must be called after any `Ok` result.
-    pub async fn progress<'f>(&'f mut self, b: &mut Behaviour<'_>) -> Result<Option<Event<'f>>, Error> {
+    pub async fn progress<'f>(&'f mut self) -> Result<Option<Event<'f>>, Error> {
         let em = if let Some(payload) = self.traffic.payload() {
             // Lifetimes here are a bit subtle.
             // `payload` has self.traffic lifetime, used until `handle_payload`
@@ -78,7 +82,7 @@ impl<'a> Runner<'a> {
             // by the send_packet().
             // After that progress() can perform more send_packet() itself.
 
-            let d = self.conn.handle_payload(payload, &mut self.keys, b).await?;
+            let d = self.conn.handle_payload(payload, &mut self.keys, &mut self.behaviour).await?;
             self.traffic.handled_payload()?;
 
             if !d.resp.is_empty() || d.event.is_none() {
@@ -118,7 +122,7 @@ impl<'a> Runner<'a> {
             }
         } else {
             trace!("no em, conn progress");
-            self.conn.progress(&mut self.traffic, &mut self.keys, b).await?;
+            self.conn.progress(&mut self.traffic, &mut self.keys, &mut self.behaviour).await?;
             self.wake();
             None
         };
-- 
GitLab