1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00
This commit is contained in:
Darien Raymond 2017-10-30 12:36:31 +01:00
parent 8e5c7a89bb
commit c3fc27f7a1
2 changed files with 9 additions and 0 deletions

View File

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

View File

@ -122,6 +122,7 @@ func (m *SessionManager) Close() {
m.sessions = nil
}
// Session represents a client connection in a Mux connection.
type Session struct {
input ray.InputStream
output ray.OutputStream
@ -137,6 +138,7 @@ func (s *Session) Close() {
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 {
if s.transferType == protocol.TransferTypeStream {
return NewStreamReader(reader)