@@ -10,13 +10,13 @@ use crate::network::protocol::login::serverbound::LoginStart;
1010use crate :: network:: protocol:: play:: serverbound:: Play ;
1111use crate :: network:: protocol:: status:: clientbound:: { StatusPong , StatusResponse } ;
1212use crate :: network:: protocol:: status:: serverbound:: StatusPing ;
13+ use crate :: network:: status:: StatusBytes ;
1314use crate :: player:: player:: { ClientId , GameProfile } ;
1415use crate :: GameProfileProperty ;
1516use anyhow:: bail;
1617use bytes:: { Buf , Bytes , BytesMut } ;
1718use fstr:: FString ;
1819use std:: collections:: HashMap ;
19- use std:: sync:: Arc ;
2020use tokio:: io:: { AsyncReadExt , AsyncWriteExt } ;
2121use tokio:: net:: TcpStream ;
2222use tokio:: sync:: mpsc:: { UnboundedReceiver , UnboundedSender } ;
@@ -40,8 +40,7 @@ pub async fn handle_client(
4040 mut rx : UnboundedReceiver < ClientHandlerMessage > ,
4141 main_tx : UnboundedSender < MainThreadMessage > ,
4242 network_tx : UnboundedSender < NetworkThreadMessage > ,
43- // probably needs to be made mutable?, so it can update the player count, etc
44- status : Arc < String >
43+ status : StatusBytes
4544) {
4645 let mut client = Client {
4746 id : client_id,
@@ -62,7 +61,7 @@ pub async fn handle_client(
6261 & mut client,
6362 & tx,
6463 & main_tx,
65- status. as_ref ( )
64+ & status
6665 ) . await {
6766 eprintln!( "client {client_id:?} errored: {err}" ) ;
6867 break ;
@@ -89,7 +88,9 @@ pub async fn handle_client(
8988 }
9089 }
9190
92- let _ = network_tx. send ( NetworkThreadMessage :: ConnectionClosed { client_id } ) ;
91+ if client. connection_state == ConnectionState :: Play {
92+ let _ = network_tx. send ( NetworkThreadMessage :: ConnectionClosed { client_id } ) ;
93+ }
9394 println ! ( "handle client for {client_id:?} closed." ) ;
9495}
9596
@@ -98,7 +99,7 @@ async fn read_packets(
9899 client : & mut Client ,
99100 client_tx : & UnboundedSender < ClientHandlerMessage > ,
100101 main_tx : & UnboundedSender < MainThreadMessage > ,
101- status : & String
102+ status : & StatusBytes
102103) -> anyhow:: Result < ( ) > {
103104 while let Some ( mut buffer) = try_read_packet_slice ( buffer) {
104105 match client. connection_state {
@@ -158,14 +159,14 @@ fn handle_handshake(buffer: &mut impl Buf, client: &mut Client) -> anyhow::Resul
158159fn handle_status (
159160 buffer : & mut impl Buf ,
160161 client_tx : & UnboundedSender < ClientHandlerMessage > ,
161- status : & String
162+ status : & StatusBytes
162163) -> anyhow:: Result < ( ) > {
163164 let packet_id = * VarInt :: read ( buffer) ?;
164165 let mut packet_buffer = PacketBuffer :: new ( ) ;
165166 match packet_id {
166167 0x00 => {
167168 packet_buffer. write_packet ( & StatusResponse {
168- status,
169+ status : status . get_str ( ) ,
169170 } ) ;
170171 }
171172 0x01 => {
@@ -215,6 +216,8 @@ fn handle_login(
215216 ) ,
216217 } ;
217218
219+ client. connection_state = ConnectionState :: Play ;
220+
218221 packet_buffer. write_packet ( & LoginSuccess {
219222 uuid : uuid. hyphenated ( ) . to_string ( ) ,
220223 name : game_profile. username . clone ( ) ,
@@ -225,7 +228,6 @@ fn handle_login(
225228 client_id : client. id ,
226229 profile : game_profile,
227230 } ) ?;
228- client. connection_state = ConnectionState :: Play ;
229231 }
230232 _ => bail ! ( "Unknown packet id during login" )
231233 }
0 commit comments