@@ -2,6 +2,7 @@ use rand::Rng;
22use shared:: logger;
33use shared:: message:: { ChatMessage , MessageTypes } ;
44use shared:: network:: TcpMessageHandler ;
5+ use shared:: version:: { self , VERSION } ;
56use std:: collections:: { HashMap , HashSet } ;
67use std:: net:: { IpAddr , SocketAddr } ;
78use std:: sync:: Arc ;
@@ -67,6 +68,10 @@ impl<'a> MessageHandlers<'a> {
6768 }
6869
6970 match message. msg_type {
71+ MessageTypes :: VersionCheck => {
72+ self . process_version_check ( message. content_as_string ( ) , & mut tcp_handler)
73+ . await ?;
74+ }
7075 MessageTypes :: Join => {
7176 self . process_join ( message. content_as_string ( ) , & mut tcp_handler, chat_name)
7277 . await ?;
@@ -610,6 +615,46 @@ impl<'a> MessageHandlers<'a> {
610615
611616 Ok ( ( ) )
612617 }
618+
619+ async fn process_version_check < S : AsyncRead + AsyncWrite + Unpin > (
620+ & self ,
621+ client_version : Option < String > ,
622+ tcp_handler : & mut StreamWrapper < ' _ , S > ,
623+ ) -> Result < ( ) , UserConnectionError > {
624+ let client_version = client_version. ok_or ( UserConnectionError :: InvalidMessage ) ?;
625+
626+ if !version:: versions_compatible ( & client_version, VERSION ) {
627+ logger:: log_warning ( & format ! (
628+ "Version mismatch from {}: client v{} != server v{}" ,
629+ self . addr, client_version, VERSION
630+ ) ) ;
631+
632+ // Send version mismatch error with details
633+ let mismatch_content = format ! (
634+ "{}|{}|{}" ,
635+ client_version,
636+ VERSION ,
637+ version:: GITHUB_README_URL
638+ ) ;
639+ let mismatch_msg = ChatMessage :: try_new (
640+ MessageTypes :: VersionMismatch ,
641+ Some ( mismatch_content. into_bytes ( ) ) ,
642+ )
643+ . map_err ( |_| UserConnectionError :: InvalidMessage ) ?;
644+ tcp_handler
645+ . send_message_chunked ( mismatch_msg)
646+ . await
647+ . map_err ( UserConnectionError :: IoError ) ?;
648+
649+ return Err ( UserConnectionError :: VersionMismatch ) ;
650+ }
651+
652+ logger:: log_info ( & format ! (
653+ "Version check passed for {}: v{}" ,
654+ self . addr, client_version
655+ ) ) ;
656+ Ok ( ( ) )
657+ }
613658}
614659
615660#[ cfg( test) ]
0 commit comments