mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-21 09:40:04 +08:00
chore: simplify N.Relay
This commit is contained in:
parent
8cdfd87d1e
commit
5e17d6fe01
@ -1,9 +1,8 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net"
|
||||
"runtime"
|
||||
|
||||
"github.com/metacubex/mihomo/common/net/deadline"
|
||||
|
||||
@ -56,9 +55,37 @@ type CountFunc = network.CountFunc
|
||||
|
||||
var Pipe = deadline.Pipe
|
||||
|
||||
// Relay copies between left and right bidirectionally.
|
||||
func Relay(leftConn, rightConn net.Conn) {
|
||||
defer runtime.KeepAlive(leftConn)
|
||||
defer runtime.KeepAlive(rightConn)
|
||||
_ = bufio.CopyConn(context.TODO(), leftConn, rightConn)
|
||||
func closeWrite(writer io.Closer) error {
|
||||
if c, ok := common.Cast[network.WriteCloser](writer); ok {
|
||||
return c.CloseWrite()
|
||||
}
|
||||
return writer.Close()
|
||||
}
|
||||
|
||||
// Relay copies between left and right bidirectionally.
|
||||
// like [bufio.CopyConn] but remove unneeded [context.Context] handle and the cost of [task.Group]
|
||||
func Relay(leftConn, rightConn net.Conn) {
|
||||
defer func() {
|
||||
_ = leftConn.Close()
|
||||
_ = rightConn.Close()
|
||||
}()
|
||||
|
||||
ch := make(chan struct{})
|
||||
go func() {
|
||||
_, err := bufio.Copy(leftConn, rightConn)
|
||||
if err == nil {
|
||||
_ = closeWrite(leftConn)
|
||||
} else {
|
||||
_ = leftConn.Close()
|
||||
}
|
||||
close(ch)
|
||||
}()
|
||||
|
||||
_, err := bufio.Copy(rightConn, leftConn)
|
||||
if err == nil {
|
||||
_ = closeWrite(rightConn)
|
||||
} else {
|
||||
_ = rightConn.Close()
|
||||
}
|
||||
<-ch
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user