Skip to content

Commit

Permalink
Merge pull request #115 from bwdeng/fix-mutiple-responses-parsing
Browse files Browse the repository at this point in the history
Fix a bug when parsing multiple responses
  • Loading branch information
tulios authored Aug 18, 2018
2 parents a1e6c4b + 74d452f commit ade12dd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 33 deletions.
60 changes: 30 additions & 30 deletions src/network/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,43 +304,43 @@ module.exports = class Connection {

this.buffer = Buffer.concat([this.buffer, rawData])

// Not enough bytes to read the expected response size, keep buffering
if (Buffer.byteLength(this.buffer) <= Decoder.int32Size()) {
return
}

const data = Buffer.from(this.buffer)
const decoder = new Decoder(data)
const expectedResponseSize = decoder.readInt32()
// Process data if there are enough bytes to read the expected response size,
// otherwise keep buffering
while (Buffer.byteLength(this.buffer) > Decoder.int32Size()) {
const data = Buffer.from(this.buffer)
const decoder = new Decoder(data)
const expectedResponseSize = decoder.readInt32()

if (!decoder.canReadBytes(expectedResponseSize)) {
return
}

if (!decoder.canReadBytes(expectedResponseSize)) {
return
}
const response = new Decoder(decoder.readBytes(expectedResponseSize))
// Reset the buffer as the rest of the bytes
this.buffer = decoder.readAll()

// The full payload is loaded, erase the temporary buffer
this.buffer = Buffer.alloc(0)
if (this.authHandlers) {
return this.authHandlers.onSuccess(data.slice(0, Decoder.int32Size() + expectedResponseSize))
}

if (this.authHandlers) {
return this.authHandlers.onSuccess(data)
}
const correlationId = response.readInt32()
const payload = response.readAll()

const correlationId = decoder.readInt32()
const payload = decoder.readAll()
const entry = this.pendingQueue[correlationId]
delete this.pendingQueue[correlationId]

const entry = this.pendingQueue[correlationId]
delete this.pendingQueue[correlationId]
if (!entry) {
this.logDebug(`Response without match`, { correlationId })
return
}

if (!entry) {
this.logDebug(`Response without match`, { correlationId })
return
entry.resolve({
size: expectedResponseSize,
correlationId,
entry,
payload,
})
}

entry.resolve({
size: expectedResponseSize,
correlationId,
entry,
payload,
})
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/protocol/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ module.exports = class Decoder {
return Buffer.byteLength(this.buffer) - this.offset >= length
}

readBytes() {
const byteLength = this.readInt32()

readBytes(byteLength = this.readInt32()) {
if (byteLength === -1) {
return null
}
Expand Down

0 comments on commit ade12dd

Please sign in to comment.