packet.resize() now only makes a new buf when necessary.

This commit is contained in:
scruzin 2019-01-20 08:41:32 +10:30
parent ddd1e4ab17
commit 969e2f4fa9
1 changed files with 5 additions and 5 deletions

View File

@ -199,9 +199,7 @@ func (pkt *packet) readFrom(c *Conn) error {
hSize += 4 hSize += 4
} }
if pkt.bodySize > 0 && pkt.body == nil { pkt.resize(pkt.bodySize, pkt.headerType)
pkt.resize(pkt.bodySize, (hbuf[0]&0xc0)>>6)
}
if pkt.bodySize > c.inChunkSize { if pkt.bodySize > c.inChunkSize {
c.log(WarnLevel, pkg+"reading large packet", "size", int(pkt.bodySize)) c.log(WarnLevel, pkg+"reading large packet", "size", int(pkt.bodySize))
@ -234,10 +232,12 @@ func (pkt *packet) readFrom(c *Conn) error {
return nil return nil
} }
// resize adjusts the packet's storage to accommodate a body of the given size and header type. // resize adjusts the packet's storage (if necessary) to accommodate a body of the given size and header type.
// When headerSizeAuto is specified, the header type is computed based on packet type. // When headerSizeAuto is specified, the header type is computed based on packet type.
func (pkt *packet) resize(size uint32, ht uint8) { func (pkt *packet) resize(size uint32, ht uint8) {
pkt.buf = make([]byte, fullHeaderSize+size) if cap(pkt.buf) < fullHeaderSize+int(size) {
pkt.buf = make([]byte, fullHeaderSize+size)
}
pkt.body = pkt.buf[fullHeaderSize:] pkt.body = pkt.buf[fullHeaderSize:]
if ht != headerSizeAuto { if ht != headerSizeAuto {
pkt.headerType = ht pkt.headerType = ht