mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 01:57:12 -05:00
remove ToNetBuffers
This commit is contained in:
parent
950612544b
commit
2594f7027a
@ -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)
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
@ -32,44 +32,35 @@ 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.
|
||||||
func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
|
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
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user