Skip to content

Commit 4df5f55

Browse files
committed
Merge branch 'main' into HEAD
2 parents 2415cf5 + 987fd20 commit 4df5f55

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

rust/src/client_api/browser.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ pub struct WebApi {
1313
error_handler: Box<dyn FnMut(Error) + 'static>,
1414
}
1515

16+
impl Drop for WebApi {
17+
fn drop(&mut self) {
18+
// Close with normal closure code when dropped
19+
let _ = self.conn.close_with_code(1000);
20+
}
21+
}
22+
1623
impl WebApi {
1724
pub fn start<ErrFn>(
1825
conn: Connection,

rust/src/client_api/client_events.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,15 @@ impl ContractError {
247247
pub enum ClientRequest<'a> {
248248
DelegateOp(#[serde(borrow)] DelegateRequest<'a>),
249249
ContractOp(#[serde(borrow)] ContractRequest<'a>),
250-
Disconnect { cause: Option<Cow<'static, str>> },
251-
Authenticate { token: String },
250+
Disconnect {
251+
cause: Option<Cow<'static, str>>,
252+
},
253+
Authenticate {
254+
token: String,
255+
},
252256
NodeQueries(ConnectedPeers),
257+
/// Gracefully disconnect from the host.
258+
Close,
253259
}
254260

255261
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -301,6 +307,7 @@ impl ClientRequest<'_> {
301307
ClientRequest::Disconnect { cause } => ClientRequest::Disconnect { cause },
302308
ClientRequest::Authenticate { token } => ClientRequest::Authenticate { token },
303309
ClientRequest::NodeQueries(query) => ClientRequest::NodeQueries(query),
310+
ClientRequest::Close => ClientRequest::Close,
304311
}
305312
}
306313

@@ -616,6 +623,7 @@ impl Display for ClientRequest<'_> {
616623
ClientRequest::Disconnect { .. } => write!(f, "client disconnected"),
617624
ClientRequest::Authenticate { .. } => write!(f, "authenticate"),
618625
ClientRequest::NodeQueries(query) => write!(f, "node queries: {:?}", query),
626+
ClientRequest::Close => write!(f, "close"),
619627
}
620628
}
621629
}

rust/src/client_api/regular.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ use tokio::{
99
net::TcpStream,
1010
sync::mpsc::{self, Receiver, Sender},
1111
};
12-
use tokio_tungstenite::{tungstenite::Message, MaybeTlsStream, WebSocketStream};
12+
use tokio_tungstenite::{
13+
tungstenite::{
14+
protocol::{frame::coding::CloseCode, CloseFrame},
15+
Message,
16+
},
17+
MaybeTlsStream, WebSocketStream,
18+
};
1319

1420
type Connection = WebSocketStream<MaybeTlsStream<TcpStream>>;
1521

@@ -19,6 +25,15 @@ pub struct WebApi {
1925
queue: Vec<ClientRequest<'static>>,
2026
}
2127

28+
impl Drop for WebApi {
29+
fn drop(&mut self) {
30+
let req = self.request_tx.clone();
31+
tokio::spawn(async move {
32+
let _ = req.send(ClientRequest::Close).await;
33+
});
34+
}
35+
}
36+
2237
impl Stream for WebApi {
2338
type Item = HostResult;
2439

@@ -114,6 +129,7 @@ impl WebApi {
114129
res.ok_or_else(|| ClientError::from(ErrorKind::ChannelClosed))?
115130
}
116131

132+
#[doc(hidden)]
117133
pub async fn disconnect(self, cause: impl Into<Cow<'static, str>>) {
118134
let _ = self
119135
.request_tx
@@ -148,6 +164,7 @@ async fn request_handler(
148164
tracing::debug!(?error, "request handler error");
149165
let error = match error {
150166
Error::ChannelClosed => ErrorKind::ChannelClosed.into(),
167+
Error::ConnectionClosed => ErrorKind::Disconnect.into(),
151168
other => ClientError::from(format!("{other}")),
152169
};
153170
let _ = response_tx.send(Err(error)).await;
@@ -163,6 +180,17 @@ async fn process_request(
163180
.map_err(Into::into)
164181
.map_err(Error::OtherError)?;
165182
conn.send(Message::Binary(msg.into())).await?;
183+
if let ClientRequest::Disconnect { cause } = req {
184+
conn.close(cause.map(|c| CloseFrame {
185+
code: CloseCode::Normal,
186+
reason: format!("{c}").into(),
187+
}))
188+
.await?;
189+
return Err(Error::ConnectionClosed);
190+
} else if let ClientRequest::Close = req {
191+
conn.close(None).await?;
192+
return Err(Error::ConnectionClosed);
193+
}
166194
Ok(())
167195
}
168196

0 commit comments

Comments
 (0)