1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 10:08:15 -05:00

remove ToNetBuffers

This commit is contained in:
Darien Raymond 2018-08-17 11:51:59 +02:00
parent 950612544b
commit 2594f7027a
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 25 additions and 35 deletions

View File

@ -2,7 +2,6 @@ package buf
import ( import (
"io" "io"
"net"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
@ -204,15 +203,6 @@ func (mb MultiBuffer) String() string {
return serial.Concat(v...) return serial.Concat(v...)
} }
// ToNetBuffers converts this MultiBuffer to net.Buffers. The return net.Buffers points to the same content of the MultiBuffer.
func (mb MultiBuffer) ToNetBuffers() net.Buffers {
bs := make([][]byte, len(mb))
for i, b := range mb {
bs[i] = b.Bytes()
}
return bs
}
// SliceBySize splits the beginning of this MultiBuffer into another one, for at most size bytes. // SliceBySize splits the beginning of this MultiBuffer into another one, for at most size bytes.
func (mb *MultiBuffer) SliceBySize(size int32) MultiBuffer { func (mb *MultiBuffer) SliceBySize(size int32) MultiBuffer {
slice := NewMultiBufferCap(10) slice := NewMultiBufferCap(10)

View File

@ -2,6 +2,7 @@ package buf
import ( import (
"io" "io"
"net"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
@ -10,6 +11,8 @@ import (
// BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer. // BufferToBytesWriter is a Writer that writes alloc.Buffer into underlying writer.
type BufferToBytesWriter struct { type BufferToBytesWriter struct {
io.Writer io.Writer
cache [][]byte
} }
// WriteMultiBuffer implements Writer. This method takes ownership of the given buffer. // WriteMultiBuffer implements Writer. This method takes ownership of the given buffer.
@ -25,10 +28,16 @@ func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error {
return WriteAllBytes(w.Writer, mb[0].Bytes()) return WriteAllBytes(w.Writer, mb[0].Bytes())
} }
bs := mb.ToNetBuffers() bs := w.cache
for _, b := range mb {
bs = append(bs, b.Bytes())
}
w.cache = bs[:0]
nb := net.Buffers(bs)
for size > 0 { for size > 0 {
n, err := bs.WriteTo(w.Writer) n, err := nb.WriteTo(w.Writer)
if err != nil { if err != nil {
return err return err
} }

View File

@ -34,6 +34,7 @@ var (
type CryptionWriter struct { type CryptionWriter struct {
stream cipher.Stream stream cipher.Stream
writer io.Writer writer io.Writer
bufWriter buf.Writer
} }
// NewCryptionWriter creates a new CryptionWriter. // NewCryptionWriter creates a new CryptionWriter.
@ -41,35 +42,25 @@ func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
return &CryptionWriter{ return &CryptionWriter{
stream: stream, stream: stream,
writer: writer, writer: writer,
bufWriter: buf.NewWriter(writer),
} }
} }
// Write implements io.Writer.Write(). // Write implements io.Writer.Write().
func (w *CryptionWriter) Write(data []byte) (int, error) { func (w *CryptionWriter) Write(data []byte) (int, error) {
w.stream.XORKeyStream(data, data) w.stream.XORKeyStream(data, data)
return w.writer.Write(data)
if err := buf.WriteAllBytes(w.writer, data); err != nil {
return 0, err
}
return len(data), nil
} }
// WriteMultiBuffer implements buf.Writer. // WriteMultiBuffer implements buf.Writer.
func (w *CryptionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { func (w *CryptionWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {
defer mb.Release() for _, b := range mb {
w.stream.XORKeyStream(b.Bytes(), b.Bytes())
size := mb.Len()
if size == 0 {
return nil
} }
bs := mb.ToNetBuffers() return w.bufWriter.WriteMultiBuffer(mb)
for _, b := range bs {
w.stream.XORKeyStream(b, b)
}
for size > 0 {
n, err := bs.WriteTo(w.writer)
if err != nil {
return err
}
size -= int32(n)
}
return nil
} }