-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Possible factorization for writePacket #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b203832
Merge writePacket and splitPacket into one function
cxmcc cc20ec8
No longer need to do header calculation for writePacket
cxmcc 276fa30
Add cxmcc to AUTHORS
cxmcc 382f349
Remove one more place that does header calculations
cxmcc f975c34
Remove an outdated comment about writePacket
cxmcc d78ba17
Minor code simplification for writePacket
cxmcc c29c515
Clean up const variable that is no longer necessary
cxmcc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,46 +77,36 @@ func (mc *mysqlConn) readPacket() ([]byte, error) { | |
// Write packet buffer 'data' | ||
// The packet header must be already included | ||
func (mc *mysqlConn) writePacket(data []byte) error { | ||
if len(data)-4 <= mc.maxWriteSize { // Can send data at once | ||
// Write packet | ||
n, err := mc.netConn.Write(data) | ||
if err == nil && n == len(data) { | ||
mc.sequence++ | ||
return nil | ||
} | ||
|
||
// Handle error | ||
if err == nil { // n != len(data) | ||
errLog.Print(errMalformPkt.Error()) | ||
} else { | ||
errLog.Print(err.Error()) | ||
} | ||
return driver.ErrBadConn | ||
} | ||
|
||
// Must split packet | ||
return mc.splitPacket(data) | ||
} | ||
|
||
func (mc *mysqlConn) splitPacket(data []byte) error { | ||
pktLen := len(data) - 4 | ||
|
||
if pktLen > mc.maxPacketAllowed { | ||
return errPktTooLarge | ||
} | ||
|
||
for pktLen >= maxPacketSize { | ||
data[0] = 0xff | ||
data[1] = 0xff | ||
data[2] = 0xff | ||
for { | ||
var size int | ||
if pktLen >= maxPacketSize { | ||
data[0] = 0xff | ||
data[1] = 0xff | ||
data[2] = 0xff | ||
size = maxPacketSize | ||
} else { | ||
data[0] = byte(pktLen) | ||
data[1] = byte(pktLen >> 8) | ||
data[2] = byte(pktLen >> 16) | ||
size = pktLen | ||
} | ||
data[3] = mc.sequence | ||
|
||
// Write packet | ||
n, err := mc.netConn.Write(data[:4+maxPacketSize]) | ||
if err == nil && n == 4+maxPacketSize { | ||
n, err := mc.netConn.Write(data[:4+size]) | ||
if err == nil && n == 4+size { | ||
mc.sequence++ | ||
data = data[maxPacketSize:] | ||
pktLen -= maxPacketSize | ||
if size != maxPacketSize { | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No big difference, but my personal preference would be to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, would be simpler. |
||
} | ||
pktLen -= size | ||
data = data[size:] | ||
continue | ||
} | ||
|
||
|
@@ -129,11 +119,7 @@ func (mc *mysqlConn) splitPacket(data []byte) error { | |
return driver.ErrBadConn | ||
} | ||
|
||
data[0] = byte(pktLen) | ||
data[1] = byte(pktLen >> 8) | ||
data[2] = byte(pktLen >> 16) | ||
data[3] = mc.sequence | ||
return mc.writePacket(data) | ||
return nil | ||
} | ||
|
||
/****************************************************************************** | ||
|
@@ -265,12 +251,6 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error { | |
// SSL Connection Request Packet | ||
// http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::SSLRequest | ||
if mc.cfg.tls != nil { | ||
// Packet header [24bit length + 1 byte sequence] | ||
data[0] = byte((4 + 4 + 1 + 23)) | ||
data[1] = byte((4 + 4 + 1 + 23) >> 8) | ||
data[2] = byte((4 + 4 + 1 + 23) >> 16) | ||
data[3] = mc.sequence | ||
|
||
// Send TLS / SSL request packet | ||
if err := mc.writePacket(data[:(4+4+1+23)+4]); err != nil { | ||
return err | ||
|
@@ -285,12 +265,6 @@ func (mc *mysqlConn) writeAuthPacket(cipher []byte) error { | |
mc.buf.rd = tlsConn | ||
} | ||
|
||
// Add the packet header [24bit length + 1 byte sequence] | ||
data[0] = byte(pktLen) | ||
data[1] = byte(pktLen >> 8) | ||
data[2] = byte(pktLen >> 16) | ||
data[3] = mc.sequence | ||
|
||
// Filler [23 bytes] (all 0x00) | ||
pos := 13 + 23 | ||
|
||
|
@@ -330,12 +304,6 @@ func (mc *mysqlConn) writeOldAuthPacket(cipher []byte) error { | |
return driver.ErrBadConn | ||
} | ||
|
||
// Add the packet header [24bit length + 1 byte sequence] | ||
data[0] = byte(pktLen) | ||
data[1] = byte(pktLen >> 8) | ||
data[2] = byte(pktLen >> 16) | ||
data[3] = mc.sequence | ||
|
||
// Add the scrambled password [null terminated string] | ||
copy(data[4:], scrambleBuff) | ||
|
||
|
@@ -357,12 +325,6 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error { | |
return driver.ErrBadConn | ||
} | ||
|
||
// Add the packet header [24bit length + 1 byte sequence] | ||
data[0] = 0x01 // 1 byte long | ||
data[1] = 0x00 | ||
data[2] = 0x00 | ||
data[3] = 0x00 // new command, sequence id is always 0 | ||
|
||
// Add command byte | ||
data[4] = command | ||
|
||
|
@@ -382,12 +344,6 @@ func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error { | |
return driver.ErrBadConn | ||
} | ||
|
||
// Add the packet header [24bit length + 1 byte sequence] | ||
data[0] = byte(pktLen) | ||
data[1] = byte(pktLen >> 8) | ||
data[2] = byte(pktLen >> 16) | ||
data[3] = 0x00 // new command, sequence id is always 0 | ||
|
||
// Add command byte | ||
data[4] = command | ||
|
||
|
@@ -409,12 +365,6 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error { | |
return driver.ErrBadConn | ||
} | ||
|
||
// Add the packet header [24bit length + 1 byte sequence] | ||
data[0] = 0x05 // 5 bytes long | ||
data[1] = 0x00 | ||
data[2] = 0x00 | ||
data[3] = 0x00 // new command, sequence id is always 0 | ||
|
||
// Add command byte | ||
data[4] = command | ||
|
||
|
@@ -748,12 +698,7 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error { | |
pktLen = dataOffset + argLen | ||
} | ||
|
||
// Add the packet header [24bit length + 1 byte sequence] | ||
data[0] = byte(pktLen) | ||
data[1] = byte(pktLen >> 8) | ||
data[2] = byte(pktLen >> 16) | ||
data[3] = 0x00 // mc.sequence | ||
|
||
stmt.mc.sequence = 0 | ||
// Add command byte [1 byte] | ||
data[4] = comStmtSendLongData | ||
|
||
|
@@ -803,26 +748,13 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error { | |
if len(args) == 0 { | ||
const pktLen = 1 + 4 + 1 + 4 | ||
data = mc.buf.takeBuffer(4 + pktLen) | ||
if data == nil { | ||
// can not take the buffer. Something must be wrong with the connection | ||
errLog.Print("Busy buffer") | ||
return driver.ErrBadConn | ||
} | ||
|
||
// packet header [4 bytes] | ||
data[0] = byte(pktLen) | ||
data[1] = byte(pktLen >> 8) | ||
data[2] = byte(pktLen >> 16) | ||
data[3] = 0x00 // new command, sequence id is always 0 | ||
} else { | ||
data = mc.buf.takeCompleteBuffer() | ||
if data == nil { | ||
// can not take the buffer. Something must be wrong with the connection | ||
errLog.Print("Busy buffer") | ||
return driver.ErrBadConn | ||
} | ||
|
||
// header (bytes 0-3) is added after we know the packet size | ||
} | ||
if data == nil { | ||
// can not take the buffer. Something must be wrong with the connection | ||
errLog.Print("Busy buffer") | ||
return driver.ErrBadConn | ||
} | ||
|
||
// command [1 byte] | ||
|
@@ -984,14 +916,6 @@ func (stmt *mysqlStmt) writeExecutePacket(args []driver.Value) error { | |
pos += len(paramValues) | ||
data = data[:pos] | ||
|
||
pktLen := pos - 4 | ||
|
||
// packet header [4 bytes] | ||
data[0] = byte(pktLen) | ||
data[1] = byte(pktLen >> 8) | ||
data[2] = byte(pktLen >> 16) | ||
data[3] = mc.sequence | ||
|
||
// Convert nullMask to bytes | ||
for i, max := 0, (stmt.paramCount+7)>>3; i < max; i++ { | ||
data[i+14] = byte(nullMask >> uint(i<<3)) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is not true anymore