github.com/kelleygo/clashcore@v1.0.2/common/net/refconn.go (about) 1 package net 2 3 import ( 4 "net" 5 "runtime" 6 "time" 7 8 "github.com/kelleygo/clashcore/common/buf" 9 ) 10 11 type refConn struct { 12 conn ExtendedConn 13 ref any 14 } 15 16 func (c *refConn) Read(b []byte) (n int, err error) { 17 defer runtime.KeepAlive(c.ref) 18 return c.conn.Read(b) 19 } 20 21 func (c *refConn) Write(b []byte) (n int, err error) { 22 defer runtime.KeepAlive(c.ref) 23 return c.conn.Write(b) 24 } 25 26 func (c *refConn) Close() error { 27 defer runtime.KeepAlive(c.ref) 28 return c.conn.Close() 29 } 30 31 func (c *refConn) LocalAddr() net.Addr { 32 defer runtime.KeepAlive(c.ref) 33 return c.conn.LocalAddr() 34 } 35 36 func (c *refConn) RemoteAddr() net.Addr { 37 defer runtime.KeepAlive(c.ref) 38 return c.conn.RemoteAddr() 39 } 40 41 func (c *refConn) SetDeadline(t time.Time) error { 42 defer runtime.KeepAlive(c.ref) 43 return c.conn.SetDeadline(t) 44 } 45 46 func (c *refConn) SetReadDeadline(t time.Time) error { 47 defer runtime.KeepAlive(c.ref) 48 return c.conn.SetReadDeadline(t) 49 } 50 51 func (c *refConn) SetWriteDeadline(t time.Time) error { 52 defer runtime.KeepAlive(c.ref) 53 return c.conn.SetWriteDeadline(t) 54 } 55 56 func (c *refConn) Upstream() any { 57 return c.conn 58 } 59 60 func (c *refConn) ReadBuffer(buffer *buf.Buffer) error { 61 defer runtime.KeepAlive(c.ref) 62 return c.conn.ReadBuffer(buffer) 63 } 64 65 func (c *refConn) WriteBuffer(buffer *buf.Buffer) error { 66 defer runtime.KeepAlive(c.ref) 67 return c.conn.WriteBuffer(buffer) 68 } 69 70 func (c *refConn) ReaderReplaceable() bool { // Relay() will handle reference 71 return true 72 } 73 74 func (c *refConn) WriterReplaceable() bool { // Relay() will handle reference 75 return true 76 } 77 78 var _ ExtendedConn = (*refConn)(nil) 79 80 func NewRefConn(conn net.Conn, ref any) net.Conn { 81 return &refConn{conn: NewExtendedConn(conn), ref: ref} 82 }