refactor: remove wrapper class

This commit is contained in:
anytls 2025-02-17 14:23:55 +09:00
parent b012a3cbba
commit 3a9897396f
3 changed files with 8 additions and 40 deletions

View file

@ -1,36 +0,0 @@
package extend
import (
"net"
"sync"
)
type CloseHookConn struct {
net.Conn
closeOnce sync.Once
closeFunc func()
}
func NewCloseHookConn(conn net.Conn, closeFunc func()) *CloseHookConn {
return &CloseHookConn{Conn: conn, closeFunc: closeFunc}
}
func (c *CloseHookConn) Close() error {
c.closeOnce.Do(func() {
c.closeFunc()
c.closeFunc = nil
})
return c.Conn.Close()
}
func (c *CloseHookConn) Upstream() any {
return c.Conn
}
func (c *CloseHookConn) ReaderReplaceable() bool {
return true
}
func (c *CloseHookConn) WriterReplaceable() bool {
return true
}

View file

@ -10,7 +10,6 @@ import (
"sync/atomic"
"time"
"github.com/metacubex/mihomo/transport/anytls/extend"
"github.com/metacubex/mihomo/transport/anytls/padding"
"github.com/metacubex/mihomo/transport/anytls/skiplist"
"github.com/metacubex/mihomo/transport/anytls/util"
@ -78,7 +77,7 @@ func (c *Client) CreateStream(ctx context.Context) (net.Conn, error) {
return nil, fmt.Errorf("too many closed session: %w", err)
}
streamC := extend.NewCloseHookConn(stream, func() {
stream.dieHook = func() {
if session.IsClosed() {
if session.dieHook != nil {
session.dieHook()
@ -89,9 +88,9 @@ func (c *Client) CreateStream(ctx context.Context) (net.Conn, error) {
c.idleSession.Insert(math.MaxUint64-session.seq, session)
c.idleSessionLock.Unlock()
}
})
}
return streamC, nil
return stream, nil
}
func (c *Client) findSession(ctx context.Context) (*Session, error) {

View file

@ -18,6 +18,7 @@ type Stream struct {
pipeW *io.PipeWriter
dieOnce sync.Once
dieHook func()
}
// newStream initiates a Stream struct
@ -57,6 +58,10 @@ func (s *Stream) sessionClose() (once bool) {
s.dieOnce.Do(func() {
s.pipeR.Close()
once = true
if s.dieHook != nil {
s.dieHook()
s.dieHook = nil
}
})
return
}