1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 01:27:03 -05:00
This commit is contained in:
v2ray 2016-07-17 12:18:34 +02:00
parent 089de290ad
commit 7e499ee0b6
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 9 additions and 20 deletions

View File

@ -7,17 +7,6 @@ import (
"github.com/v2ray/v2ray-core/common/alloc"
)
// ReadFrom reads from a reader and put all content to a buffer.
// If buffer is nil, ReadFrom creates a new normal buffer.
func ReadFrom(reader io.Reader, buffer *alloc.Buffer) (*alloc.Buffer, error) {
if buffer == nil {
buffer = alloc.NewBuffer()
}
nBytes, err := reader.Read(buffer.Value)
buffer.Slice(0, nBytes)
return buffer, err
}
// Reader extends io.Reader with alloc.Buffer.
type Reader interface {
common.Releasable
@ -42,7 +31,12 @@ func NewAdaptiveReader(reader io.Reader) *AdaptiveReader {
// Read implements Reader.Read().
func (this *AdaptiveReader) Read() (*alloc.Buffer, error) {
buffer, err := ReadFrom(this.reader, this.allocate())
buffer := this.allocate().Clear()
_, err := buffer.FillFrom(this.reader)
if err != nil {
buffer.Release()
return nil, err
}
if buffer.Len() >= alloc.BufferSize {
this.allocate = alloc.NewLargeBuffer
@ -50,10 +44,6 @@ func (this *AdaptiveReader) Read() (*alloc.Buffer, error) {
this.allocate = alloc.NewBuffer
}
if err != nil {
buffer.Release()
return nil, err
}
return buffer, nil
}

View File

@ -4,7 +4,6 @@ import (
"fmt"
"net"
v2io "github.com/v2ray/v2ray-core/common/io"
v2net "github.com/v2ray/v2ray-core/common/net"
)
@ -44,13 +43,13 @@ func (server *Server) acceptConnections(listener *net.TCPListener) {
}
func (server *Server) handleConnection(conn net.Conn) {
request := make([]byte, 4096)
for true {
request, err := v2io.ReadFrom(conn, nil)
nBytes, err := conn.Read(request)
if err != nil {
break
}
response := server.MsgProcessor(request.Value)
request.Release()
response := server.MsgProcessor(request[:nBytes])
conn.Write(response)
}
conn.Close()