mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-30 05:56:54 -05:00
Cleanup
This commit is contained in:
parent
089de290ad
commit
7e499ee0b6
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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()
|
||||||
|
Loading…
Reference in New Issue
Block a user