@@ -658,82 +658,6 @@ public function get_buffer_size(): int {
658658 }
659659}
660660
661- class SingleUseMySQLSocketServer {
662- private $ server ;
663- private $ socket ;
664- private $ port ;
665-
666- public function __construct ( MySQLQueryHandler $ query_handler , $ options = array () ) {
667- $ this ->server = new MySQLGateway ( $ query_handler );
668- $ this ->port = $ options ['port ' ] ?? 3306 ;
669- }
670-
671- public function start () {
672- $ this ->socket = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP );
673- socket_bind ( $ this ->socket , '0.0.0.0 ' , $ this ->port );
674- socket_listen ( $ this ->socket );
675- echo "MySQL PHP Server listening on port {$ this ->port }... \n" ;
676-
677- // Accept a single client for simplicity
678- $ client = socket_accept ( $ this ->socket );
679- if ( ! $ client ) {
680- exit ( "Failed to accept connection \n" );
681- }
682- $ this ->handle_client ( $ client );
683- socket_close ( $ client );
684- socket_close ( $ this ->socket );
685- }
686-
687- private function handle_client ( $ client ) {
688- // Send initial handshake
689- $ handshake = $ this ->server ->get_initial_handshake ();
690- socket_write ( $ client , $ handshake );
691-
692- while ( true ) {
693- // Read available data (up to 4096 bytes at a time)
694- $ data = @socket_read ( $ client , 4096 ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
695- if ( false === $ data || '' === $ data ) {
696- break ; // connection closed
697- }
698-
699- try {
700- // Process the data
701- $ response = $ this ->server ->receive_bytes ( $ data );
702- if ( $ response ) {
703- socket_write ( $ client , $ response );
704- }
705-
706- // If there's still data in the buffer, process it immediately
707- while ( $ this ->server ->has_buffered_data () ) {
708- try {
709- // Try to process more complete packets from the buffer
710- $ response = $ this ->server ->receive_bytes ( '' );
711- if ( $ response ) {
712- socket_write ( $ client , $ response );
713- }
714- } catch ( IncompleteInputException $ e ) {
715- // Not enough data to complete another packet, wait for more
716- break ;
717- }
718- }
719- } catch ( IncompleteInputException $ e ) {
720- // Not enough data yet, continue reading
721- continue ;
722- }
723- }
724-
725- echo "Client disconnected, terminating the server. \n" ;
726- $ this ->server ->reset ();
727- }
728- }
729-
730- if ( ! function_exists ( 'post_message_to_js ' ) ) {
731- function post_message_to_js ( string $ message ) {
732- echo 'The "post_message_to_js" function is only available in WordPress Playground but you are running it in a standalone PHP environment. ' . PHP_EOL ;
733- echo 'The message was: ' . $ message . PHP_EOL ;
734- }
735- }
736-
737661class MySQLSocketServer {
738662 private $ query_handler ;
739663 private $ socket ;
@@ -846,120 +770,3 @@ public function start() {
846770 }
847771 }
848772}
849-
850-
851- class MySQLPlaygroundYieldServer {
852- private $ query_handler ;
853- private $ clients = array ();
854- private $ client_servers = array ();
855- private $ port ;
856-
857- public function __construct ( MySQLQueryHandler $ query_handler , $ options = array () ) {
858- $ this ->query_handler = $ query_handler ;
859- $ this ->port = $ options ['port ' ] ?? 3306 ;
860- }
861-
862- public function start () {
863- echo "MySQL PHP Server listening via message passing on port {$ this ->port }... \n" ;
864-
865- // Main event loop
866- while ( true ) {
867- // Wait for a message from JS
868- $ message = post_message_to_js (
869- json_encode (
870- array (
871- 'type ' => 'ready_for_event ' ,
872- )
873- )
874- );
875-
876- $ command = json_decode ( $ message , true );
877- var_dump ( 'decoded event ' , $ command );
878- if ( ! $ command || ! isset ( $ command ['type ' ] ) ) {
879- continue ;
880- }
881-
882- switch ( $ command ['type ' ] ) {
883- case 'new_connection ' :
884- $ this ->handle_new_connection ( $ command ['clientId ' ] );
885- break ;
886-
887- case 'data_received ' :
888- $ this ->handle_data_received ( $ command ['clientId ' ], $ command ['data ' ] );
889- break ;
890-
891- case 'client_disconnected ' :
892- $ this ->handle_client_disconnected ( $ command ['clientId ' ] );
893- break ;
894- }
895- }
896- }
897-
898- private function handle_new_connection ( $ client_id ) {
899- echo "New client connected (ID: $ client_id). \n" ;
900- $ this ->clients [] = $ client_id ;
901- $ this ->client_servers [ $ client_id ] = new MySQLGateway ( $ this ->query_handler );
902-
903- // Send initial handshake
904- $ handshake = $ this ->client_servers [ $ client_id ]->get_initial_handshake ();
905- $ this ->send_response ( $ client_id , $ handshake );
906- }
907-
908- private function handle_data_received ( $ client_id , $ encoded_data ) {
909- if ( ! isset ( $ this ->client_servers [ $ client_id ] ) ) {
910- throw new IncompleteInputException ( 'No client server found ' );
911- }
912-
913- $ data = base64_decode ( $ encoded_data );
914-
915- try {
916- // Process the data
917- $ response = $ this ->client_servers [ $ client_id ]->receive_bytes ( $ data );
918- if ( $ response ) {
919- $ this ->send_response ( $ client_id , $ response );
920- } else {
921- throw new IncompleteInputException ( 'No response from client ' );
922- }
923-
924- // Process any buffered data
925- while ( $ this ->client_servers [ $ client_id ]->has_buffered_data () ) {
926- try {
927- $ response = $ this ->client_servers [ $ client_id ]->receive_bytes ( '' );
928- if ( $ response ) {
929- $ this ->send_response ( $ client_id , $ response );
930- }
931- } catch ( IncompleteInputException $ e ) {
932- throw $ e ;
933- }
934- }
935- } catch ( IncompleteInputException $ e ) {
936- // Not enough data yet, wait for mo
937- throw $ e ;
938- }
939- }
940-
941- private function handle_client_disconnected ( $ client_id ) {
942- echo "Client disconnected (ID: $ client_id). \n" ;
943- if ( isset ( $ this ->client_servers [ $ client_id ] ) ) {
944- $ this ->client_servers [ $ client_id ]->reset ();
945- unset( $ this ->client_servers [ $ client_id ] );
946- }
947-
948- $ index = array_search ( $ client_id , $ this ->clients , true );
949- if ( false !== $ index ) {
950- unset( $ this ->clients [ $ index ] );
951- }
952- }
953-
954- private function send_response ( $ client_id , $ data ) {
955- var_dump ( 'sending response ' );
956- $ response = json_encode (
957- array (
958- 'type ' => 'response_from_php ' ,
959- 'clientId ' => $ client_id ,
960- 'data ' => base64_encode ( $ data ),
961- )
962- );
963- post_message_to_js ( $ response );
964- }
965- }
0 commit comments