1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-30 21:26:33 -05:00

remove static bytes array

This commit is contained in:
Darien Raymond 2018-04-02 22:12:51 +02:00
parent c5bd23105e
commit 009d58dd6c
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -5,6 +5,8 @@ import (
"crypto/rand" "crypto/rand"
"io" "io"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/transport/internet" "v2ray.com/core/transport/internet"
) )
@ -50,8 +52,6 @@ type KCPPacketWriter struct {
Header internet.PacketHeader Header internet.PacketHeader
Security cipher.AEAD Security cipher.AEAD
Writer io.Writer Writer io.Writer
buffer [2048]byte
} }
func (w *KCPPacketWriter) Overhead() int { func (w *KCPPacketWriter) Overhead() int {
@ -66,27 +66,28 @@ func (w *KCPPacketWriter) Overhead() int {
} }
func (w *KCPPacketWriter) Write(b []byte) (int, error) { func (w *KCPPacketWriter) Write(b []byte) (int, error) {
x := w.buffer[:] bb := buf.NewSize(int32(len(b) + w.Overhead()))
size := 0 defer bb.Release()
if w.Header != nil { if w.Header != nil {
nBytes, _ := w.Header.Write(x) common.Must(bb.AppendSupplier(func(x []byte) (int, error) {
size += nBytes return w.Header.Write(x)
x = x[nBytes:] }))
} }
if w.Security != nil { if w.Security != nil {
nonceSize := w.Security.NonceSize() nonceSize := w.Security.NonceSize()
var nonce []byte common.Must(bb.AppendSupplier(func(x []byte) (int, error) {
if nonceSize > 0 { return rand.Read(x[:nonceSize])
nonce = x[:nonceSize] }))
rand.Read(nonce) nonce := bb.BytesFrom(int32(-nonceSize))
x = x[nonceSize:] common.Must(bb.AppendSupplier(func(x []byte) (int, error) {
} eb := w.Security.Seal(x[:0], nonce, b, nil)
x = w.Security.Seal(x[:0], nonce, b, nil) return len(eb), nil
size += nonceSize + len(x) }))
} else { } else {
size += copy(x, b) bb.Append(b)
} }
_, err := w.Writer.Write(w.buffer[:size]) _, err := w.Writer.Write(bb.Bytes())
return len(b), err return len(b), err
} }