summaryrefslogtreecommitdiff
path: root/packet.go
diff options
context:
space:
mode:
authorYawning Angel <yawning@schwanenlied.me>2014-05-14 09:58:53 +0000
committerYawning Angel <yawning@schwanenlied.me>2014-05-14 09:58:53 +0000
commit48c6f06d04440996b05503add1e6bb3225d87590 (patch)
tree94576a56373e71618598554408bc460fcb199025 /packet.go
parent89d5338eed93e9324d91ff498962c7dbe5b93f56 (diff)
Change the framing Encoder/Decoder to take the destination slice.
In theory this is easier on the garbage collector. Probably could reuse more of the intermediary buffers by stashing them in the connection state, but that makes the code kind of messy. This should be an improvement.
Diffstat (limited to 'packet.go')
-rw-r--r--packet.go21
1 files changed, 11 insertions, 10 deletions
diff --git a/packet.go b/packet.go
index 339a86d..6f9eb03 100644
--- a/packet.go
+++ b/packet.go
@@ -100,18 +100,18 @@ func (c *Obfs4Conn) producePacket(w io.Writer, pktType uint8, data []byte, padLe
pktLen := packetOverhead + len(data) + int(padLen)
// Encode the packet in an AEAD frame.
- // TODO: Change Encode to write into frame directly
- var frame []byte
- _, frame, err = c.encoder.Encode(pkt[:pktLen])
+ var frame [framing.MaximumSegmentLength]byte
+ frameLen := 0
+ frameLen, err = c.encoder.Encode(frame[:], pkt[:pktLen])
if err != nil {
// All encoder errors are fatal.
return
}
var wrLen int
- wrLen, err = w.Write(frame)
+ wrLen, err = w.Write(frame[:frameLen])
if err != nil {
return
- } else if wrLen < len(frame) {
+ } else if wrLen < frameLen {
err = io.ErrShortWrite
return
}
@@ -132,22 +132,23 @@ func (c *Obfs4Conn) consumeFramedPackets(w io.Writer) (n int, err error) {
}
c.receiveBuffer.Write(buf[:rdLen])
+ var decoded [framing.MaximumFramePayloadLength]byte
for c.receiveBuffer.Len() > 0 {
// Decrypt an AEAD frame.
- // TODO: Change Decode to write into packet directly
- var pkt []byte
- _, pkt, err = c.decoder.Decode(&c.receiveBuffer)
+ decLen := 0
+ decLen, err = c.decoder.Decode(decoded[:], &c.receiveBuffer)
if err == framing.ErrAgain {
// The accumulated payload does not make up a full frame.
return
} else if err != nil {
break
- } else if len(pkt) < packetOverhead {
- err = InvalidPacketLengthError(len(pkt))
+ } else if decLen < packetOverhead {
+ err = InvalidPacketLengthError(decLen)
break
}
// Decode the packet.
+ pkt := decoded[0:decLen]
pktType := pkt[0]
payloadLen := binary.BigEndian.Uint16(pkt[1:])
if int(payloadLen) > len(pkt)-packetOverhead {