@@ -23,49 +23,37 @@ pub struct Connection {
2323}
2424
2525impl Connection {
26- /// Create a new [`Connection`]
27- pub fn new (
26+ /// Create a new [`Connection`] and do the initial key exchange
27+ pub async fn connect (
2828 stream : TcpStream ,
2929 addr : SocketAddr ,
3030 host_key : Arc < Ed25519KeyPair > ,
31- ) -> anyhow:: Result < Self > {
32- stream. set_nodelay ( true ) ?;
31+ ) -> Result < Connection , ( ) > {
32+ if let Err ( error) = stream. set_nodelay ( true ) {
33+ warn ! ( addr = %addr, %error, "failed to set nodelay" ) ;
34+ return Err ( ( ) ) ;
35+ }
3336
3437 let ( stream_read, stream_write) = stream. into_split ( ) ;
3538
36- Ok ( Self {
39+ let mut connection = Self {
3740 stream_read : DecryptingReader :: new ( stream_read) ,
3841 stream_write : EncryptingWriter :: new ( stream_write) ,
3942 addr,
4043 host_key,
41- } )
42- }
44+ } ;
4345
44- /// Drive the connection forward
45- pub async fn run ( mut self ) {
4646 let mut exchange = digest:: Context :: new ( & digest:: SHA256 ) ;
4747 let state = VersionExchange :: default ( ) ;
48- let Ok ( state) = state. advance ( & mut exchange, & mut self ) . await else {
49- return ;
50- } ;
48+ let state = state. advance ( & mut exchange, & mut connection ) . await ? ;
49+ let state = state . advance ( & mut exchange , & mut connection ) . await ? ;
50+ state . advance ( exchange , & mut connection ) . await ? ;
5151
52- let Ok ( state) = state. advance ( & mut exchange, & mut self ) . await else {
53- return ;
54- } ;
55-
56- let Ok ( ( ) ) = state. advance ( exchange, & mut self ) . await else {
57- return ;
58- } ;
59-
60- todo ! ( ) ;
52+ Ok ( connection)
6153 }
6254
63- pub ( crate ) async fn connect (
64- stream : TcpStream ,
65- addr : SocketAddr ,
66- host_key : Arc < Ed25519KeyPair > ,
67- ) -> anyhow:: Result < Self > {
68- // complete connection till kex finished (incl sending the newkeys message)
55+ /// Drive the connection forward
56+ pub async fn run ( mut self ) {
6957 todo ! ( )
7058 }
7159
0 commit comments