@@ -11,6 +11,8 @@ use tokio::sync::{
1111 oneshot,
1212} ;
1313
14+ use crate :: client:: ConfigurationState ;
15+
1416use super :: { Game , GraphicsContexts } ;
1517
1618use letsplay_av_ffmpeg:: encoder_thread;
@@ -59,56 +61,70 @@ fn main(mut rx: mpsc::UnboundedReceiver<GameThreadMessage>, mut game: Box<dyn Ga
5961
6062 game. init ( & contexts, & video_encoder_control) ;
6163
62- // TEMP CODE: This accepts a single tcp connection and broadcasts packets to it
63- // this is temporary as all hell
64- // yes I know sync io but its running on another thread anyways so its fine.
65- let server = std:: net:: TcpListener :: bind ( "0.0.0.0:6040" ) . expect ( "rrr" ) ;
66- let mut clients = Vec :: new ( ) ;
67-
68- while clients. len ( ) != 1 {
69- let client = server. accept ( ) . expect ( "baned" ) ;
70- clients. push ( client. 0 ) ;
71- }
64+ #[ cfg( feature = "temp" ) ]
65+ {
66+ // TEMP CODE: This accepts a single tcp connection and broadcasts NALU packets to it
67+ // this is temporary as all hell
68+ // yes I know sync io but its running on another thread anyways so its fine.
69+ let server = std:: net:: TcpListener :: bind ( "0.0.0.0:6040" ) . expect ( "rrr" ) ;
70+ let mut clients = Vec :: new ( ) ;
71+
72+ while clients. len ( ) != 1 {
73+ let client = server. accept ( ) . expect ( "baned" ) ;
74+ clients. push ( client. 0 ) ;
75+ }
7276
73- tracing:: info!( "all clients accepted - unblocking and completing intialization" ) ;
77+ tracing:: info!( "all clients accepted - unblocking and completing intialization" ) ;
7478
75- // Helper thread
76- std:: thread:: spawn ( move || loop {
77- let frame = packet_waiter. wait_for_packet ( ) ;
78- for client in & mut clients {
79- let _ = client. write_all ( frame. data ( ) . unwrap ( ) ) ;
80- }
81- } ) ;
79+ // Helper thread
80+ std:: thread:: spawn ( move || loop {
81+ let frame = packet_waiter. wait_for_packet ( ) ;
82+ for client in & mut clients {
83+ let _ = client. write_all ( frame. data ( ) . unwrap ( ) ) ;
84+ }
85+ } ) ;
86+ }
8287
8388 loop {
8489 match rx. try_recv ( ) {
85- Ok ( message) => match message {
86- GameThreadMessage :: Shutdown => break ,
87- GameThreadMessage :: Suspend { suspend, tx } => {
88- if suspended != suspend {
89- suspended = suspend
90- }
90+ Ok ( message) => {
91+ match message {
92+ GameThreadMessage :: Shutdown => break ,
93+ GameThreadMessage :: Suspend { suspend, tx } => {
94+ if suspended != suspend {
95+ // TODO: This is temporary, since we probably should instead shutdown or something
96+ // since a unconfigured game indicates a JSON misconfiguration.
97+ if suspend == false
98+ && game. get_configuration_state ( )
99+ == ConfigurationState :: ConfigurationNeeded
100+ {
101+ tracing:: error!( "Attempting to unsuspend a game that hasn't been fully configured!" ) ;
102+ continue ;
103+ }
104+ suspended = suspend
105+ }
91106
92- let _ = tx. send ( ( ) ) ;
93- }
107+ let _ = tx. send ( ( ) ) ;
108+ }
94109
95- GameThreadMessage :: Reset { tx } => {
96- game. reset ( ) ;
97- let _ = tx. send ( ( ) ) ;
98- }
110+ GameThreadMessage :: Reset { tx } => {
111+ game. reset ( ) ;
112+ let _ = tx. send ( ( ) ) ;
113+ }
99114
100- GameThreadMessage :: SetProperty { key, value, tx } => {
101- // TODO: we should send the error result to the given tx
102- // so that we can propegate errors to the main thread
103- match game. set_property ( & key, & value) {
104- Ok ( _) => { }
105- Err ( err) => {
106- tracing:: error!( "Error setting property {key} to {value}: {}" , err) ;
107- }
108- } ;
109- let _ = tx. send ( ( ) ) ;
115+ GameThreadMessage :: SetProperty { key, value, tx } => {
116+ // TODO: we should send the error result to the given tx
117+ // so that we can propegate errors to the main thread
118+ match game. set_property ( & key, & value) {
119+ Ok ( _) => { }
120+ Err ( err) => {
121+ tracing:: error!( "Error setting property {key} to {value}: {}" , err) ;
122+ }
123+ } ;
124+ let _ = tx. send ( ( ) ) ;
125+ }
110126 }
111- } ,
127+ }
112128
113129 Err ( TryRecvError :: Empty ) => { }
114130 Err ( TryRecvError :: Disconnected ) => break ,
@@ -197,8 +213,8 @@ impl GameThread {
197213 let _ = rx. await ;
198214 }
199215
200- /// Shuts down the game thread.
201- pub async fn shutdown ( self ) {
216+ /// Shuts down and waits for the game thread to exit .
217+ pub fn shutdown ( self ) {
202218 let _ = self . tx . send ( GameThreadMessage :: Shutdown ) ;
203219 self . join_handle . join ( ) . expect ( "Failed to join game thread" ) ;
204220 }
0 commit comments