Skip to content

Commit e7f64ca

Browse files
committed
feat: disconnect gracefully
1 parent 76363c5 commit e7f64ca

5 files changed

Lines changed: 61 additions & 37 deletions

File tree

‎player/src/audio.rs‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -267,31 +267,29 @@ impl AudioThread {
267267
AudioCommand::AudioData(d) => match d {
268268
AudioData::Frame(frame) => {
269269
if frame.frame % 10 == 0 {
270-
self.tx
271-
.send(AudioStatus::Elapsed(self.p.get_seconds_elapsed()))
272-
.unwrap();
273-
self.tx
274-
.send(AudioStatus::Buffer(self.p.buffer_status()))
275-
.unwrap();
270+
let _ = self
271+
.tx
272+
.send(AudioStatus::Elapsed(self.p.get_seconds_elapsed()));
273+
let _ = self.tx.send(AudioStatus::Buffer(self.p.buffer_status()));
276274
}
277275

278276
self.p.receive(frame.data);
279277
if let Some(samples) = self.p.get_visualizer_buffer() {
280278
let bars = calculate_visualizer(&samples);
281-
self.tx.send(AudioStatus::Visualizer(bars)).unwrap();
279+
let _ = self.tx.send(AudioStatus::Visualizer(bars));
282280
}
283281
}
284282
AudioData::Start => {
285283
self.wants_play = true;
286284
self.p.fake_frames_received(0);
287-
self.tx.send(AudioStatus::Buffering(true)).unwrap();
285+
let _ = self.tx.send(AudioStatus::Buffering(true));
288286
}
289287
AudioData::Finish => {
290288
while !self.p.finish() {
291289
std::thread::sleep(std::time::Duration::from_millis(20));
292290
}
293291
self.p.pause();
294-
self.tx.send(AudioStatus::Finished).unwrap();
292+
let _ = self.tx.send(AudioStatus::Finished);
295293
}
296294
AudioData::Resume => {
297295
self.p.resume();
@@ -303,18 +301,20 @@ impl AudioThread {
303301
}
304302

305303
AudioCommand::Clear => {
306-
self.tx.send(AudioStatus::Elapsed(0)).unwrap();
304+
let _ = self.tx.send(AudioStatus::Elapsed(0));
307305
self.p.clear();
308306
}
309307
AudioCommand::Volume(val) => {
310308
self.p.volume(val);
311309
}
312-
AudioCommand::Shutdown => break,
310+
AudioCommand::Shutdown => {
311+
break;
312+
}
313313
}
314314
if self.p.is_ready() && self.wants_play {
315315
self.wants_play = false;
316316

317-
self.tx.send(AudioStatus::Buffering(false)).unwrap();
317+
let _ = self.tx.send(AudioStatus::Buffering(false));
318318

319319
if !self.p.is_started() {
320320
self.p.start()

‎player/src/gui.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub enum UIEvent {
4040
BtnNext,
4141
BtnPrev,
4242
BtnQueue,
43+
BtnConnect,
4344
VolumeSlider(f32),
4445
VolumeUp,
4546
VolumeDown,

‎player/src/gui/main_window.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl MainWindow {
178178
main_win.add(&users);
179179

180180
let mut btn_connect = Button::new(main_win.width() - 85 - 46, 26, 24, 24, "Cn");
181-
//btn_connect.emit(sender, crate::UIEvent::Test("wow".to_string()));
181+
btn_connect.emit(s.clone(), UIEvent::BtnConnect);
182182
main_win.add(&btn_connect);
183183

184184
let mut btn_queue = Button::new(main_win.width() - 85 - 46, 26 + 24 + 2, 24, 24, "Qu");

‎player/src/main.rs‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::{HashMap, VecDeque};
33
use std::error::Error;
44

55
use audio::{AudioStatusRx, AudioThreadHandle};
6-
use futures::StreamExt;
6+
use futures::{SinkExt, StreamExt};
77
use gui::UIThreadHandle;
88
use key::Key;
99
use protocol::network::FrameStream;
@@ -173,6 +173,7 @@ async fn main() -> std::io::Result<()> {
173173

174174
// pass stuff in for now
175175
let mut t = MainThread::setup(addr, my_id).await;
176+
t.connect().await; // temporary
176177
t.run().await;
177178

178179
println!("exiting");
@@ -193,13 +194,11 @@ impl MainThread {
193194
async fn setup(addr: &str, my_id: String) -> Self {
194195
let ui_thread = UIThread::spawn(my_id.to_owned());
195196

196-
let connection = Connection::create(addr, &my_id).await.unwrap();
197-
198197
MainThread {
199198
addr: addr.to_owned(),
200199
my_id: my_id,
201200
ui: ui_thread,
202-
connection: Some(connection),
201+
connection: None,
203202

204203
loading_count: 0,
205204
}
@@ -210,21 +209,11 @@ impl MainThread {
210209
self.update_ui_status();
211210

212211
let mut ui_interval = tokio::time::interval(tokio::time::Duration::from_millis(50));
213-
// let timeout = tokio::time::sleep(tokio::time::Duration::from_secs(5));
214-
// tokio::pin!(timeout);
215212

216213
loop {
217214
tokio::select! {
218-
// () = &mut timeout => {
219-
// let connection = Connection::create(&self.addr, &self.my_id).await.unwrap();
220-
// self.connection= Some(connection);
221-
// }
222-
223215
// something to send
224-
// Some(msg) = self.connection.get_message_rx().recv() => {
225216
Some(msg) = maybe_message_rx(&self.connection) => {
226-
// Some(Some(msg)) = OptionFuture::from(self.connection.as_mut().map(|c| c.message_rx.recv())) => {
227-
// Some(Some(msg)) = self.connection.message_rx.recv() => {
228217
let c = self.connection.as_mut().unwrap();
229218
c.stream.borrow_mut().send(&msg).await.unwrap();
230219
},
@@ -333,11 +322,19 @@ impl MainThread {
333322
}
334323

335324
None => {
336-
// socket disconnected
337-
// TODO: if connection is none then we didnt get disconnected anyway we shouldnt break
338-
// just unset connection or something maybe
339-
// println!("socket disconnected");
340-
// break;
325+
if let Some(c) = self.connection.as_mut() {
326+
// socket was disconnected (either forcefully or because we closed it)
327+
// shutdown other threads
328+
c.transmit.send(TransmitCommand::Shutdown).unwrap();
329+
c.audio.send(AudioCommand::Shutdown).unwrap();
330+
331+
// unset connection now
332+
self.connection = None;
333+
334+
self.update_ui_status();
335+
} else {
336+
// we dont have a connection right now so dont do anything
337+
}
341338
}
342339
}
343340

@@ -346,6 +343,15 @@ impl MainThread {
346343
// some ui event
347344
Some(event) = self.ui.ui_rx.recv() => {
348345
match event {
346+
UIEvent::BtnConnect => {
347+
// awaiting here will block the loop maybe???
348+
if self.connection.is_some() {
349+
self.disconnect().await;
350+
} else {
351+
self.connect().await;
352+
}
353+
}
354+
349355
UIEvent::Play(path) => {
350356
if let Some(conn) = self.connection.as_mut() {
351357
// temporary api lol
@@ -372,7 +378,6 @@ impl MainThread {
372378
self.loading_count -= 1;
373379
//self.update_ui_status();
374380
}
375-
376381
}
377382
UIEvent::Pause => {
378383

@@ -456,12 +461,15 @@ impl MainThread {
456461
}
457462
}
458463

459-
fn connect() {
460-
todo!();
464+
async fn connect(&mut self) {
465+
let connection = Connection::create(&self.addr, &self.my_id).await.unwrap();
466+
self.connection = Some(connection);
461467
}
462468

463-
fn disconnect() {
464-
todo!();
469+
async fn disconnect(&mut self) {
470+
if let Some(c) = &mut self.connection {
471+
c.stream.borrow_mut().get_inner().close().await.unwrap();
472+
}
465473
}
466474
}
467475

‎player/src/transmit.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ impl AudioReader {
333333
pub enum TransmitCommand {
334334
Start(String),
335335
Stop,
336+
Shutdown,
336337
}
337338

338339
//temp
@@ -341,6 +342,9 @@ type MessageTx = tokio::sync::mpsc::UnboundedSender<Message>;
341342
pub type TransmitTx = tokio::sync::mpsc::UnboundedSender<TransmitCommand>;
342343
pub type TransmitRx = tokio::sync::mpsc::UnboundedReceiver<TransmitCommand>;
343344

345+
type ShutdownTx = tokio::sync::mpsc::UnboundedSender<()>;
346+
type ShutdownRx = tokio::sync::mpsc::UnboundedReceiver<()>;
347+
344348
pub struct TransmitThreadHandle {
345349
tx: TransmitTx,
346350
// join_handle: JoinHandle<()>,
@@ -359,6 +363,8 @@ pub struct TransmitThread {
359363
rx: TransmitRx,
360364
message_tx: MessageTx,
361365
audio_tx: AudioTx,
366+
shutdown_tx: ShutdownTx,
367+
shutdown_rx: ShutdownRx,
362368

363369
audio_reader: Option<AudioReader>,
364370
interval: Interval,
@@ -396,10 +402,14 @@ impl TransmitThread {
396402
// LOL OK it's two frames idk why maybe because it's stereo interleaved??????
397403
let interval = tokio::time::interval(tokio::time::Duration::from_millis(100));
398404

405+
let (shutdown_tx, shutdown_rx) = tokio::sync::mpsc::unbounded_channel();
406+
399407
TransmitThread {
400408
rx,
401409
message_tx,
402410
audio_tx,
411+
shutdown_tx,
412+
shutdown_rx,
403413

404414
audio_reader: None,
405415
interval,
@@ -419,6 +429,8 @@ impl TransmitThread {
419429
Some(cmd) = self.rx.recv() => self.handle_command(cmd),
420430

421431
_ = self.interval.tick() => self.handle_tick(),
432+
433+
_ = self.shutdown_rx.recv() => break,
422434
}
423435
}
424436
}
@@ -445,6 +457,9 @@ impl TransmitThread {
445457
t.set_finished();
446458
}
447459
}
460+
TransmitCommand::Shutdown => {
461+
self.shutdown_tx.send(()).unwrap();
462+
}
448463
}
449464
}
450465

0 commit comments

Comments
 (0)