2021-06-15 17:13:40 +08:00
|
|
|
package net
|
2020-05-12 11:29:53 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"net"
|
2023-01-16 09:42:03 +08:00
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/common/buf"
|
2020-05-12 11:29:53 +08:00
|
|
|
)
|
|
|
|
|
2023-01-16 10:50:31 +08:00
|
|
|
var _ ExtendedConn = (*BufferedConn)(nil)
|
2023-01-16 09:42:03 +08:00
|
|
|
|
2020-05-12 11:29:53 +08:00
|
|
|
type BufferedConn struct {
|
|
|
|
r *bufio.Reader
|
2023-01-16 10:50:31 +08:00
|
|
|
ExtendedConn
|
2023-02-24 09:54:54 +08:00
|
|
|
peeked bool
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBufferedConn(c net.Conn) *BufferedConn {
|
2021-09-28 23:15:53 +08:00
|
|
|
if bc, ok := c.(*BufferedConn); ok {
|
|
|
|
return bc
|
|
|
|
}
|
2023-02-24 09:54:54 +08:00
|
|
|
return &BufferedConn{bufio.NewReader(c), NewExtendedConn(c), false}
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
|
2023-11-02 10:31:58 +08:00
|
|
|
func WarpConnWithBioReader(c net.Conn, br *bufio.Reader) net.Conn {
|
|
|
|
if br != nil && br.Buffered() > 0 {
|
|
|
|
if bc, ok := c.(*BufferedConn); ok && bc.r == br {
|
|
|
|
return bc
|
|
|
|
}
|
|
|
|
return &BufferedConn{br, NewExtendedConn(c), true}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-05-12 11:29:53 +08:00
|
|
|
// Reader returns the internal bufio.Reader.
|
|
|
|
func (c *BufferedConn) Reader() *bufio.Reader {
|
|
|
|
return c.r
|
|
|
|
}
|
|
|
|
|
2023-02-25 19:41:01 +08:00
|
|
|
func (c *BufferedConn) ResetPeeked() {
|
|
|
|
c.peeked = false
|
|
|
|
}
|
|
|
|
|
2023-02-24 09:54:54 +08:00
|
|
|
func (c *BufferedConn) Peeked() bool {
|
|
|
|
return c.peeked
|
|
|
|
}
|
|
|
|
|
2020-05-12 11:29:53 +08:00
|
|
|
// Peek returns the next n bytes without advancing the reader.
|
|
|
|
func (c *BufferedConn) Peek(n int) ([]byte, error) {
|
2023-02-24 09:54:54 +08:00
|
|
|
c.peeked = true
|
2020-05-12 11:29:53 +08:00
|
|
|
return c.r.Peek(n)
|
|
|
|
}
|
|
|
|
|
2023-02-24 09:54:54 +08:00
|
|
|
func (c *BufferedConn) Discard(n int) (discarded int, err error) {
|
|
|
|
return c.r.Discard(n)
|
|
|
|
}
|
|
|
|
|
2020-05-12 11:29:53 +08:00
|
|
|
func (c *BufferedConn) Read(p []byte) (int, error) {
|
|
|
|
return c.r.Read(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BufferedConn) ReadByte() (byte, error) {
|
|
|
|
return c.r.ReadByte()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BufferedConn) UnreadByte() error {
|
|
|
|
return c.r.UnreadByte()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *BufferedConn) Buffered() int {
|
|
|
|
return c.r.Buffered()
|
|
|
|
}
|
2023-01-16 09:42:03 +08:00
|
|
|
|
|
|
|
func (c *BufferedConn) ReadBuffer(buffer *buf.Buffer) (err error) {
|
2023-05-11 11:30:20 +08:00
|
|
|
if c.r != nil && c.r.Buffered() > 0 {
|
2023-01-16 09:42:03 +08:00
|
|
|
_, err = buffer.ReadOnceFrom(c.r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return c.ExtendedConn.ReadBuffer(buffer)
|
|
|
|
}
|
|
|
|
|
2023-04-25 23:01:05 +08:00
|
|
|
func (c *BufferedConn) ReadCached() *buf.Buffer { // call in sing/common/bufio.Copy
|
2023-05-11 11:30:20 +08:00
|
|
|
if c.r != nil && c.r.Buffered() > 0 {
|
2023-04-25 23:01:05 +08:00
|
|
|
length := c.r.Buffered()
|
|
|
|
b, _ := c.r.Peek(length)
|
|
|
|
_, _ = c.r.Discard(length)
|
|
|
|
return buf.As(b)
|
|
|
|
}
|
2023-11-09 22:19:29 +08:00
|
|
|
c.r = nil // drop bufio.Reader to let gc can clean up its internal buf
|
2023-04-25 23:01:05 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 09:42:03 +08:00
|
|
|
func (c *BufferedConn) Upstream() any {
|
|
|
|
return c.ExtendedConn
|
|
|
|
}
|
2023-01-16 12:28:30 +08:00
|
|
|
|
|
|
|
func (c *BufferedConn) ReaderReplaceable() bool {
|
2023-05-11 11:30:20 +08:00
|
|
|
if c.r != nil && c.r.Buffered() > 0 {
|
2023-01-16 12:28:30 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2023-04-17 00:23:12 +08:00
|
|
|
|
|
|
|
func (c *BufferedConn) WriterReplaceable() bool {
|
|
|
|
return true
|
|
|
|
}
|