mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-05 18:57:30 +03:00
39 lines
619 B
Go
39 lines
619 B
Go
package onclose
|
|
|
|
import (
|
|
"net"
|
|
"sync"
|
|
)
|
|
|
|
type CloseHandlerFunc = func()
|
|
|
|
type Conn struct {
|
|
net.Conn
|
|
onClose func()
|
|
once sync.Once
|
|
}
|
|
|
|
func NewConn(conn net.Conn, onClose func()) *Conn {
|
|
return &Conn{Conn: conn, onClose: onClose}
|
|
}
|
|
|
|
func (c *Conn) Close() error {
|
|
c.once.Do(c.onClose)
|
|
return c.Conn.Close()
|
|
}
|
|
|
|
type PacketConn struct {
|
|
net.PacketConn
|
|
onClose func()
|
|
once sync.Once
|
|
}
|
|
|
|
func NewPacketConn(conn net.PacketConn, onClose func()) *PacketConn {
|
|
return &PacketConn{PacketConn: conn, onClose: onClose}
|
|
}
|
|
|
|
func (c *PacketConn) Close() error {
|
|
c.once.Do(c.onClose)
|
|
return c.PacketConn.Close()
|
|
}
|