v2ray-core/common/buf/io.go

104 lines
2.1 KiB
Go
Raw Normal View History

2016-12-09 11:08:25 +00:00
package buf
2016-12-09 12:17:34 +00:00
import (
"io"
2018-12-03 13:20:57 +00:00
"net"
2018-07-24 19:48:28 +00:00
"syscall"
2017-03-27 09:12:34 +00:00
"time"
2016-12-09 12:17:34 +00:00
)
2017-11-09 21:33:15 +00:00
// Reader extends io.Reader with MultiBuffer.
2016-12-09 12:17:34 +00:00
type Reader interface {
2017-11-09 21:33:15 +00:00
// ReadMultiBuffer reads content from underlying reader, and put it into a MultiBuffer.
ReadMultiBuffer() (MultiBuffer, error)
2016-12-09 12:17:34 +00:00
}
2017-10-22 13:03:20 +00:00
// ErrReadTimeout is an error that happens with IO timeout.
2017-04-09 13:04:04 +00:00
var ErrReadTimeout = newError("IO timeout")
2017-03-27 09:12:34 +00:00
2017-11-08 17:14:44 +00:00
// TimeoutReader is a reader that returns error if Read() operation takes longer than the given timeout.
2017-03-27 09:12:34 +00:00
type TimeoutReader interface {
ReadMultiBufferTimeout(time.Duration) (MultiBuffer, error)
2017-03-27 09:12:34 +00:00
}
2017-11-09 21:33:15 +00:00
// Writer extends io.Writer with MultiBuffer.
2016-12-09 12:17:34 +00:00
type Writer interface {
2017-11-09 21:33:15 +00:00
// WriteMultiBuffer writes a MultiBuffer into underlying writer.
WriteMultiBuffer(MultiBuffer) error
2016-12-09 12:17:34 +00:00
}
2016-12-09 11:08:25 +00:00
2018-07-31 11:43:27 +00:00
// WriteAllBytes ensures all bytes are written into the given writer.
2018-07-28 13:03:40 +00:00
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
}
2018-12-03 13:20:57 +00:00
func isPacketReader(reader io.Reader) bool {
_, ok := reader.(net.PacketConn)
return ok
}
2016-12-09 12:17:34 +00:00
// NewReader creates a new Reader.
// The Reader instance doesn't take the ownership of reader.
func NewReader(reader io.Reader) Reader {
2017-11-09 21:33:15 +00:00
if mr, ok := reader.(Reader); ok {
return mr
2017-04-15 20:22:29 +00:00
}
2018-12-03 13:20:57 +00:00
if isPacketReader(reader) {
return &PacketReader{
Reader: reader,
}
}
2018-08-25 08:41:39 +00:00
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,
}
2016-12-09 12:17:34 +00:00
}
2018-12-03 13:20:57 +00:00
func isPacketWriter(writer io.Writer) bool {
if _, ok := writer.(net.PacketConn); ok {
return true
}
// If the writer doesn't implement syscall.Conn, it is probably not a TCP connection.
if _, ok := writer.(syscall.Conn); !ok {
return true
}
return false
}
2016-12-09 12:17:34 +00:00
// NewWriter creates a new Writer.
func NewWriter(writer io.Writer) Writer {
2017-11-09 21:33:15 +00:00
if mw, ok := writer.(Writer); ok {
return mw
2017-04-19 19:27:21 +00:00
}
2018-12-03 13:20:57 +00:00
if isPacketWriter(writer) {
return &SequentialWriter{
Writer: writer,
}
}
2016-12-09 12:17:34 +00:00
return &BufferToBytesWriter{
2017-11-09 21:33:15 +00:00
Writer: writer,
2017-04-20 09:00:15 +00:00
}
}