mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-26 20:16:55 -05:00
merge stread reader from mux to crypto
This commit is contained in:
parent
e6b5356ea9
commit
128ec95218
@ -4,6 +4,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
|
"v2ray.com/core/common/crypto"
|
||||||
"v2ray.com/core/common/serial"
|
"v2ray.com/core/common/serial"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -64,35 +65,7 @@ func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|||||||
return buf.NewMultiBufferValue(b), nil
|
return buf.NewMultiBufferValue(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamReader reads Mux frame as a stream.
|
|
||||||
type StreamReader struct {
|
|
||||||
reader *buf.BufferedReader
|
|
||||||
leftOver int32
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewStreamReader creates a new StreamReader.
|
// NewStreamReader creates a new StreamReader.
|
||||||
func NewStreamReader(reader *buf.BufferedReader) *StreamReader {
|
func NewStreamReader(reader *buf.BufferedReader) buf.Reader {
|
||||||
return &StreamReader{
|
return crypto.NewChunkStreamReaderWithChunkCount(crypto.PlainChunkSizeParser{}, reader, 1)
|
||||||
reader: reader,
|
|
||||||
leftOver: -1,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadMultiBuffer implmenets buf.Reader.
|
|
||||||
func (r *StreamReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|
||||||
if r.leftOver == 0 {
|
|
||||||
return nil, io.EOF
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.leftOver == -1 {
|
|
||||||
size, err := serial.ReadUint16(r.reader)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
r.leftOver = int32(size)
|
|
||||||
}
|
|
||||||
|
|
||||||
mb, err := r.reader.ReadAtMost(r.leftOver)
|
|
||||||
r.leftOver -= mb.Len()
|
|
||||||
return mb, err
|
|
||||||
}
|
}
|
||||||
|
@ -68,12 +68,19 @@ type ChunkStreamReader struct {
|
|||||||
|
|
||||||
buffer []byte
|
buffer []byte
|
||||||
leftOverSize int32
|
leftOverSize int32
|
||||||
|
maxNumChunk uint32
|
||||||
|
numChunk uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewChunkStreamReader(sizeDecoder ChunkSizeDecoder, reader io.Reader) *ChunkStreamReader {
|
func NewChunkStreamReader(sizeDecoder ChunkSizeDecoder, reader io.Reader) *ChunkStreamReader {
|
||||||
|
return NewChunkStreamReaderWithChunkCount(sizeDecoder, reader, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewChunkStreamReaderWithChunkCount(sizeDecoder ChunkSizeDecoder, reader io.Reader, maxNumChunk uint32) *ChunkStreamReader {
|
||||||
r := &ChunkStreamReader{
|
r := &ChunkStreamReader{
|
||||||
sizeDecoder: sizeDecoder,
|
sizeDecoder: sizeDecoder,
|
||||||
buffer: make([]byte, sizeDecoder.SizeBytes()),
|
buffer: make([]byte, sizeDecoder.SizeBytes()),
|
||||||
|
maxNumChunk: maxNumChunk,
|
||||||
}
|
}
|
||||||
if breader, ok := reader.(*buf.BufferedReader); ok {
|
if breader, ok := reader.(*buf.BufferedReader); ok {
|
||||||
r.reader = breader
|
r.reader = breader
|
||||||
@ -94,6 +101,10 @@ func (r *ChunkStreamReader) readSize() (uint16, error) {
|
|||||||
func (r *ChunkStreamReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
func (r *ChunkStreamReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
||||||
size := r.leftOverSize
|
size := r.leftOverSize
|
||||||
if size == 0 {
|
if size == 0 {
|
||||||
|
r.numChunk++
|
||||||
|
if r.maxNumChunk > 0 && r.numChunk > r.maxNumChunk {
|
||||||
|
return nil, io.EOF
|
||||||
|
}
|
||||||
nextSize, err := r.readSize()
|
nextSize, err := r.readSize()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
Reference in New Issue
Block a user