@@ -26,7 +26,7 @@ use thiserror::Error;
2626use tokio:: {
2727 net:: TcpStream ,
2828 sync:: { Mutex , OwnedSemaphorePermit , RwLock } ,
29- time:: { self , timeout} ,
29+ time:: { self , timeout, Instant } ,
3030} ;
3131use uuid:: Uuid ;
3232
@@ -38,10 +38,11 @@ use crate::{
3838 play:: {
3939 ConfirmTeleportS , GameEvent , GameEventC , Gamemode , KeepAliveC , LoginPlayC ,
4040 PlayerInfoUpdateC , PlayerStatus , SetBorderCenterC , SetBorderSizeC , SetCenterChunkC ,
41- SetTickingStateC , StepTicksC , SynchronisePositionC ,
41+ SetPlayerPositionAndRotationS , SetPlayerPositionS , SetTickingStateC , StepTicksC ,
42+ SynchronisePositionC ,
4243 } ,
4344 } ,
44- PacketState ,
45+ Frame , Packet , PacketState ,
4546 } ,
4647 CrawlState ,
4748} ;
@@ -62,6 +63,8 @@ pub struct Player {
6263
6364 uuid : RwLock < Option < Uuid > > ,
6465 tp_state : Mutex < TeleportState > ,
66+
67+ last_keepalive : RwLock < Instant > ,
6568}
6669
6770#[ derive( Debug ) ]
@@ -85,12 +88,14 @@ impl SharedPlayer {
8588 id,
8689 io : Mutex :: new ( NetIo :: new ( connection) ) ,
8790 _permit : permit,
88- uuid : RwLock :: new ( None ) ,
8991
9092 crawlstate,
9193 packet_state : RwLock :: new ( PacketState :: Handshaking ) ,
9294
9395 tp_state : Mutex :: new ( TeleportState :: Clear ) ,
96+ uuid : RwLock :: new ( None ) ,
97+
98+ last_keepalive : RwLock :: new ( Instant :: now ( ) ) ,
9499 } ) )
95100 }
96101
@@ -333,20 +338,27 @@ impl SharedPlayer {
333338 // thread but realistically who knows burhhhh
334339 state. player_send . send ( self . clone ( ) ) . await ?;
335340
336- loop {
337- tokio:: select! {
338- _ = self . keepalive( ) => {
339- // keepalive needs to be sent every ~15 sec after keepalive response
340- time:: sleep( Duration :: from_secs( 12 ) ) . await ;
341- }
342- _ = state. shutdown_token. cancelled( ) => {
343- return Ok ( ( ) ) ;
344- }
345- }
346- }
341+ state. shutdown_token . cancelled ( ) . await ;
342+ Ok ( ( ) )
343+ }
344+
345+ pub async fn handle_all_packets ( & self ) -> Result < ( ) > {
346+ let packets = self . rx_all ( ) . await ;
347+ self . handle_frames ( packets) . await
347348 }
348349
349- async fn keepalive ( & self ) -> Result < ( ) > {
350+ pub async fn keepalive ( & self ) -> Result < ( ) > {
351+ let last_keepalive = self . 0 . last_keepalive . read ( ) . await ;
352+ let now = Instant :: now ( ) ;
353+
354+ if now - * last_keepalive < Duration :: from_secs ( 10 ) {
355+ return Ok ( ( ) ) ;
356+ }
357+
358+ drop ( last_keepalive) ;
359+ let mut last_keepalive = self . 0 . last_keepalive . write ( ) . await ;
360+ * last_keepalive = now;
361+
350362 let id = {
351363 let mut rng = rand:: thread_rng ( ) ;
352364 rng. gen ( )
@@ -359,6 +371,17 @@ impl SharedPlayer {
359371 }
360372 }
361373
374+ async fn rx_all ( & self ) -> Vec < Frame > {
375+ let mut io = self . 0 . io . lock ( ) . await ;
376+
377+ let mut frames = Vec :: new ( ) ;
378+ while let Ok ( frame) = io. rx_raw ( ) . await {
379+ frames. push ( frame) ;
380+ }
381+
382+ frames
383+ }
384+
362385 async fn ping ( & self , id : i64 ) -> Result < ( ) > {
363386 let mut io = self . 0 . io . lock ( ) . await ;
364387 io. flush ( ) . await ?;
@@ -417,6 +440,37 @@ impl SharedPlayer {
417440 }
418441 Ok ( ( ) )
419442 }
443+
444+ async fn handle_frames ( & self , frames : Vec < Frame > ) -> Result < ( ) > {
445+ for frame in frames {
446+ match frame. id {
447+ SetPlayerPositionS :: ID => {
448+ let packet: SetPlayerPositionS = frame. decode ( ) ?;
449+ debug ! (
450+ "Player {} moved to {}, {}, {}" ,
451+ self . 0 . id, packet. x, packet. feet_y, packet. z
452+ ) ;
453+ }
454+
455+ SetPlayerPositionAndRotationS :: ID => {
456+ let packet: SetPlayerPositionAndRotationS = frame. decode ( ) ?;
457+ debug ! (
458+ "Player {} moved to {}, {}, {} rotated {} {}" ,
459+ self . 0 . id, packet. x, packet. feet_y, packet. z, packet. pitch, packet. yaw
460+ ) ;
461+ }
462+
463+ id => {
464+ debug ! (
465+ "Got packet with id {id} from player {}, ignoring" ,
466+ self . 0 . id
467+ ) ;
468+ }
469+ }
470+ }
471+
472+ Ok ( ( ) )
473+ }
420474}
421475
422476#[ derive( Debug , Error ) ]
0 commit comments