simplify reader/writer interface

pull/120/head
v2ray 9 years ago
parent 308c40553c
commit 70f803173a

@ -20,15 +20,11 @@ func ReadFrom(reader io.Reader, buffer *alloc.Buffer) (*alloc.Buffer, error) {
// Reader extends io.Reader with alloc.Buffer.
type Reader interface {
common.Releasable
// Read reads content from underlying reader, and put it into an alloc.Buffer.
Read() (*alloc.Buffer, error)
}
type ReleasableReader interface {
Reader
common.Releasable
}
// AdaptiveReader is a Reader that adjusts its reading speed automatically.
type AdaptiveReader struct {
reader io.Reader

@ -3,21 +3,17 @@ package io
import (
"io"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common"
"github.com/v2ray/v2ray-core/common/alloc"
)
// Writer extends io.Writer with alloc.Buffer.
type Writer interface {
common.Releasable
// Write writes an alloc.Buffer into underlying writer.
Write(*alloc.Buffer) error
}
type ReleasableWriter interface {
Writer
common.Releasable
}
// AdaptiveWriter is a Writer that writes alloc.Buffer into underlying writer.
type AdaptiveWriter struct {
writer io.Writer

@ -66,6 +66,11 @@ func NewChunkReader(reader io.Reader, auth *Authenticator) *ChunkReader {
}
}
func (this *ChunkReader) Release() {
this.reader = nil
this.auth = nil
}
func (this *ChunkReader) Read() (*alloc.Buffer, error) {
buffer := alloc.NewLargeBuffer()
if _, err := io.ReadFull(this.reader, buffer.Value[:2]); err != nil {

@ -234,6 +234,7 @@ func (this *Shadowsocks) handleConnection(conn *hub.TCPConn) {
v2io.ReaderToChan(ray.InboundInput(), payloadReader)
close(ray.InboundInput())
payloadReader.Release()
writeFinish.Lock()
}

@ -148,7 +148,7 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) {
defer close(input)
defer readFinish.Unlock()
bodyReader := session.DecodeRequestBody(reader)
var requestReader v2io.ReleasableReader
var requestReader v2io.Reader
if request.Option.IsChunkStream() {
requestReader = vmessio.NewAuthChunkReader(bodyReader)
} else {
@ -179,12 +179,12 @@ func (this *VMessInboundHandler) HandleConnection(connection *hub.TCPConn) {
writer.SetCached(false)
go func(finish *sync.Mutex) {
var writer v2io.ReleasableWriter = v2io.NewAdaptiveWriter(bodyWriter)
var writer v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
if request.Option.IsChunkStream() {
writer = vmessio.NewAuthChunkWriter(writer)
}
v2io.ChanToWriter(writer, output)
writer.Release()
writer.Release()
finish.Unlock()
}(&writeFinish)
writeFinish.Lock()

@ -9,10 +9,10 @@ import (
)
type AuthChunkWriter struct {
writer v2io.ReleasableWriter
writer v2io.Writer
}
func NewAuthChunkWriter(writer v2io.ReleasableWriter) *AuthChunkWriter {
func NewAuthChunkWriter(writer v2io.Writer) *AuthChunkWriter {
return &AuthChunkWriter{
writer: writer,
}
@ -25,7 +25,7 @@ func (this *AuthChunkWriter) Write(buffer *alloc.Buffer) error {
func (this *AuthChunkWriter) Release() {
this.writer.Release()
this.writer = nil
this.writer = nil
}
func Authenticate(buffer *alloc.Buffer) {

@ -116,12 +116,12 @@ func (this *VMessOutboundHandler) handleRequest(session *raw.ClientSession, conn
writer.SetCached(false)
if moreChunks {
var streamWriter v2io.ReleasableWriter = v2io.NewAdaptiveWriter(bodyWriter)
var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
if request.Option.IsChunkStream() {
streamWriter = vmessio.NewAuthChunkWriter(streamWriter)
}
v2io.ChanToWriter(streamWriter, input)
streamWriter.Release()
streamWriter.Release()
}
return
}
@ -150,6 +150,7 @@ func (this *VMessOutboundHandler) handleResponse(session *raw.ClientSession, con
}
v2io.ReaderToChan(output, bodyReader)
bodyReader.Release()
return
}

Loading…
Cancel
Save