@@ -3,7 +3,7 @@ use std::collections::{HashMap, VecDeque};
33use std:: error:: Error ;
44
55use audio:: { AudioStatusRx , AudioThreadHandle } ;
6- use futures:: StreamExt ;
6+ use futures:: { SinkExt , StreamExt } ;
77use gui:: UIThreadHandle ;
88use key:: Key ;
99use 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
0 commit comments