@@ -33,7 +33,10 @@ use oxcr_protocol::{
3333 uuid:: Uuid ,
3434 AsyncSet , PlayerN , PlayerNet , ProtocolPlugin ,
3535} ;
36- use std:: { net:: SocketAddr , sync:: Arc } ;
36+ use std:: {
37+ net:: SocketAddr ,
38+ sync:: { atomic:: Ordering , Arc } ,
39+ } ;
3740use tokio:: net:: TcpListener ;
3841use tokio_util:: sync:: CancellationToken ;
3942use tracing:: instrument;
@@ -84,15 +87,17 @@ async fn login(net: Arc<PlayerNet>, cx: Arc<TaskContext>, ent_id: Entity) -> Res
8487
8588 net. send_packet ( SetCompression {
8689 threshold : VarInt :: < i32 > ( threshold. try_into ( ) . unwrap ( ) ) ,
87- } ) ?;
90+ } )
91+ . await ?;
8892
89- net. compressing . set ( true ) . await ;
93+ net. compressing . store ( true , Ordering :: SeqCst ) ;
9094 }
9195
9296 net. send_packet ( LoginSuccess {
9397 uuid,
9498 username : name. clone ( ) ,
95- } ) ?;
99+ } )
100+ . await ?;
96101
97102 net. state . set ( State :: Play ) . await ;
98103
@@ -168,28 +173,32 @@ async fn login(net: Arc<PlayerNet>, cx: Arc<TaskContext>, ent_id: Entity) -> Res
168173 portal_cooldown : VarInt ( 20 ) ,
169174 } ;
170175
171- net. send_packet ( login_play) ?;
176+ net. send_packet ( login_play) . await ?;
172177
173178 let difficulty = cx
174179 . run_on_main_thread ( move |w| * w. world . resource :: < DifficultySetting > ( ) )
175180 . await ;
176181
177182 net. send_packet ( FeatureFlags {
178183 feature_flags : Array :: new ( & [ FeatureFlags :: FEATURE_VANILLA ] ) ,
179- } ) ?;
184+ } )
185+ . await ?;
180186
181- net. plugin_message ( Identifier :: MINECRAFT_BRAND , "implodent" ) ?;
187+ net. plugin_message ( Identifier :: MINECRAFT_BRAND , "implodent" )
188+ . await ?;
182189
183190 net. send_packet ( ChangeDifficulty {
184191 difficulty : difficulty. difficulty ,
185192 difficulty_locked : difficulty. is_locked ,
186- } ) ?;
193+ } )
194+ . await ?;
187195
188196 net. send_packet ( PlayerAbilities {
189197 flags : Abilities :: FLYING ,
190198 flying_speed : 0.05f32 ,
191199 fov_modifier : 0.1f32 ,
192- } ) ?;
200+ } )
201+ . await ?;
193202
194203 net. send_packet ( SetDefaultSpawnPosition {
195204 location : Position {
@@ -198,7 +207,8 @@ async fn login(net: Arc<PlayerNet>, cx: Arc<TaskContext>, ent_id: Entity) -> Res
198207 y : 50i8 . into ( ) ,
199208 } ,
200209 angle : 0f32 ,
201- } ) ?;
210+ } )
211+ . await ?;
202212
203213 Ok ( ( ) )
204214}
@@ -242,10 +252,11 @@ async fn status(net: Arc<PlayerNet>) -> Result<()> {
242252 } ,
243253 ..Default :: default ( )
244254 } ) ,
245- } ) ?;
255+ } )
256+ . await ?;
246257
247258 let PingRequest { payload } = net. recv_packet ( ) . await ?;
248- net. send_packet ( PongResponse { payload } ) ?;
259+ net. send_packet ( PongResponse { payload } ) . await ?;
249260
250261 Ok ( ( ) )
251262}
@@ -324,30 +335,36 @@ fn on_login(rt: Res<TokioTasksRuntime>, mut ev: EventReader<PlayerLoginEvent>, q
324335 let player = q. get ( event. entity ) . unwrap ( ) . 0 . clone ( ) ;
325336 rt. spawn_background_task ( move |task| async move {
326337 let cx = Arc :: new ( task) ;
327- match when_the_miette ( lifecycle ( player. clone ( ) , cx. clone ( ) , event. entity ) . await ) {
328- Ok ( ( ) ) => Ok :: < ( ) , Error > ( ( ) ) ,
329- Err ( e) => {
330- error ! ( error=?e, ?player, "Disconnecting" ) ;
331-
332- // ignore the result because we term the connection afterwards
333- let _ = match * ( player. state . read ( ) . await ) {
334- State :: Login => player. send_packet ( DisconnectLogin {
335- reason : Json ( ChatComponent :: String ( ChatStringComponent {
336- text : format ! ( "{e}" ) ,
337- ..Default :: default ( )
338- } ) ) ,
339- } ) ,
340- State :: Play => player. send_packet ( DisconnectPlay {
341- reason : Json ( ChatComponent :: String ( ChatStringComponent {
342- text : format ! ( "{e}" ) ,
343- ..Default :: default ( )
344- } ) ) ,
345- } ) ,
346- _ => Ok ( ( ) ) ,
347- } ;
348-
349- player. cancellator . cancel ( ) ;
350-
338+ tokio:: select! {
339+ lfc = lifecycle( player. clone( ) , cx. clone( ) , event. entity) => match when_the_miette( lfc) {
340+ Ok ( ( ) ) => Ok :: <( ) , Error >( ( ) ) ,
341+ Err ( e) => {
342+ error!( error=?e, ?player, "Disconnecting" ) ;
343+
344+ // ignore the result because we term the connection afterwards
345+ let _ = match * ( player. state. read( ) . await ) {
346+ State :: Login => player. send_packet( DisconnectLogin {
347+ reason: Json ( ChatComponent :: String ( ChatStringComponent {
348+ text: format!( "{e}" ) ,
349+ ..Default :: default ( )
350+ } ) ) ,
351+ } ) . await ,
352+ State :: Play => player. send_packet( DisconnectPlay {
353+ reason: Json ( ChatComponent :: String ( ChatStringComponent {
354+ text: format!( "{e}" ) ,
355+ ..Default :: default ( )
356+ } ) ) ,
357+ } ) . await ,
358+ _ => Ok ( ( ) ) ,
359+ } ;
360+
361+ player. cancellator. cancel( ) ;
362+
363+ Ok ( ( ) )
364+ }
365+ } ,
366+ _ = player. cancellator. cancelled( ) => {
367+ info!( "connection ended" ) ;
351368 Ok ( ( ) )
352369 }
353370 }
0 commit comments