@@ -138,25 +138,26 @@ public function parse($data)
138138 $ this ->state = self ::STATE_STANDBY ;
139139 //$this->stream->bufferSize = 4;
140140 if ($ this ->phase === 0 ) {
141- $ this ->phase = self ::PHASE_GOT_INIT ;
142- $ this ->protocalVersion = $ this ->buffer ->readInt1 ();
143- $ this ->debug (sprintf ("Protocal Version: %d " , $ this ->protocalVersion ));
144- if ($ this ->protocalVersion === 0xFF ) { //error
145- $ fieldCount = $ this ->protocalVersion ;
146- $ this ->protocalVersion = 0 ;
147- printf ("Error: \n" );
148-
149- $ this ->rsState = self ::RS_STATE_HEADER ;
150- $ this ->resultFields = [];
151- if ($ this ->phase === self ::PHASE_AUTH_SENT || $ this ->phase === self ::PHASE_GOT_INIT ) {
152- $ this ->phase = self ::PHASE_AUTH_ERR ;
153- }
141+ $ response = $ this ->buffer ->readInt1 ();
142+ if ($ response === 0xFF ) {
143+ // error packet before handshake means we did not exchange capabilities and error does not include SQL state
144+ $ this ->phase = self ::PHASE_AUTH_ERR ;
145+ $ this ->errno = $ this ->buffer ->readInt2 ();
146+ $ this ->errmsg = $ this ->buffer ->read ($ this ->pctSize - $ len + $ this ->buffer ->length ());
147+ $ this ->debug (sprintf ("Error Packet:%d %s \n" , $ this ->errno , $ this ->errmsg ));
154148
155- goto field;
149+ // error during init phase also means we're not currently executing any command
150+ // simply reject the first outstanding command in the queue (AuthenticateCommand)
151+ $ this ->currCommand = $ this ->executor ->dequeue ();
152+ $ this ->onError ();
153+ return ;
156154 }
157155
158- $ options = &$ this ->connectOptions ;
156+ $ this ->phase = self ::PHASE_GOT_INIT ;
157+ $ this ->protocalVersion = $ response ;
158+ $ this ->debug (sprintf ("Protocal Version: %d " , $ this ->protocalVersion ));
159159
160+ $ options = &$ this ->connectOptions ;
160161 $ options ['serverVersion ' ] = $ this ->buffer ->readStringNull ();
161162 $ options ['threadId ' ] = $ this ->buffer ->readInt4 ();
162163 $ this ->scramble = $ this ->buffer ->read (8 ); // 1st part
@@ -173,14 +174,15 @@ public function parse($data)
173174 $ this ->buffer ->readStringNull (); // skip authentication plugin name
174175 }
175176
177+ // init completed, continue with sending AuthenticateCommand
176178 $ this ->nextRequest (true );
177179 } else {
178180 $ fieldCount = $ this ->buffer ->readInt1 ();
179- field:
181+
180182 if ($ fieldCount === 0xFF ) {
181183 // error packet
182184 $ this ->errno = $ this ->buffer ->readInt2 ();
183- $ this ->buffer ->skip (6 ); // state
185+ $ this ->buffer ->skip (6 ); // skip SQL state
184186 $ this ->errmsg = $ this ->buffer ->read ($ this ->pctSize - $ len + $ this ->buffer ->length ());
185187 $ this ->debug (sprintf ("Error Packet:%d %s \n" , $ this ->errno , $ this ->errmsg ));
186188
0 commit comments