Optimize quic speed

This commit is contained in:
NNdroid 2022-12-12 06:46:26 +08:00
parent 234f82616a
commit c87dc42d9d
2 changed files with 17 additions and 16 deletions

View File

@ -42,6 +42,7 @@ func StartClient(iFace *water.Interface, config config.Config) {
func tunToQuic(config config.Config, stream quic.Stream, iFace *water.Interface) {
packet := make([]byte, config.BufferSize)
shb := make([]byte, 2)
defer stream.Close()
for {
shn, err := iFace.Read(packet)
if err != nil {
@ -57,8 +58,9 @@ func tunToQuic(config config.Config, stream quic.Stream, iFace *water.Interface)
}
shb[0] = byte(shn >> 8 & 0xff)
shb[1] = byte(shn & 0xff)
b = append(shb, b...)
n, err := stream.Write(b)
copy(packet[len(shb):len(shb)+len(b)], b)
copy(packet[:len(shb)], shb)
n, err := stream.Write(packet[:len(shb)+len(b)])
if err != nil {
netutil.PrintErr(err, config.Verbose)
break
@ -69,8 +71,9 @@ func tunToQuic(config config.Config, stream quic.Stream, iFace *water.Interface)
// quicToTun sends packets from quic to tun
func quicToTun(config config.Config, stream quic.Stream, iFace *water.Interface) {
var packet []byte
var shb = make([]byte, 2)
packet := make([]byte, config.BufferSize)
shb := make([]byte, 2)
defer stream.Close()
for {
n, err := stream.Read(shb)
if err != nil {
@ -83,11 +86,10 @@ func quicToTun(config config.Config, stream quic.Stream, iFace *water.Interface)
shn := 0
shn = ((shn & 0x00) | int(shb[0])) << 8
shn = shn | int(shb[1])
packet = make([]byte, shn)
splitSize := 64
splitSize := 99
var count = 0
if shn < splitSize {
n, err = stream.Read(packet)
n, err = stream.Read(packet[:shn])
if err != nil {
netutil.PrintErr(err, config.Verbose)
break
@ -99,13 +101,11 @@ func quicToTun(config config.Config, stream quic.Stream, iFace *water.Interface)
if shn-count < splitSize {
receiveSize = shn - count
}
buffer := make([]byte, receiveSize)
n, err = stream.Read(buffer)
n, err = stream.Read(packet[count : count+receiveSize])
if err != nil {
netutil.PrintErr(err, config.Verbose)
break
}
copy(packet[count:count+n], buffer[:n])
count += n
}
}

View File

@ -74,8 +74,9 @@ func toClient(config config.Config, iFace *water.Interface) {
b = snappy.Encode(nil, b)
}
stream := v.(quic.Stream)
b = append(shb, b...)
n, err := stream.Write(b)
copy(packet[len(shb):len(shb)+len(b)], b)
copy(packet[:len(shb)], shb)
n, err := stream.Write(packet[:len(shb)+len(b)])
if err != nil {
netutil.PrintErr(err, config.Verbose)
break
@ -88,8 +89,9 @@ func toClient(config config.Config, iFace *water.Interface) {
// toServer sends packets from quicConn to iface
func toServer(config config.Config, stream quic.Stream, iface *water.Interface) {
var packet []byte
packet := make([]byte, config.BufferSize)
shb := make([]byte, 2)
defer stream.Close()
for {
n, err := stream.Read(shb)
if err != nil {
@ -97,13 +99,12 @@ func toServer(config config.Config, stream quic.Stream, iface *water.Interface)
break
}
if n < 2 {
continue
break
}
shn := 0
shn = ((shn & 0x00) | int(shb[0])) << 8
shn = shn | int(shb[1])
packet = make([]byte, shn)
n, err = stream.Read(packet)
n, err = stream.Read(packet[:shn])
if err == io.EOF || err != nil {
netutil.PrintErr(err, config.Verbose)
break