@@ -155,15 +155,21 @@ async fn maybe_audio_status_rx(t: &Option<Connection>) -> Option<AudioStatus> {
155155 }
156156}
157157
158- async fn maybe_tcp_message ( t : & Option < Connection > ) -> Option < Result < bytes :: BytesMut , ( ) > > {
158+ async fn maybe_tcp_message ( t : & Option < Connection > ) -> Option < Result < Message , ( ) > > {
159159 match t {
160160 Some ( c) => {
161161 let mut stream = c. stream . borrow_mut ( ) ;
162162 let result = stream. get_inner ( ) . next ( ) . await ;
163163
164164 match result {
165165 Some ( r) => match r {
166- Ok ( b) => Some ( Ok ( b) ) ,
166+ Ok ( bytes) => match bincode:: deserialize :: < Message > ( & bytes) {
167+ Ok ( msg) => Some ( Ok ( msg) ) ,
168+ Err ( e) => {
169+ println ! ( "failed to deserialize message: {:?}" , e) ;
170+ Some ( Err ( ( ) ) )
171+ }
172+ } ,
167173 Err ( e) => {
168174 println ! ( "tcp stream error: {:?}" , e) ;
169175 Some ( Err ( ( ) ) )
@@ -174,6 +180,8 @@ async fn maybe_tcp_message(t: &Option<Connection>) -> Option<Result<bytes::Bytes
174180 None => Some ( Err ( ( ) ) ) ,
175181 }
176182 }
183+
184+ // nothing happened yet
177185 None => None ,
178186 }
179187}
@@ -258,89 +266,81 @@ impl MainThread {
258266 } ,
259267
260268 // tcp message
261- Some ( maybe_message) = maybe_tcp_message( & self . connection) => {
262- match maybe_message {
263- Ok ( bytes) => {
264- let c = self . connection. as_mut( ) . unwrap( ) ;
265-
266- let msg: Message =
267- bincode:: deserialize( & bytes) . expect( "failed to deserialize message" ) ;
268-
269- match msg {
270- Message :: Info (
271- info
272- ) => {
273- match info {
274- protocol:: Info :: Queue ( queue) => {
275- // TODO
276- println!( "got queue: {:?}" , queue) ;
277- self . ui. update( UIUpdateEvent :: UpdateQueue ( c. playing. clone( ) , queue. clone( ) ) ) ;
278- c. queue = queue;
279- } ,
280- protocol:: Info :: Playing ( playing) => {
281- // TODO
282- println!( "got playing: {:?}" , playing) ;
283-
284- c. playing = playing. clone( ) ;
285-
286- if let Some ( t) = playing {
287- if t. owner == self . my_id {
288- let path = c. key. decrypt_path( t. path) . unwrap( ) ;
289- c. transmit. send( TransmitCommand :: Start ( path) ) . unwrap( ) ;
290- }
269+ Some ( message_or_disconnect) = maybe_tcp_message( & self . connection) => {
270+ if let Ok ( message) = message_or_disconnect {
271+ let c = self . connection. as_mut( ) . unwrap( ) ;
272+
273+ match message {
274+ Message :: Info ( info) => {
275+ match info {
276+ protocol:: Info :: Queue ( queue) => {
277+ // TODO
278+ println!( "got queue: {:?}" , queue) ;
279+ self . ui. update( UIUpdateEvent :: UpdateQueue ( c. playing. clone( ) , queue. clone( ) ) ) ;
280+ c. queue = queue;
281+ } ,
282+ protocol:: Info :: Playing ( playing) => {
283+ // TODO
284+ println!( "got playing: {:?}" , playing) ;
285+
286+ c. playing = playing. clone( ) ;
287+
288+ if let Some ( t) = playing {
289+ if t. owner == self . my_id {
290+ let path = c. key. decrypt_path( t. path) . unwrap( ) ;
291+ c. transmit. send( TransmitCommand :: Start ( path) ) . unwrap( ) ;
291292 }
292- } ,
293- protocol:: Info :: ConnectedUsers ( list) => {
294- c. connected_users = list;
295- self . ui. update( UIUpdateEvent :: UpdateUserList ( c. connected_users. clone( ) ) ) ;
296- }
297- protocol:: Info :: Room ( opts) => {
298- self . ui. update( UIUpdateEvent :: UpdateRoomName ( opts. map( |o| o. name) ) ) ;
299293 }
300- _ => { }
294+ } ,
295+ protocol:: Info :: ConnectedUsers ( list) => {
296+ c. connected_users = list;
297+ self . ui. update( UIUpdateEvent :: UpdateUserList ( c. connected_users. clone( ) ) ) ;
301298 }
299+ protocol:: Info :: Room ( opts) => {
300+ self . ui. update( UIUpdateEvent :: UpdateRoomName ( opts. map( |o| o. name) ) ) ;
301+ }
302+ _ => { }
302303 }
303- Message :: AudioData ( data) => {
304- match data {
305- protocol:: AudioData :: Frame ( f) => {
306- if !c. is_synced {
307- // we're late.... try and catch up will you
308- c. is_synced = true ;
309- c. audio. send( AudioCommand :: StartLate ( f. frame as usize ) ) . unwrap( ) ;
310- }
311-
312- c. audio. send( AudioCommand :: AudioData ( AudioData :: Frame ( f) ) ) . unwrap( ) ;
313- }
314- protocol:: AudioData :: Start => {
304+ }
305+ Message :: AudioData ( data) => {
306+ match data {
307+ protocol:: AudioData :: Frame ( f) => {
308+ if !c. is_synced {
309+ // we're late.... try and catch up will you
315310 c. is_synced = true ;
316- c. audio. send( AudioCommand :: AudioData ( data ) ) . unwrap( ) ;
311+ c. audio. send( AudioCommand :: StartLate ( f . frame as usize ) ) . unwrap( ) ;
317312 }
318- _ => {
319- // forward to audio thread
320- c. audio. send( AudioCommand :: AudioData ( data) ) . unwrap( ) ;
321- }
322- }
323- } ,
324313
325- _ => {
326- println!( "received message: {:?}" , msg) ;
314+ c. audio. send( AudioCommand :: AudioData ( AudioData :: Frame ( f) ) ) . unwrap( ) ;
315+ }
316+ protocol:: AudioData :: Start => {
317+ c. is_synced = true ;
318+ c. audio. send( AudioCommand :: AudioData ( data) ) . unwrap( ) ;
319+ }
320+ _ => {
321+ // forward to audio thread
322+ c. audio. send( AudioCommand :: AudioData ( data) ) . unwrap( ) ;
323+ }
327324 }
325+ } ,
326+
327+ _ => {
328+ println!( "received message: {:?}" , message) ;
328329 }
329330 }
330- Err ( ( ) ) => {
331- if let Some ( c) = self . connection. as_mut( ) {
332- // socket was disconnected (either forcefully or because we closed it)
333- // shutdown other threads
334- c. transmit. send( TransmitCommand :: Shutdown ) . unwrap( ) ;
335- c. audio. send( AudioCommand :: Shutdown ) . unwrap( ) ;
331+ } else {
332+ if let Some ( c) = self . connection. as_mut( ) {
333+ // socket was disconnected (either forcefully or because we closed it)
334+ // shutdown other threads
335+ c. transmit. send( TransmitCommand :: Shutdown ) . unwrap( ) ;
336+ c. audio. send( AudioCommand :: Shutdown ) . unwrap( ) ;
336337
337- // unset connection now
338- self . connection = None ;
338+ // unset connection now
339+ self . connection = None ;
339340
340- self . update_ui_status( ) ;
341- } else {
342- // we dont have a connection right now so dont do anything
343- }
341+ self . update_ui_status( ) ;
342+ } else {
343+ // we dont have a connection right now so dont do anything
344344 }
345345 }
346346 }
0 commit comments