github.com/kelleygo/clashcore@v1.0.2/common/net/bufconn_unsafe.go (about) 1 package net 2 3 import ( 4 "io" 5 "unsafe" 6 ) 7 8 // bufioReader copy from stdlib bufio/bufio.go 9 // This structure has remained unchanged from go1.5 to go1.21. 10 type bufioReader struct { 11 buf []byte 12 rd io.Reader // reader provided by the client 13 r, w int // buf read and write positions 14 err error 15 lastByte int // last byte read for UnreadByte; -1 means invalid 16 lastRuneSize int // size of last rune read for UnreadRune; -1 means invalid 17 } 18 19 func (c *BufferedConn) AppendData(buf []byte) (ok bool) { 20 b := (*bufioReader)(unsafe.Pointer(c.r)) 21 pos := len(b.buf) - b.w - len(buf) 22 if pos >= -b.r { // len(b.buf)-(b.w - b.r) >= len(buf) 23 if pos < 0 { // len(b.buf)-b.w < len(buf) 24 // Slide existing data to beginning. 25 copy(b.buf, b.buf[b.r:b.w]) 26 b.w -= b.r 27 b.r = 0 28 } 29 30 b.w += copy(b.buf[b.w:], buf) 31 return true 32 } 33 return false 34 }