Browse Source

refactor MultiBuffer append

pull/1435/head
Darien Raymond 6 years ago
parent
commit
27772a75a7
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 21
      common/buf/multi_buffer.go
  2. 2
      common/buf/multi_buffer_test.go
  3. 2
      common/buf/readv_test.go
  4. 6
      common/crypto/auth.go
  5. 6
      common/crypto/auth_test.go
  6. 4
      common/crypto/chunk.go
  7. 2
      common/mux/mux_test.go
  8. 4
      common/mux/writer.go
  9. 2
      transport/internet/kcp/receiving.go
  10. 2
      transport/pipe/impl.go

21
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:]

2
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))
}

2
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
}

6
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() {

6
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))

4
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

2
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
}

4
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)
}

2
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()
}

2
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
}

Loading…
Cancel
Save