1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-19 10:56:06 -05:00
v2fly/app/proxyman/mux/reader.go

96 lines
1.9 KiB
Go
Raw Normal View History

2017-02-07 15:11:47 -05:00
package mux
2017-04-02 03:48:30 -04:00
import (
"io"
2017-02-07 15:11:47 -05:00
2017-04-02 03:48:30 -04:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/serial"
)
2017-10-30 07:36:31 -04:00
// ReadMetadata reads FrameMetadata from the given reader.
2017-10-27 10:32:00 -04:00
func ReadMetadata(reader io.Reader) (*FrameMetadata, error) {
metaLen, err := serial.ReadUint16(reader)
2017-05-02 16:23:07 -04:00
if err != nil {
2017-02-07 15:11:47 -05:00
return nil, err
}
2017-04-03 06:55:46 -04:00
if metaLen > 512 {
2017-12-27 16:25:12 -05:00
return nil, newError("invalid metalen ", metaLen).AtError()
2017-04-03 06:55:46 -04:00
}
2017-05-02 16:23:07 -04:00
b := buf.NewSize(uint32(metaLen))
2017-10-27 10:32:00 -04:00
defer b.Release()
2018-04-01 18:44:47 -04:00
if err := b.Reset(buf.ReadFullFrom(reader, int32(metaLen))); err != nil {
2017-02-07 15:11:47 -05:00
return nil, err
}
2018-02-23 18:57:54 -05:00
return ReadFrameFrom(b)
2017-05-02 16:23:07 -04:00
}
2017-10-30 07:36:31 -04:00
// PacketReader is an io.Reader that reads whole chunk of Mux frames every time.
2017-05-02 16:23:07 -04:00
type PacketReader struct {
reader io.Reader
eof bool
2017-02-07 15:11:47 -05:00
}
2017-10-30 07:36:31 -04:00
// NewPacketReader creates a new PacketReader.
2017-05-02 16:23:07 -04:00
func NewPacketReader(reader io.Reader) *PacketReader {
return &PacketReader{
reader: reader,
eof: false,
2017-04-27 16:20:29 -04:00
}
}
2017-11-09 16:33:15 -05:00
// ReadMultiBuffer implements buf.Reader.
func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
2017-05-02 16:23:07 -04:00
if r.eof {
return nil, io.EOF
}
size, err := serial.ReadUint16(r.reader)
if err != nil {
return nil, err
}
2018-03-12 17:10:13 -04:00
b := buf.NewSize(uint32(size))
2018-04-01 18:44:47 -04:00
if err := b.Reset(buf.ReadFullFrom(r.reader, int32(size))); err != nil {
2017-05-02 16:23:07 -04:00
b.Release()
return nil, err
}
r.eof = true
return buf.NewMultiBufferValue(b), nil
}
2017-10-30 07:36:31 -04:00
// StreamReader reads Mux frame as a stream.
2017-05-02 16:23:07 -04:00
type StreamReader struct {
reader *buf.BufferedReader
2018-03-12 22:39:36 -04:00
leftOver int32
2017-05-02 16:23:07 -04:00
}
2017-10-30 07:36:31 -04:00
// NewStreamReader creates a new StreamReader.
func NewStreamReader(reader *buf.BufferedReader) *StreamReader {
2017-05-02 16:23:07 -04:00
return &StreamReader{
reader: reader,
leftOver: -1,
}
}
2017-11-09 16:33:15 -05:00
// ReadMultiBuffer implmenets buf.Reader.
func (r *StreamReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
2017-04-27 16:20:29 -04:00
if r.leftOver == 0 {
r.leftOver = -1
return nil, io.EOF
}
2017-05-02 16:23:07 -04:00
2017-04-27 16:20:29 -04:00
if r.leftOver == -1 {
2017-05-02 16:23:07 -04:00
size, err := serial.ReadUint16(r.reader)
if err != nil {
2017-04-27 16:20:29 -04:00
return nil, err
}
2018-03-12 22:39:36 -04:00
r.leftOver = int32(size)
2017-02-07 15:11:47 -05:00
}
2018-04-02 14:00:50 -04:00
mb, err := r.reader.ReadAtMost(r.leftOver)
r.leftOver -= mb.Len()
return mb, err
2017-02-07 15:11:47 -05:00
}