From 969e2f4fa940719e14924faac0ec358066d7fb46 Mon Sep 17 00:00:00 2001 From: scruzin Date: Sun, 20 Jan 2019 08:41:32 +1030 Subject: [PATCH] packet.resize() now only makes a new buf when necessary. --- rtmp/packet.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/rtmp/packet.go b/rtmp/packet.go index 0f3ae241..a92f7022 100644 --- a/rtmp/packet.go +++ b/rtmp/packet.go @@ -199,9 +199,7 @@ func (pkt *packet) readFrom(c *Conn) error { hSize += 4 } - if pkt.bodySize > 0 && pkt.body == nil { - pkt.resize(pkt.bodySize, (hbuf[0]&0xc0)>>6) - } + pkt.resize(pkt.bodySize, pkt.headerType) if pkt.bodySize > c.inChunkSize { c.log(WarnLevel, pkg+"reading large packet", "size", int(pkt.bodySize)) @@ -234,10 +232,12 @@ func (pkt *packet) readFrom(c *Conn) error { 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. 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:] if ht != headerSizeAuto { pkt.headerType = ht