pull/692/head^2
Darien Raymond 7 years ago
parent 8e5c7a89bb
commit c3fc27f7a1

@ -7,6 +7,7 @@ import (
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
) )
// ReadMetadata reads FrameMetadata from the given reader.
func ReadMetadata(reader io.Reader) (*FrameMetadata, error) { func ReadMetadata(reader io.Reader) (*FrameMetadata, error) {
metaLen, err := serial.ReadUint16(reader) metaLen, err := serial.ReadUint16(reader)
if err != nil { if err != nil {
@ -25,11 +26,13 @@ func ReadMetadata(reader io.Reader) (*FrameMetadata, error) {
return ReadFrameFrom(b.Bytes()) return ReadFrameFrom(b.Bytes())
} }
// PacketReader is an io.Reader that reads whole chunk of Mux frames every time.
type PacketReader struct { type PacketReader struct {
reader io.Reader reader io.Reader
eof bool eof bool
} }
// NewPacketReader creates a new PacketReader.
func NewPacketReader(reader io.Reader) *PacketReader { func NewPacketReader(reader io.Reader) *PacketReader {
return &PacketReader{ return &PacketReader{
reader: reader, reader: reader,
@ -37,6 +40,7 @@ func NewPacketReader(reader io.Reader) *PacketReader {
} }
} }
// Read implements buf.Reader.
func (r *PacketReader) Read() (buf.MultiBuffer, error) { func (r *PacketReader) Read() (buf.MultiBuffer, error) {
if r.eof { if r.eof {
return nil, io.EOF return nil, io.EOF
@ -61,11 +65,13 @@ func (r *PacketReader) Read() (buf.MultiBuffer, error) {
return buf.NewMultiBufferValue(b), nil return buf.NewMultiBufferValue(b), nil
} }
// StreamReader reads Mux frame as a stream.
type StreamReader struct { type StreamReader struct {
reader io.Reader reader io.Reader
leftOver int leftOver int
} }
// NewStreamReader creates a new StreamReader.
func NewStreamReader(reader io.Reader) *StreamReader { func NewStreamReader(reader io.Reader) *StreamReader {
return &StreamReader{ return &StreamReader{
reader: reader, reader: reader,
@ -73,6 +79,7 @@ func NewStreamReader(reader io.Reader) *StreamReader {
} }
} }
// Read implmenets buf.Reader.
func (r *StreamReader) Read() (buf.MultiBuffer, error) { func (r *StreamReader) Read() (buf.MultiBuffer, error) {
if r.leftOver == 0 { if r.leftOver == 0 {
r.leftOver = -1 r.leftOver = -1

@ -122,6 +122,7 @@ func (m *SessionManager) Close() {
m.sessions = nil m.sessions = nil
} }
// Session represents a client connection in a Mux connection.
type Session struct { type Session struct {
input ray.InputStream input ray.InputStream
output ray.OutputStream output ray.OutputStream
@ -137,6 +138,7 @@ func (s *Session) Close() {
s.parent.Remove(s.ID) s.parent.Remove(s.ID)
} }
// NewReader creates a buf.Reader based on the transfer type of this Session.
func (s *Session) NewReader(reader io.Reader) buf.Reader { func (s *Session) NewReader(reader io.Reader) buf.Reader {
if s.transferType == protocol.TransferTypeStream { if s.transferType == protocol.TransferTypeStream {
return NewStreamReader(reader) return NewStreamReader(reader)

Loading…
Cancel
Save