Fix: avoid extra copy on windows

This commit is contained in:
xjasonlyu
2021-02-14 20:25:04 +08:00
parent 361a8133c9
commit 0e1e04acc7
6 changed files with 40 additions and 31 deletions

View File

@@ -0,0 +1,29 @@
// +build darwin freebsd linux,!amd64,!arm64 openbsd
package tun
import (
"github.com/xjasonlyu/tun2socks/common/pool"
)
const offset = 4 /* 4 bytes TUN_PI */
func (t *TUN) Read(packet []byte) (n int, err error) {
buf := pool.Get(offset + len(packet))
defer pool.Put(buf)
if n, err = t.nt.Read(buf, offset); err != nil {
return
}
copy(packet, buf[offset:offset+n])
return
}
func (t *TUN) Write(packet []byte) (int, error) {
buf := pool.Get(offset + len(packet))
defer pool.Put(buf)
copy(buf[offset:], packet)
return t.nt.Write(buf[:offset+len(packet)], offset)
}