1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00

Unify buffer allocation

This commit is contained in:
v2ray 2016-05-25 22:05:47 +02:00
parent a2268d2a7b
commit 81cdaa0e4e

View File

@ -6,19 +6,10 @@ import (
"io" "io"
"github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/alloc"
"github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/common/serial" "github.com/v2ray/v2ray-core/common/serial"
"github.com/v2ray/v2ray-core/transport" "github.com/v2ray/v2ray-core/transport"
) )
// @Private
func AllocBuffer(size int) *alloc.Buffer {
if size < 8*1024-16 {
return alloc.NewBuffer()
}
return alloc.NewLargeBuffer()
}
// @Private // @Private
type Validator struct { type Validator struct {
actualAuth hash.Hash32 actualAuth hash.Hash32
@ -37,7 +28,6 @@ func (this *Validator) Consume(b []byte) {
} }
func (this *Validator) Validate() bool { func (this *Validator) Validate() bool {
log.Debug("VMess Reader: Expected auth ", this.expectedAuth, " actual auth: ", this.actualAuth.Sum32())
return this.actualAuth.Sum32() == this.expectedAuth return this.actualAuth.Sum32() == this.expectedAuth
} }
@ -61,7 +51,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
buffer = this.last buffer = this.last
this.last = nil this.last = nil
} else { } else {
buffer = AllocBuffer(this.chunkLength).Clear() buffer = alloc.NewBufferWithSize(4096).Clear()
} }
if this.chunkLength == -1 { if this.chunkLength == -1 {
@ -72,7 +62,6 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
return nil, err return nil, err
} }
} }
log.Debug("VMess Reader: raw buffer: ", buffer.Value)
length := serial.BytesToUint16(buffer.Value[:2]) length := serial.BytesToUint16(buffer.Value[:2])
this.chunkLength = int(length) - 4 this.chunkLength = int(length) - 4
this.validator = NewValidator(serial.BytesToUint32(buffer.Value[2:6])) this.validator = NewValidator(serial.BytesToUint32(buffer.Value[2:6]))
@ -101,7 +90,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
} }
leftLength := buffer.Len() - this.chunkLength leftLength := buffer.Len() - this.chunkLength
if leftLength > 0 { if leftLength > 0 {
this.last = AllocBuffer(leftLength).Clear() this.last = alloc.NewBufferWithSize(leftLength + 4096).Clear()
this.last.Append(buffer.Value[this.chunkLength:]) this.last.Append(buffer.Value[this.chunkLength:])
buffer.Slice(0, this.chunkLength) buffer.Slice(0, this.chunkLength)
} }