diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index 9225b374..8014d55e 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -206,17 +206,17 @@ func NewBuffer() *Buffer { } // NewLargeBuffer creates a Buffer with 64K bytes of arbitrary content. -func NewLargeBuffer() *Buffer { - return largePool.Allocate() -} +//func NewLargeBuffer() *Buffer { +// return largePool.Allocate() +//} -func NewBufferWithSize(size int) *Buffer { - if size <= BufferSize { - return NewBuffer() - } - - return NewLargeBuffer() -} +//func NewBufferWithSize(size int) *Buffer { +// if size <= BufferSize { +// return NewBuffer() +// } +// +// return NewLargeBuffer() +//} func NewLocalBuffer(size int) *Buffer { return CreateBuffer(make([]byte, size), nil) diff --git a/common/alloc/buffer_pool.go b/common/alloc/buffer_pool.go index c17b37a5..74d24dcd 100644 --- a/common/alloc/buffer_pool.go +++ b/common/alloc/buffer_pool.go @@ -63,7 +63,7 @@ const ( var ( mediumPool *BufferPool - largePool *BufferPool + //largePool *BufferPool ) func init() { @@ -76,6 +76,6 @@ func init() { } } totalByteSize := size * 1024 * 1024 - mediumPool = NewBufferPool(mediumBufferByteSize, totalByteSize/4*3/mediumBufferByteSize) - largePool = NewBufferPool(largeBufferByteSize, totalByteSize/4/largeBufferByteSize) + mediumPool = NewBufferPool(mediumBufferByteSize, totalByteSize/mediumBufferByteSize) + //largePool = NewBufferPool(largeBufferByteSize, totalByteSize/4/largeBufferByteSize) } diff --git a/common/io/buffered_reader_test.go b/common/io/buffered_reader_test.go index 71375b73..d2ee2bff 100644 --- a/common/io/buffered_reader_test.go +++ b/common/io/buffered_reader_test.go @@ -11,7 +11,7 @@ import ( func TestBufferedReader(t *testing.T) { assert := assert.On(t) - content := alloc.NewLargeBuffer() + content := alloc.NewBuffer() len := content.Len() reader := NewBufferedReader(content) @@ -31,13 +31,4 @@ func TestBufferedReader(t *testing.T) { assert.Error(err).IsNil() assert.Int(content.Len()).Equals(len2) - reader.SetCached(false) - - payload2 := alloc.NewBuffer() - reader.Read(payload2.Value) - - assert.Int(content.Len()).Equals(len2) - - reader.Read(payload2.Value) - assert.Int(content.Len()).LessThan(len2) } diff --git a/common/io/buffered_writer_test.go b/common/io/buffered_writer_test.go index c92e210f..bf999f3c 100644 --- a/common/io/buffered_writer_test.go +++ b/common/io/buffered_writer_test.go @@ -11,7 +11,7 @@ import ( func TestBufferedWriter(t *testing.T) { assert := assert.On(t) - content := alloc.NewLargeBuffer().Clear() + content := alloc.NewBuffer().Clear() writer := NewBufferedWriter(content) assert.Bool(writer.Cached()).IsTrue() diff --git a/common/io/chain_writer.go b/common/io/chain_writer.go index 3c51b10f..643d1f67 100644 --- a/common/io/chain_writer.go +++ b/common/io/chain_writer.go @@ -19,24 +19,28 @@ func NewChainWriter(writer Writer) *ChainWriter { } func (this *ChainWriter) Write(payload []byte) (int, error) { - if this.writer == nil { - return 0, io.ErrClosedPipe - } - - size := len(payload) - buffer := alloc.NewBufferWithSize(size).Clear() - buffer.Append(payload) - this.Lock() defer this.Unlock() if this.writer == nil { return 0, io.ErrClosedPipe } - err := this.writer.Write(buffer) - if err != nil { - return 0, err + size := len(payload) + for size > 0 { + buffer := alloc.NewBuffer().Clear() + if size > alloc.BufferSize { + buffer.Append(payload[:alloc.BufferSize]) + size -= alloc.BufferSize + } else { + buffer.Append(payload) + size = 0 + } + err := this.writer.Write(buffer) + if err != nil { + return 0, err + } } + return size, nil } diff --git a/common/io/reader.go b/common/io/reader.go index 1c2dc770..a2d2dbb8 100644 --- a/common/io/reader.go +++ b/common/io/reader.go @@ -38,11 +38,11 @@ func (this *AdaptiveReader) Read() (*alloc.Buffer, error) { return nil, err } - if buffer.Len() >= alloc.BufferSize { - this.allocate = alloc.NewLargeBuffer - } else { - this.allocate = alloc.NewBuffer - } + //if buffer.Len() >= alloc.BufferSize { + // this.allocate = alloc.NewLargeBuffer + //} else { + // this.allocate = alloc.NewBuffer + //} return buffer, nil } diff --git a/common/io/reader_test.go b/common/io/reader_test.go index a687e694..44b3cccb 100644 --- a/common/io/reader_test.go +++ b/common/io/reader_test.go @@ -19,9 +19,4 @@ func TestAdaptiveReader(t *testing.T) { assert.Error(err).IsNil() assert.Bool(b1.IsFull()).IsTrue() assert.Int(b1.Len()).Equals(alloc.BufferSize) - - b2, err := reader.Read() - assert.Error(err).IsNil() - assert.Bool(b2.IsFull()).IsTrue() - assert.Int(b2.Len()).Equals(alloc.LargeBufferSize) } diff --git a/common/io/writer_test.go b/common/io/writer_test.go index ca3f8f1f..cd1a44e7 100644 --- a/common/io/writer_test.go +++ b/common/io/writer_test.go @@ -13,7 +13,7 @@ import ( func TestAdaptiveWriter(t *testing.T) { assert := assert.On(t) - lb := alloc.NewLargeBuffer() + lb := alloc.NewBuffer() rand.Read(lb.Value) writeBuffer := make([]byte, 0, 1024*1024) diff --git a/main/main.go b/main/main.go index af73d6f2..f13d2255 100644 --- a/main/main.go +++ b/main/main.go @@ -12,6 +12,8 @@ import ( "v2ray.com/core" "v2ray.com/core/common/log" + //"net/http" + //_ "net/http/pprof" ) var ( @@ -94,6 +96,10 @@ func main() { return } + //go func() { + // http.ListenAndServe(":6060", nil) + //}() + if point := startV2Ray(); point != nil { osSignals := make(chan os.Signal, 1) signal.Notify(osSignals, os.Interrupt, os.Kill, syscall.SIGTERM) diff --git a/proxy/shadowsocks/ota.go b/proxy/shadowsocks/ota.go index 86451015..cc1ae26b 100644 --- a/proxy/shadowsocks/ota.go +++ b/proxy/shadowsocks/ota.go @@ -73,7 +73,7 @@ func (this *ChunkReader) Release() { } func (this *ChunkReader) Read() (*alloc.Buffer, error) { - buffer := alloc.NewLargeBuffer() + buffer := alloc.NewBuffer() if _, err := io.ReadFull(this.reader, buffer.Value[:2]); err != nil { buffer.Release() return nil, err diff --git a/proxy/shadowsocks/protocol_test.go b/proxy/shadowsocks/protocol_test.go index 0508540f..cb461446 100644 --- a/proxy/shadowsocks/protocol_test.go +++ b/proxy/shadowsocks/protocol_test.go @@ -59,7 +59,7 @@ func TestTCPRequest(t *testing.T) { } data := alloc.NewLocalBuffer(256).Clear().AppendString("test string") - cache := alloc.NewLargeBuffer().Clear() + cache := alloc.NewBuffer().Clear() writer, err := WriteTCPRequest(request, cache) assert.Error(err).IsNil() diff --git a/proxy/vmess/io/io_test.go b/proxy/vmess/io/io_test.go index f8bb265c..ae3e83ee 100644 --- a/proxy/vmess/io/io_test.go +++ b/proxy/vmess/io/io_test.go @@ -60,36 +60,6 @@ func TestLargeIO(t *testing.T) { if writeSize == len(content) { break } - - chunkSize = 8 * 1024 - if chunkSize+writeSize > len(content) { - chunkSize = len(content) - writeSize - } - writer.Write(alloc.NewLargeBuffer().Clear().Append(content[writeSize : writeSize+chunkSize])) - writeSize += chunkSize - if writeSize == len(content) { - break - } - - chunkSize = 63 * 1024 - if chunkSize+writeSize > len(content) { - chunkSize = len(content) - writeSize - } - writer.Write(alloc.NewLargeBuffer().Clear().Append(content[writeSize : writeSize+chunkSize])) - writeSize += chunkSize - if writeSize == len(content) { - break - } - - chunkSize = 64*1024 - 16 - if chunkSize+writeSize > len(content) { - chunkSize = len(content) - writeSize - } - writer.Write(alloc.NewLargeBuffer().Clear().Append(content[writeSize : writeSize+chunkSize])) - writeSize += chunkSize - if writeSize == len(content) { - break - } } writer.Write(alloc.NewBuffer().Clear()) writer.Release() diff --git a/proxy/vmess/io/reader.go b/proxy/vmess/io/reader.go index 25db03ec..6b5bd9c2 100644 --- a/proxy/vmess/io/reader.go +++ b/proxy/vmess/io/reader.go @@ -51,7 +51,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) { buffer = this.last this.last = nil } else { - buffer = alloc.NewBufferWithSize(4096).Clear() + buffer = alloc.NewBuffer().Clear() } if this.chunkLength == -1 { @@ -97,7 +97,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) { } leftLength := buffer.Len() - this.chunkLength if leftLength > 0 { - this.last = alloc.NewBufferWithSize(leftLength + 4096).Clear() + this.last = alloc.NewBuffer().Clear() this.last.Append(buffer.Value[this.chunkLength:]) buffer.Slice(0, this.chunkLength) }