github.com/metacubex/mihomo@v1.18.5/common/net/cached.go (about) 1 package net 2 3 import ( 4 "net" 5 6 "github.com/metacubex/mihomo/common/buf" 7 ) 8 9 var _ ExtendedConn = (*CachedConn)(nil) 10 11 type CachedConn struct { 12 ExtendedConn 13 data []byte 14 } 15 16 func NewCachedConn(c net.Conn, data []byte) *CachedConn { 17 return &CachedConn{NewExtendedConn(c), data} 18 } 19 20 func (c *CachedConn) Read(b []byte) (n int, err error) { 21 if len(c.data) > 0 { 22 n = copy(b, c.data) 23 c.data = c.data[n:] 24 return 25 } 26 return c.ExtendedConn.Read(b) 27 } 28 29 func (c *CachedConn) ReadCached() *buf.Buffer { // call in sing/common/bufio.Copy 30 if len(c.data) > 0 { 31 return buf.As(c.data) 32 } 33 return nil 34 } 35 36 func (c *CachedConn) Upstream() any { 37 return c.ExtendedConn 38 } 39 40 func (c *CachedConn) ReaderReplaceable() bool { 41 if len(c.data) > 0 { 42 return false 43 } 44 return true 45 } 46 47 func (c *CachedConn) WriterReplaceable() bool { 48 return true 49 }