Refactor buffer usage

This commit is contained in:
世界 2022-06-09 17:08:24 +08:00
parent 6084599638
commit 699292971c
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 62 additions and 38 deletions

View file

@ -250,6 +250,7 @@ func (c *clientPacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksad
return err
}
writeCipher.Seal(buffer.Index(c.keySaltLength), rw.ZeroBytes[:writeCipher.NonceSize()], buffer.From(c.keySaltLength), nil)
buffer.Extend(Overhead)
return common.Error(c.Write(buffer.Bytes()))
}
@ -290,15 +291,17 @@ func (c *clientPacketConn) ReadFrom(p []byte) (n int, addr net.Addr, err error)
return
}
addr = destination.UDPAddr()
copy(p, buffer.Bytes())
n = copy(p, buffer.Bytes())
return
}
func (c *clientPacketConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
destination := M.SocksaddrFromNet(addr)
_buffer := buf.StackNewSize(c.keySaltLength + M.SocksaddrSerializer.AddrPortLen(destination) + len(p))
_buffer := buf.StackNewSize(c.keySaltLength + M.SocksaddrSerializer.AddrPortLen(destination) + len(p) + Overhead)
defer common.KeepAlive(_buffer)
buffer := common.Dup(_buffer)
buffer.Resize(c.keySaltLength+M.SocksaddrSerializer.AddrPortLen(destination), 0)
common.Must1(buffer.Write(p))
err = c.WritePacket(buffer, destination)
if err != nil {
return