Skip to content

Commit

Permalink
improve packets.go
Browse files Browse the repository at this point in the history
  • Loading branch information
methane committed May 18, 2024
1 parent 0c65ab7 commit 9c487b7
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func (mc *mysqlConn) writePacket(data []byte) error {
// Write packet
if mc.writeTimeout > 0 {
if err := mc.netConn.SetWriteDeadline(time.Now().Add(mc.writeTimeout)); err != nil {
mc.log(err)
mc.cleanup()
return err
}
}
Expand Down Expand Up @@ -296,7 +298,7 @@ func (mc *mysqlConn) writeHandshakeResponsePacket(authResp []byte, plugin string
data, err := mc.buf.takeBuffer(pktLen + 4)
if err != nil {
// cannot take the buffer. Something must be wrong with the connection
mc.cleanup() // Avoid repeated "busy buffer" errors.
mc.cleanup()
return err
}

Expand Down Expand Up @@ -515,32 +517,33 @@ func (mc *okHandler) readResultOK() error {
}

// Result Set Header Packet
// http://dev.mysql.com/doc/internals/en/com-query-response.html#packet-ProtocolText::Resultset
// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response.html
func (mc *okHandler) readResultSetHeaderPacket() (int, error) {
// handleOkPacket replaces both values; other cases leave the values unchanged.
mc.result.affectedRows = append(mc.result.affectedRows, 0)
mc.result.insertIds = append(mc.result.insertIds, 0)

data, err := mc.conn().readPacket()
if err == nil {
switch data[0] {

case iOK:
return 0, mc.handleOkPacket(data)
if err != nil {
return 0, err
}

case iERR:
return 0, mc.conn().handleErrorPacket(data)
switch data[0] {
case iOK:
return 0, mc.handleOkPacket(data)

case iLocalInFile:
return 0, mc.handleInFileRequest(string(data[1:]))
}
case iERR:
return 0, mc.conn().handleErrorPacket(data)

// column count
num, _, _ := readLengthEncodedInteger(data)
// ignore remaining data in the packet. see #1478.
return int(num), nil
case iLocalInFile:
return 0, mc.handleInFileRequest(string(data[1:]))
}
return 0, err

// column count
// https://dev.mysql.com/doc/dev/mysql-server/latest/page_protocol_com_query_response_text_resultset.html
num, _, _ := readLengthEncodedInteger(data)
// ignore remaining data in the packet. see #1478.
return int(num), nil
}

// Error Packet
Expand Down

0 comments on commit 9c487b7

Please sign in to comment.