diff --git a/common/buf/multi_buffer.go b/common/buf/multi_buffer.go index 23e7b884..e03d950b 100644 --- a/common/buf/multi_buffer.go +++ b/common/buf/multi_buffer.go @@ -38,16 +38,13 @@ func ReadAllToBytes(reader io.Reader) ([]byte, error) { // MultiBuffer is a list of Buffers. The order of Buffer matters. type MultiBuffer []*Buffer -// Append appends buffer to the end of this MultiBuffer -func (mb *MultiBuffer) Append(buf *Buffer) { - if buf != nil { - *mb = append(*mb, buf) +// MergeMulti merges content from src to dest, and returns the new address of dest and src +func MergeMulti(dest MultiBuffer, src MultiBuffer) (MultiBuffer, MultiBuffer) { + dest = append(dest, src...) + for idx := range src { + src[idx] = nil } -} - -// AppendMulti appends a MultiBuffer to the end of this one. -func (mb *MultiBuffer) AppendMulti(buf MultiBuffer) { - *mb = append(*mb, buf...) + return dest, src[:0] } // Copy copied the beginning part of the MultiBuffer into the given byte array. @@ -73,7 +70,7 @@ func (mb *MultiBuffer) ReadFrom(reader io.Reader) (int64, error) { if b.IsEmpty() { b.Release() } else { - mb.Append(b) + *mb = append(*mb, b) } totalBytes += int64(b.Len()) if err != nil { @@ -138,7 +135,7 @@ func (mb *MultiBuffer) Write(b []byte) (int, error) { bb := New() nBytes, _ := bb.Write(b) b = b[nBytes:] - mb.Append(bb) + *mb = append(*mb, bb) } return totalBytes, nil @@ -204,7 +201,7 @@ func (mb *MultiBuffer) SliceBySize(size int32) MultiBuffer { break } sliceSize += b.Len() - slice.Append(b) + slice = append(slice, b) (*mb)[i] = nil } *mb = (*mb)[endIndex:] diff --git a/common/buf/multi_buffer_test.go b/common/buf/multi_buffer_test.go index 494a4fb8..2d329328 100644 --- a/common/buf/multi_buffer_test.go +++ b/common/buf/multi_buffer_test.go @@ -33,7 +33,7 @@ func TestMultiBufferAppend(t *testing.T) { var mb MultiBuffer b := New() b.WriteString("ab") - mb.Append(b) + mb = append(mb, b) assert(mb.Len(), Equals, int32(2)) } diff --git a/common/buf/readv_test.go b/common/buf/readv_test.go index 8f8eb2b5..62cb539f 100644 --- a/common/buf/readv_test.go +++ b/common/buf/readv_test.go @@ -51,7 +51,7 @@ func TestReadvReader(t *testing.T) { if err != nil { t.Fatal("unexpected error: ", err) } - rmb.AppendMulti(mb) + rmb, _ = MergeMulti(rmb, mb) if rmb.Len() == size { break } diff --git a/common/crypto/auth.go b/common/crypto/auth.go index 5236827e..a1202b40 100644 --- a/common/crypto/auth.go +++ b/common/crypto/auth.go @@ -176,7 +176,7 @@ func (r *AuthenticationReader) readInternal(soft bool, mb *buf.MultiBuffer) erro if err != nil { return nil } - mb.Append(b) + *mb = append(*mb, b) return nil } @@ -289,7 +289,7 @@ func (w *AuthenticationWriter) writeStream(mb buf.MultiBuffer) error { mb2Write.Release() return err } - mb2Write.Append(eb) + mb2Write = append(mb2Write, eb) if mb.IsEmpty() { break } @@ -313,7 +313,7 @@ func (w *AuthenticationWriter) writePacket(mb buf.MultiBuffer) error { continue } - mb2Write.Append(eb) + mb2Write = append(mb2Write, eb) } if mb2Write.IsEmpty() { diff --git a/common/crypto/auth_test.go b/common/crypto/auth_test.go index 3e4d367a..b53c99f3 100644 --- a/common/crypto/auth_test.go +++ b/common/crypto/auth_test.go @@ -60,7 +60,7 @@ func TestAuthenticationReaderWriter(t *testing.T) { mb2, err := reader.ReadMultiBuffer() assert(err, IsNil) - mb.AppendMulti(mb2) + mb, _ = buf.MergeMulti(mb, mb2) } assert(mb.Len(), Equals, int32(payloadSize)) @@ -97,11 +97,11 @@ func TestAuthenticationReaderWriterPacket(t *testing.T) { var payload buf.MultiBuffer pb1 := buf.New() pb1.Write([]byte("abcd")) - payload.Append(pb1) + payload = append(payload, pb1) pb2 := buf.New() pb2.Write([]byte("efgh")) - payload.Append(pb2) + payload = append(payload, pb2) assert(writer.WriteMultiBuffer(payload), IsNil) assert(cache.Len(), GreaterThan, int32(0)) diff --git a/common/crypto/chunk.go b/common/crypto/chunk.go index 465c21c1..65c5c0f8 100755 --- a/common/crypto/chunk.go +++ b/common/crypto/chunk.go @@ -147,8 +147,8 @@ func (w *ChunkStreamWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { b := buf.New() w.sizeEncoder.Encode(uint16(slice.Len()), b.Extend(w.sizeEncoder.SizeBytes())) - mb2Write.Append(b) - mb2Write.AppendMulti(slice) + mb2Write = append(mb2Write, b) + mb2Write = append(mb2Write, slice...) if mb.IsEmpty() { break diff --git a/common/mux/mux_test.go b/common/mux/mux_test.go index 16dbe7c7..93d073af 100644 --- a/common/mux/mux_test.go +++ b/common/mux/mux_test.go @@ -22,7 +22,7 @@ func readAll(reader buf.Reader) (buf.MultiBuffer, error) { if err != nil { return nil, err } - mb.AppendMulti(b) + mb = append(mb, b...) } return mb, nil } diff --git a/common/mux/writer.go b/common/mux/writer.go index 72536739..cd6daf1f 100644 --- a/common/mux/writer.go +++ b/common/mux/writer.go @@ -71,8 +71,8 @@ func writeMetaWithFrame(writer buf.Writer, meta FrameMetadata, data buf.MultiBuf } mb2 := make(buf.MultiBuffer, 0, len(data)+1) - mb2.Append(frame) - mb2.AppendMulti(data) + mb2 = append(mb2, frame) + mb2 = append(mb2, data...) return writer.WriteMultiBuffer(mb2) } diff --git a/transport/internet/kcp/receiving.go b/transport/internet/kcp/receiving.go index 000cf197..68815140 100644 --- a/transport/internet/kcp/receiving.go +++ b/transport/internet/kcp/receiving.go @@ -196,7 +196,7 @@ func (w *ReceivingWorker) ReadMultiBuffer() buf.MultiBuffer { break } w.nextNumber++ - mb.Append(seg.Detach()) + mb = append(mb, seg.Detach()) seg.Release() } diff --git a/transport/pipe/impl.go b/transport/pipe/impl.go index 1484506a..3f15f900 100644 --- a/transport/pipe/impl.go +++ b/transport/pipe/impl.go @@ -120,7 +120,7 @@ func (p *pipe) writeMultiBufferInternal(mb buf.MultiBuffer) error { return err } - p.data.AppendMulti(mb) + p.data, _ = buf.MergeMulti(p.data, mb) return nil }