mirror of
https://github.com/xjasonlyu/tun2socks.git
synced 2025-10-16 13:50:57 +08:00
Fix: avoid extra copy on windows
This commit is contained in:
29
core/device/tun/io_unix.go
Normal file
29
core/device/tun/io_unix.go
Normal 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)
|
||||
}
|
11
core/device/tun/io_windows.go
Normal file
11
core/device/tun/io_windows.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package tun
|
||||
|
||||
const offset = 0
|
||||
|
||||
func (t *TUN) Read(packet []byte) (int, error) {
|
||||
return t.nt.Read(packet, offset)
|
||||
}
|
||||
|
||||
func (t *TUN) Write(packet []byte) (int, error) {
|
||||
return t.nt.Write(packet, offset)
|
||||
}
|
@@ -1,5 +0,0 @@
|
||||
// +build darwin freebsd linux openbsd
|
||||
|
||||
package tun
|
||||
|
||||
const offset = 4 /* 4 bytes TUN_PI */
|
@@ -1,3 +0,0 @@
|
||||
package tun
|
||||
|
||||
const offset = 0
|
@@ -108,5 +108,3 @@ func setMTU(name string, n uint32) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ = offset /* ignore static check */
|
||||
|
@@ -5,7 +5,6 @@ package tun
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/xjasonlyu/tun2socks/common/pool"
|
||||
"github.com/xjasonlyu/tun2socks/core/device"
|
||||
"github.com/xjasonlyu/tun2socks/core/device/rwbased"
|
||||
|
||||
@@ -48,26 +47,6 @@ func Open(opts ...Option) (device.Device, error) {
|
||||
return t, nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (t *TUN) Name() string {
|
||||
name, _ := t.nt.Name()
|
||||
return name
|
||||
|
Reference in New Issue
Block a user