1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00

remove use of prepend

This commit is contained in:
Darien Raymond 2016-12-08 11:21:24 +01:00
parent 12bce36693
commit 0ad629ca31
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 13 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package testing package testing
import ( import (
"v2ray.com/core/common/alloc"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
"v2ray.com/core/transport/ray" "v2ray.com/core/transport/ray"
@ -19,8 +20,11 @@ func NewTestPacketDispatcher(handler func(destination v2net.Destination, traffic
if err != nil { if err != nil {
break break
} }
payload.Prepend([]byte("Processed: ")) output := alloc.NewBuffer()
traffic.OutboundOutput().Write(payload) output.Append([]byte("Processed: "))
output.Append(payload.Bytes())
payload.Release()
traffic.OutboundOutput().Write(output)
} }
traffic.OutboundOutput().Close() traffic.OutboundOutput().Close()
} }

View File

@ -5,6 +5,7 @@ import (
"crypto/hmac" "crypto/hmac"
"crypto/sha1" "crypto/sha1"
"io" "io"
"v2ray.com/core/common/alloc" "v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
@ -111,12 +112,14 @@ func (v *ChunkReader) Read() (*alloc.Buffer, error) {
type ChunkWriter struct { type ChunkWriter struct {
writer io.Writer writer io.Writer
auth *Authenticator auth *Authenticator
buffer []byte
} }
func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter { func NewChunkWriter(writer io.Writer, auth *Authenticator) *ChunkWriter {
return &ChunkWriter{ return &ChunkWriter{
writer: writer, writer: writer,
auth: auth, auth: auth,
buffer: make([]byte, 32*1024),
} }
} }
@ -127,8 +130,9 @@ func (v *ChunkWriter) Release() {
func (v *ChunkWriter) Write(payload *alloc.Buffer) error { func (v *ChunkWriter) Write(payload *alloc.Buffer) error {
totalLength := payload.Len() totalLength := payload.Len()
payload.PrependFunc(AuthSize, v.auth.Authenticate(payload.Bytes())) serial.Uint16ToBytes(uint16(totalLength), v.buffer[:0])
payload.PrependFunc(2, serial.WriteUint16(uint16(totalLength))) v.auth.Authenticate(payload.Bytes())(v.buffer[2:])
_, err := v.writer.Write(payload.Bytes()) copy(v.buffer[2+AuthSize:], payload.Bytes())
_, err := v.writer.Write(v.buffer[:2+AuthSize+payload.Len()])
return err return err
} }

View File

@ -19,9 +19,6 @@ func TestNormalChunkReading(t *testing.T) {
payload, err := reader.Read() payload, err := reader.Read()
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Bytes(payload.Bytes()).Equals([]byte{11, 12, 13, 14, 15, 16, 17, 18}) assert.Bytes(payload.Bytes()).Equals([]byte{11, 12, 13, 14, 15, 16, 17, 18})
payload.PrependBytes(3, 4)
assert.Bytes(payload.Bytes()).Equals([]byte{3, 4, 11, 12, 13, 14, 15, 16, 17, 18})
} }
func TestNormalChunkWriting(t *testing.T) { func TestNormalChunkWriting(t *testing.T) {