1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -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" "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. // Reader extends io.Reader with alloc.Buffer.
type Reader interface { type Reader interface {
common.Releasable common.Releasable
@ -42,7 +31,12 @@ func NewAdaptiveReader(reader io.Reader) *AdaptiveReader {
// Read implements Reader.Read(). // Read implements Reader.Read().
func (this *AdaptiveReader) Read() (*alloc.Buffer, error) { 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 { if buffer.Len() >= alloc.BufferSize {
this.allocate = alloc.NewLargeBuffer this.allocate = alloc.NewLargeBuffer
@ -50,10 +44,6 @@ func (this *AdaptiveReader) Read() (*alloc.Buffer, error) {
this.allocate = alloc.NewBuffer this.allocate = alloc.NewBuffer
} }
if err != nil {
buffer.Release()
return nil, err
}
return buffer, nil return buffer, nil
} }

View File

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