2017-02-07 20:11:47 +00:00
|
|
|
package mux
|
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
import (
|
|
|
|
"io"
|
2017-02-07 20:11:47 +00:00
|
|
|
|
2017-04-02 07:48:30 +00:00
|
|
|
"v2ray.com/core/common/buf"
|
|
|
|
"v2ray.com/core/common/serial"
|
|
|
|
)
|
|
|
|
|
2017-04-02 11:43:24 +00:00
|
|
|
type Reader struct {
|
2017-04-27 20:20:29 +00:00
|
|
|
reader io.Reader
|
|
|
|
buffer *buf.Buffer
|
|
|
|
leftOver int
|
2017-02-07 20:11:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-02 11:43:24 +00:00
|
|
|
func NewReader(reader buf.Reader) *Reader {
|
|
|
|
return &Reader{
|
2017-04-27 20:20:29 +00:00
|
|
|
reader: buf.ToBytesReader(reader),
|
|
|
|
buffer: buf.NewLocal(1024),
|
|
|
|
leftOver: -1,
|
2017-02-07 20:11:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-02 11:43:24 +00:00
|
|
|
func (r *Reader) ReadMetadata() (*FrameMetadata, error) {
|
2017-04-27 20:20:29 +00:00
|
|
|
r.leftOver = -1
|
|
|
|
|
2017-02-07 20:11:47 +00:00
|
|
|
b := r.buffer
|
|
|
|
b.Clear()
|
|
|
|
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, 2)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
metaLen := serial.BytesToUint16(b.Bytes())
|
2017-04-03 10:55:46 +00:00
|
|
|
if metaLen > 512 {
|
2017-04-19 08:14:52 +00:00
|
|
|
return nil, newError("invalid metalen ", metaLen).AtWarning()
|
2017-04-03 10:55:46 +00:00
|
|
|
}
|
2017-02-07 20:11:47 +00:00
|
|
|
b.Clear()
|
|
|
|
if err := b.AppendSupplier(buf.ReadFullFrom(r.reader, int(metaLen))); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ReadFrameFrom(b.Bytes())
|
|
|
|
}
|
|
|
|
|
2017-04-27 20:20:29 +00:00
|
|
|
func (r *Reader) readSize() error {
|
2017-04-19 14:29:36 +00:00
|
|
|
if err := r.buffer.Reset(buf.ReadFullFrom(r.reader, 2)); err != nil {
|
2017-04-27 20:20:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.leftOver = int(serial.BytesToUint16(r.buffer.Bytes()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Reader) Read() (buf.MultiBuffer, error) {
|
|
|
|
if r.leftOver == 0 {
|
|
|
|
r.leftOver = -1
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
if r.leftOver == -1 {
|
|
|
|
if err := r.readSize(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-07 20:11:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 19:07:23 +00:00
|
|
|
mb := buf.NewMultiBuffer()
|
2017-04-27 20:20:29 +00:00
|
|
|
for r.leftOver > 0 {
|
2017-04-15 19:07:23 +00:00
|
|
|
readLen := buf.Size
|
2017-04-27 20:20:29 +00:00
|
|
|
if r.leftOver < readLen {
|
|
|
|
readLen = r.leftOver
|
2017-04-15 19:07:23 +00:00
|
|
|
}
|
2017-04-19 14:29:36 +00:00
|
|
|
b := buf.New()
|
2017-04-27 20:20:29 +00:00
|
|
|
if err := b.AppendSupplier(func(bb []byte) (int, error) {
|
|
|
|
return r.reader.Read(bb[:readLen])
|
|
|
|
}); err != nil {
|
2017-04-15 19:07:23 +00:00
|
|
|
mb.Release()
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-27 20:20:29 +00:00
|
|
|
r.leftOver -= b.Len()
|
2017-04-15 19:07:23 +00:00
|
|
|
mb.Append(b)
|
2017-04-27 20:20:29 +00:00
|
|
|
if b.Len() < readLen {
|
|
|
|
break
|
|
|
|
}
|
2017-02-07 20:11:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 19:07:23 +00:00
|
|
|
return mb, nil
|
2017-02-07 20:11:47 +00:00
|
|
|
}
|