github.com/kelleygo/clashcore@v1.0.2/common/callback/close_callback.go (about)

     1  package callback
     2  
     3  import (
     4  	"sync"
     5  
     6  	C "github.com/kelleygo/clashcore/constant"
     7  )
     8  
     9  type closeCallbackConn struct {
    10  	C.Conn
    11  	closeFunc func()
    12  	closeOnce sync.Once
    13  }
    14  
    15  func (w *closeCallbackConn) Close() error {
    16  	w.closeOnce.Do(w.closeFunc)
    17  	return w.Conn.Close()
    18  }
    19  
    20  func (w *closeCallbackConn) ReaderReplaceable() bool {
    21  	return true
    22  }
    23  
    24  func (w *closeCallbackConn) WriterReplaceable() bool {
    25  	return true
    26  }
    27  
    28  func (w *closeCallbackConn) Upstream() any {
    29  	return w.Conn
    30  }
    31  
    32  func NewCloseCallbackConn(conn C.Conn, callback func()) C.Conn {
    33  	return &closeCallbackConn{Conn: conn, closeFunc: callback}
    34  }
    35  
    36  type closeCallbackPacketConn struct {
    37  	C.PacketConn
    38  	closeFunc func()
    39  	closeOnce sync.Once
    40  }
    41  
    42  func (w *closeCallbackPacketConn) Close() error {
    43  	w.closeOnce.Do(w.closeFunc)
    44  	return w.PacketConn.Close()
    45  }
    46  
    47  func (w *closeCallbackPacketConn) ReaderReplaceable() bool {
    48  	return true
    49  }
    50  
    51  func (w *closeCallbackPacketConn) WriterReplaceable() bool {
    52  	return true
    53  }
    54  
    55  func (w *closeCallbackPacketConn) Upstream() any {
    56  	return w.PacketConn
    57  }
    58  
    59  func NewCloseCallbackPacketConn(conn C.PacketConn, callback func()) C.PacketConn {
    60  	return &closeCallbackPacketConn{PacketConn: conn, closeFunc: callback}
    61  }