Skip to content

Commit 4d2f7da

Browse files
author
Leonidas Loucas
committed
chore: Abstract to try to add test seam
1 parent 7f0e6bc commit 4d2f7da

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

rust/lib/srpc/client/src/lib.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::error::Error;
66
use std::fmt;
77
use std::pin::Pin;
88
use std::sync::Arc;
9-
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader};
9+
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader};
1010
use tokio::net::TcpStream;
1111
use tokio::sync::{mpsc, Mutex};
1212
use 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>>>;
6971
impl 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);

rust/lib/srpc/client/src/python_bindings.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ use std::{
88
sync::Arc,
99
task::{Context, Poll},
1010
};
11+
use tokio::net::TcpStream;
1112
use tokio::sync::{mpsc, Mutex};
13+
use tokio_openssl::SslStream;
1214

1315
#[pyclass]
1416
pub struct SrpcClientConfig(ClientConfig);
1517

1618
#[pyclass]
17-
pub struct ConnectedSrpcClient(Arc<Mutex<ConnectedClient>>);
19+
pub struct ConnectedSrpcClient(Arc<Mutex<ConnectedClient<SslStream<TcpStream>>>>);
1820

1921
#[pymethods]
2022
impl SrpcClientConfig {

0 commit comments

Comments
 (0)