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-05-02 16:23:07 -04:00
|
|
|
type MetadataReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
buffer []byte
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
2017-05-02 16:23:07 -04:00
|
|
|
func NewMetadataReader(reader io.Reader) *MetadataReader {
|
|
|
|
return &MetadataReader{
|
|
|
|
reader: reader,
|
|
|
|
buffer: make([]byte, 1024),
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 16:23:07 -04:00
|
|
|
func (r *MetadataReader) Read() (*FrameMetadata, error) {
|
|
|
|
metaLen, err := serial.ReadUint16(r.reader)
|
|
|
|
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-04-19 04:14:52 -04:00
|
|
|
return nil, newError("invalid metalen ", metaLen).AtWarning()
|
2017-04-03 06:55:46 -04:00
|
|
|
}
|
2017-05-02 16:23:07 -04:00
|
|
|
|
|
|
|
if _, err := io.ReadFull(r.reader, r.buffer[:metaLen]); err != nil {
|
2017-02-07 15:11:47 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-02 16:23:07 -04:00
|
|
|
return ReadFrameFrom(r.buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
type PacketReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
eof bool
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
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-05-02 16:23:07 -04:00
|
|
|
func (r *PacketReader) Read() (buf.MultiBuffer, error) {
|
|
|
|
if r.eof {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
size, err := serial.ReadUint16(r.reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var b *buf.Buffer
|
|
|
|
if size <= buf.Size {
|
|
|
|
b = buf.New()
|
|
|
|
} else {
|
|
|
|
b = buf.NewLocal(int(size))
|
|
|
|
}
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, int(size))); err != nil {
|
|
|
|
b.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r.eof = true
|
|
|
|
return buf.NewMultiBufferValue(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type StreamReader struct {
|
|
|
|
reader io.Reader
|
|
|
|
leftOver int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStreamReader(reader io.Reader) *StreamReader {
|
|
|
|
return &StreamReader{
|
|
|
|
reader: reader,
|
|
|
|
leftOver: -1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *StreamReader) Read() (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
|
|
|
|
}
|
2017-05-02 16:23:07 -04:00
|
|
|
r.leftOver = int(size)
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
|
|
|
|
2017-04-15 15:07:23 -04:00
|
|
|
mb := buf.NewMultiBuffer()
|
2017-04-27 16:20:29 -04:00
|
|
|
for r.leftOver > 0 {
|
2017-04-15 15:07:23 -04:00
|
|
|
readLen := buf.Size
|
2017-04-27 16:20:29 -04:00
|
|
|
if r.leftOver < readLen {
|
|
|
|
readLen = r.leftOver
|
2017-04-15 15:07:23 -04:00
|
|
|
}
|
2017-04-19 10:29:36 -04:00
|
|
|
b := buf.New()
|
2017-04-27 16:20:29 -04:00
|
|
|
if err := b.AppendSupplier(func(bb []byte) (int, error) {
|
|
|
|
return r.reader.Read(bb[:readLen])
|
|
|
|
}); err != nil {
|
2017-04-15 15:07:23 -04:00
|
|
|
mb.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-27 16:20:29 -04:00
|
|
|
r.leftOver -= b.Len()
|
2017-04-15 15:07:23 -04:00
|
|
|
mb.Append(b)
|
2017-04-27 16:20:29 -04:00
|
|
|
if b.Len() < readLen {
|
|
|
|
break
|
|
|
|
}
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|
2017-04-15 15:07:23 -04:00
|
|
|
return mb, nil
|
2017-02-07 15:11:47 -05:00
|
|
|
}
|