@@ -6,7 +6,7 @@ use std::error::Error;
66use std:: fmt;
77use std:: pin:: Pin ;
88use std:: sync:: Arc ;
9- use tokio:: io:: { AsyncReadExt , AsyncWriteExt , BufReader } ;
9+ use tokio:: io:: { AsyncRead , AsyncReadExt , AsyncWrite , AsyncWriteExt , BufReader } ;
1010use tokio:: net:: TcpStream ;
1111use tokio:: sync:: { mpsc, Mutex } ;
1212use tokio:: time:: { timeout, Duration } ;
@@ -60,12 +60,14 @@ impl Default for ReceiveOptions {
6060 }
6161}
6262
63- pub struct ConnectedClient {
63+ pub struct ConnectedClient < T >
64+ where
65+ T : AsyncRead + AsyncWrite + Unpin + Send + ' static ,
66+ {
6467 pub connection_params : ClientConfig ,
65- stream : Connected ,
68+ stream : Arc < Mutex < T > > ,
6669}
6770
68- type Connected = Arc < Mutex < SslStream < TcpStream > > > ;
6971impl ClientConfig {
7072 pub fn new ( host : & str , port : u16 , path : & str , cert : & str , key : & str ) -> Self {
7173 ClientConfig {
@@ -77,7 +79,7 @@ impl ClientConfig {
7779 }
7880 }
7981
80- pub async fn connect ( self ) -> Result < ConnectedClient , Box < dyn Error > > {
82+ pub async fn connect ( self ) -> Result < ConnectedClient < SslStream < TcpStream > > , Box < dyn Error > > {
8183 debug ! ( "Attempting to connect to {}:{}..." , self . host, self . port) ;
8284
8385 let connect_timeout = Duration :: from_secs ( 10 ) ;
@@ -115,10 +117,7 @@ impl ClientConfig {
115117
116118 debug ! ( "Connection fully established" ) ;
117119
118- Ok ( ConnectedClient {
119- connection_params : self ,
120- stream : Arc :: new ( Mutex :: new ( stream) ) ,
121- } )
120+ Ok ( ConnectedClient :: new ( self , stream) )
122121 }
123122
124123 async fn do_http_connect ( & self , stream : & TcpStream ) -> Result < ( ) , Box < dyn Error > > {
@@ -166,7 +165,17 @@ impl ClientConfig {
166165 }
167166}
168167
169- impl ConnectedClient {
168+ impl < T > ConnectedClient < T >
169+ where
170+ T : AsyncRead + AsyncWrite + Unpin + Send + ' static ,
171+ {
172+ pub fn new ( connection_params : ClientConfig , stream : T ) -> Self {
173+ ConnectedClient {
174+ connection_params,
175+ stream : Arc :: new ( Mutex :: new ( stream) ) ,
176+ }
177+ }
178+
170179 pub async fn send_message ( & self , message : & str ) -> Result < ( ) , Box < dyn Error > > {
171180 let stream = self . stream . lock ( ) . await ;
172181 let mut pinned = Pin :: new ( stream) ;
0 commit comments