tun 不用chan而用回调函数,增强性能

用chan完全多此一举
This commit is contained in:
e1732a364fed
2000-01-01 00:00:00 +00:00
parent 92f2c1df70
commit d27b659bf7
2 changed files with 26 additions and 31 deletions

View File

@@ -51,10 +51,7 @@ func (sc *StackCloser) Close() error {
return nil
}
func Listen(dev device.Device) (tcpChan chan netLayer.TCPRequestInfo, udpChan chan netLayer.UDPRequestInfo, closer io.Closer, err error) {
tcpChan = make(chan netLayer.TCPRequestInfo)
udpChan = make(chan netLayer.UDPRequestInfo)
func Listen(dev device.Device, tcpFunc func(netLayer.TCPRequestInfo), udpFunc func(netLayer.UDPRequestInfo)) (closer io.Closer, err error) {
s := stack.New(stack.Options{
NetworkProtocols: []stack.NetworkProtocolFactory{
@@ -130,7 +127,7 @@ func Listen(dev device.Device) (tcpChan chan netLayer.TCPRequestInfo, udpChan ch
// log.Printf("forward tcp request %s:%d->%s:%d\n",
// id.RemoteAddress, id.RemotePort, id.LocalAddress, id.LocalPort)
tcpChan <- info
go tcpFunc(info)
r.Complete(false)
})
@@ -160,7 +157,7 @@ func Listen(dev device.Device) (tcpChan chan netLayer.TCPRequestInfo, udpChan ch
},
}
udpChan <- info
go udpFunc(info)
})
s.SetTransportProtocolHandler(udp.ProtocolNumber, udpForwarder.HandlePacket)

View File

@@ -242,7 +242,29 @@ func (s *Server) StartListen(tcpFunc func(netLayer.TCPRequestInfo), udpFunc func
autoRouteFunc(s.devName, s.realIP, s.selfip, s.autoRouteDirectList)
}
newTchan, newUchan, stackCloser, err := tun.Listen(tunDev)
newTcpFunc := func(info netLayer.TCPRequestInfo) {
if s.stopped {
return
}
if ce := utils.CanLogInfo("tun got new tcp"); ce != nil {
ce.Write(zap.String("->", info.Target.String()))
}
tcpFunc(info)
}
newUdpFunc := func(info netLayer.UDPRequestInfo) {
if s.stopped {
return
}
if ce := utils.CanLogInfo("tun got new udp"); ce != nil {
ce.Write(zap.String("->", info.Target.String()))
}
udpFunc(info)
}
stackCloser, err := tun.Listen(tunDev, newTcpFunc, newUdpFunc)
if err != nil {
if ce := utils.CanLogErr("tun listen failed"); ce != nil {
@@ -252,30 +274,6 @@ func (s *Server) StartListen(tcpFunc func(netLayer.TCPRequestInfo), udpFunc func
return nil
}
go func() {
for tr := range newTchan {
if s.stopped {
return
}
if ce := utils.CanLogInfo("tun got new tcp"); ce != nil {
ce.Write(zap.String("->", tr.Target.String()))
}
go tcpFunc(tr)
}
}()
go func() {
for ur := range newUchan {
if s.stopped {
return
}
if ce := utils.CanLogInfo("tun got new udp"); ce != nil {
ce.Write(zap.String("->", ur.Target.String()))
}
go udpFunc(ur)
}
}()
s.stackCloser = stackCloser
s.tunDev = tunDev