mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 10:08:15 -05:00
95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
package buf
|
|
|
|
import (
|
|
"io"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// Reader extends io.Reader with MultiBuffer.
|
|
type Reader interface {
|
|
// ReadMultiBuffer reads content from underlying reader, and put it into a MultiBuffer.
|
|
ReadMultiBuffer() (MultiBuffer, error)
|
|
}
|
|
|
|
// ErrReadTimeout is an error that happens with IO timeout.
|
|
var ErrReadTimeout = newError("IO timeout")
|
|
|
|
// TimeoutReader is a reader that returns error if Read() operation takes longer than the given timeout.
|
|
type TimeoutReader interface {
|
|
ReadMultiBufferTimeout(time.Duration) (MultiBuffer, error)
|
|
}
|
|
|
|
// Writer extends io.Writer with MultiBuffer.
|
|
type Writer interface {
|
|
// WriteMultiBuffer writes a MultiBuffer into underlying writer.
|
|
WriteMultiBuffer(MultiBuffer) error
|
|
}
|
|
|
|
// ReadFrom creates a Supplier to read from a given io.Reader.
|
|
func ReadFrom(reader io.Reader) Supplier {
|
|
return func(b []byte) (int, error) {
|
|
return reader.Read(b)
|
|
}
|
|
}
|
|
|
|
// ReadFullFrom creates a Supplier to read full buffer from a given io.Reader.
|
|
func ReadFullFrom(reader io.Reader, size int32) Supplier {
|
|
return func(b []byte) (int, error) {
|
|
return io.ReadFull(reader, b[:size])
|
|
}
|
|
}
|
|
|
|
// WriteAllBytes ensures all bytes are written into the given writer.
|
|
func WriteAllBytes(writer io.Writer, payload []byte) error {
|
|
for len(payload) > 0 {
|
|
n, err := writer.Write(payload)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
payload = payload[n:]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// NewReader creates a new Reader.
|
|
// The Reader instance doesn't take the ownership of reader.
|
|
func NewReader(reader io.Reader) Reader {
|
|
if mr, ok := reader.(Reader); ok {
|
|
return mr
|
|
}
|
|
|
|
if useReadv {
|
|
if sc, ok := reader.(syscall.Conn); ok {
|
|
rawConn, err := sc.SyscallConn()
|
|
if err != nil {
|
|
newError("failed to get sysconn").Base(err).WriteToLog()
|
|
} else {
|
|
return NewReadVReader(reader, rawConn)
|
|
}
|
|
}
|
|
}
|
|
|
|
return &SingleReader{
|
|
Reader: reader,
|
|
}
|
|
}
|
|
|
|
// NewWriter creates a new Writer.
|
|
func NewWriter(writer io.Writer) Writer {
|
|
if mw, ok := writer.(Writer); ok {
|
|
return mw
|
|
}
|
|
|
|
if _, ok := writer.(syscall.Conn); !ok {
|
|
// If the writer doesn't implement syscall.Conn, it is probably not a TCP connection.
|
|
return &SequentialWriter{
|
|
Writer: writer,
|
|
}
|
|
}
|
|
|
|
return &BufferToBytesWriter{
|
|
Writer: writer,
|
|
}
|
|
}
|