Skip to content

Commit c2a9433

Browse files
committed
Switched to cancellation tokens…
They were 2 pages further ahead in the tokio documentation. No idea why they use channels on the cancellation pattern when tokens are available...
1 parent fb87006 commit c2a9433

File tree

2 files changed

+14
-44
lines changed

2 files changed

+14
-44
lines changed

crates/icboard/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ relative-path = { workspace = true }
3838
regex = { workspace = true }
3939
rand = { workspace = true }
4040
tokio = { workspace = true }
41+
tokio-util = "0.7.13"
4142
http = "1.1.0"
4243

4344
# for file view kb/mb

crates/icboard/src/main.rs

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use ratatui::{Terminal, backend::Backend};
2525
use semver::Version;
2626
use system_statistics_screen::{SystemStatisticsScreen, SystemStatisticsScreenMessage};
2727
use tokio::sync::Mutex;
28+
use tokio_util::sync::CancellationToken;
2829
use tui::{Tui, print_exit_screen};
2930

3031
use crate::bbs::await_securewebsocket_connections;
@@ -123,7 +124,8 @@ async fn start_icy_board(arguments: &Cli, file: PathBuf) -> Res<()> {
123124
restore_terminal()?;
124125
return Ok(());
125126
}
126-
start_connections(&bbs, &board).await;
127+
let mut connection_token = CancellationToken::new();
128+
start_connections(&bbs, &board, connection_token.clone()).await;
127129
let mut app = CallWaitScreen::new(&board).await?;
128130
let mut terminal = init_terminal()?;
129131
loop {
@@ -139,7 +141,9 @@ async fn start_icy_board(arguments: &Cli, file: PathBuf) -> Res<()> {
139141
bbs = Arc::new(Mutex::new(BBS::new(icy_board.config.board.num_nodes as usize)));
140142
board = Arc::new(tokio::sync::Mutex::new(icy_board));
141143
app = CallWaitScreen::new(&board).await?;
142-
start_connections(&bbs, &board).await;
144+
connection_token.cancel();
145+
connection_token = CancellationToken::new();
146+
start_connections(&bbs, &board, connection_token.clone()).await;
143147
continue;
144148
}
145149
}
@@ -167,30 +171,17 @@ async fn start_icy_board(arguments: &Cli, file: PathBuf) -> Res<()> {
167171
}
168172
}
169173

170-
static mut TELENET_RECEIVER: Option<tokio::sync::oneshot::Receiver<bool>> = None;
171-
static mut SSH_RECEIVER: Option<tokio::sync::oneshot::Receiver<bool>> = None;
172-
static mut SWEBSOCKET_RECEIVER: Option<tokio::sync::oneshot::Receiver<bool>> = None;
173-
174-
async fn start_connections(bbs: &Arc<Mutex<BBS>>, board: &Arc<Mutex<IcyBoard>>) {
174+
async fn start_connections(bbs: &Arc<Mutex<BBS>>, board: &Arc<Mutex<IcyBoard>>, token: CancellationToken) {
175175
let telnet_connection: icy_board_engine::icy_board::login_server::Telnet = board.lock().await.config.login_server.telnet.clone();
176176
if telnet_connection.is_enabled {
177177
let bbs = bbs.clone();
178178
let board: Arc<Mutex<IcyBoard>> = board.clone();
179-
let (mut tx1, rx1) = tokio::sync::oneshot::channel::<bool>();
180-
unsafe {
181-
#[allow(static_mut_refs)]
182-
if let Some(mut rx) = TELENET_RECEIVER.take() {
183-
rx.close();
184-
}
185-
186-
TELENET_RECEIVER = Some(rx1);
187-
}
179+
let token = token.clone();
188180
tokio::spawn(async move {
189181
tokio::select! {
190182
_ = await_telnet_connections(telnet_connection, board, bbs) => {
191183
},
192-
_ = tx1.closed() => {
193-
log::info!("telnet connection closed");
184+
_ = token.cancelled() => {
194185
}
195186
}
196187
});
@@ -200,23 +191,12 @@ async fn start_connections(bbs: &Arc<Mutex<BBS>>, board: &Arc<Mutex<IcyBoard>>)
200191
if ssh_connection.is_enabled {
201192
let bbs: Arc<Mutex<BBS>> = bbs.clone();
202193
let board = board.clone();
203-
204-
let (mut tx1, rx1) = tokio::sync::oneshot::channel::<bool>();
205-
unsafe {
206-
#[allow(static_mut_refs)]
207-
if let Some(mut rx) = SSH_RECEIVER.take() {
208-
rx.close();
209-
}
210-
211-
SSH_RECEIVER = Some(rx1);
212-
}
213-
194+
let token = token.clone();
214195
tokio::spawn(async move {
215196
tokio::select! {
216197
_ = bbs::ssh::await_ssh_connections(ssh_connection, board, bbs) => {
217198
},
218-
_ = tx1.closed() => {
219-
log::info!("telnet connection closed");
199+
_ = token.cancelled() => {
220200
}
221201
}
222202
});
@@ -239,23 +219,12 @@ async fn start_connections(bbs: &Arc<Mutex<BBS>>, board: &Arc<Mutex<IcyBoard>>)
239219
if secure_websocket_connection.is_enabled {
240220
let bbs = bbs.clone();
241221
let board = board.clone();
242-
243-
let (mut tx1, rx1) = tokio::sync::oneshot::channel::<bool>();
244-
unsafe {
245-
#[allow(static_mut_refs)]
246-
if let Some(mut rx) = SWEBSOCKET_RECEIVER.take() {
247-
rx.close();
248-
}
249-
250-
SWEBSOCKET_RECEIVER = Some(rx1);
251-
}
252-
222+
let token = token.clone();
253223
tokio::spawn(async move {
254224
tokio::select! {
255225
_ = await_securewebsocket_connections(secure_websocket_connection, board, bbs) => {
256226
},
257-
_ = tx1.closed() => {
258-
log::info!("telnet connection closed");
227+
_ = token.cancelled() => {
259228
}
260229
}
261230
});

0 commit comments

Comments
 (0)