From a01fdc29a616139acd81b0833691493c90e16e38 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 16 Nov 2018 11:08:12 +0100 Subject: [PATCH] remove NewMultiBufferValue --- app/proxyman/inbound/worker.go | 2 +- common/buf/multi_buffer.go | 7 +------ common/buf/multi_buffer_test.go | 2 +- common/buf/reader_test.go | 4 ++-- common/buf/readv_reader.go | 2 +- common/buf/writer.go | 2 +- common/buf/writer_test.go | 2 +- common/crypto/auth.go | 2 +- common/crypto/chunk_test.go | 4 ++-- common/mux/mux_test.go | 2 +- common/mux/reader.go | 2 +- common/mux/writer.go | 6 +++--- proxy/blackhole/config.go | 2 +- proxy/shadowsocks/ota_test.go | 2 +- proxy/shadowsocks/protocol.go | 4 ++-- proxy/shadowsocks/protocol_test.go | 6 +++--- proxy/socks/protocol.go | 2 +- proxy/socks/protocol_test.go | 2 +- testing/servers/tcp/tcp.go | 2 +- transport/internet/udp/dispatcher.go | 2 +- transport/pipe/pipe_test.go | 12 ++++++------ 21 files changed, 33 insertions(+), 38 deletions(-) diff --git a/app/proxyman/inbound/worker.go b/app/proxyman/inbound/worker.go index 772a68d9..d1c4f8db 100644 --- a/app/proxyman/inbound/worker.go +++ b/app/proxyman/inbound/worker.go @@ -275,7 +275,7 @@ func (w *udpWorker) callback(b *buf.Buffer, source net.Destination, originalDest conn, existing := w.getConnection(id) // payload will be discarded in pipe is full. - conn.writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)) // nolint: errcheck + conn.writer.WriteMultiBuffer(buf.MultiBuffer{b}) // nolint: errcheck if !existing { common.Must(w.checker.Start()) diff --git a/common/buf/multi_buffer.go b/common/buf/multi_buffer.go index 3f48cc5d..50d642fc 100644 --- a/common/buf/multi_buffer.go +++ b/common/buf/multi_buffer.go @@ -43,11 +43,6 @@ func NewMultiBufferCap(capacity int32) MultiBuffer { return MultiBuffer(make([]*Buffer, 0, capacity)) } -// NewMultiBufferValue wraps a list of Buffers into MultiBuffer. -func NewMultiBufferValue(b ...*Buffer) MultiBuffer { - return MultiBuffer(b) -} - // Append appends buffer to the end of this MultiBuffer func (mb *MultiBuffer) Append(buf *Buffer) { if buf != nil { @@ -221,7 +216,7 @@ func (mb *MultiBuffer) SliceBySize(size int32) MultiBuffer { if endIndex == 0 && len(*mb) > 0 { b := New() common.Must2(b.ReadFullFrom((*mb)[0], size)) - return NewMultiBufferValue(b) + return MultiBuffer{b} } return slice } diff --git a/common/buf/multi_buffer_test.go b/common/buf/multi_buffer_test.go index 0ec46ef1..494a4fb8 100644 --- a/common/buf/multi_buffer_test.go +++ b/common/buf/multi_buffer_test.go @@ -18,7 +18,7 @@ func TestMultiBufferRead(t *testing.T) { b2 := New() b2.WriteString("cd") - mb := NewMultiBufferValue(b1, b2) + mb := MultiBuffer{b1, b2} bs := make([]byte, 32) nBytes, err := mb.Read(bs) diff --git a/common/buf/reader_test.go b/common/buf/reader_test.go index 272ab5a9..dddc87d6 100644 --- a/common/buf/reader_test.go +++ b/common/buf/reader_test.go @@ -20,7 +20,7 @@ func TestBytesReaderWriteTo(t *testing.T) { b1.WriteString("abc") b2 := New() b2.WriteString("efg") - assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil) + assert(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2}), IsNil) pWriter.Close() pReader2, pWriter2 := pipe.New(pipe.WithSizeLimit(1024)) @@ -47,7 +47,7 @@ func TestBytesReaderMultiBuffer(t *testing.T) { b1.WriteString("abc") b2 := New() b2.WriteString("efg") - assert(pWriter.WriteMultiBuffer(NewMultiBufferValue(b1, b2)), IsNil) + assert(pWriter.WriteMultiBuffer(MultiBuffer{b1, b2}), IsNil) pWriter.Close() mbReader := NewReader(reader) diff --git a/common/buf/readv_reader.go b/common/buf/readv_reader.go index cca1a7c3..f127ccde 100644 --- a/common/buf/readv_reader.go +++ b/common/buf/readv_reader.go @@ -126,7 +126,7 @@ func (r *ReadVReader) ReadMultiBuffer() (MultiBuffer, error) { if b.IsFull() { r.alloc.Adjust(1) } - return NewMultiBufferValue(b), nil + return MultiBuffer{b}, nil } mb, err := r.readMulti() diff --git a/common/buf/writer.go b/common/buf/writer.go index 650519d8..7caabd09 100644 --- a/common/buf/writer.go +++ b/common/buf/writer.go @@ -175,7 +175,7 @@ func (w *BufferedWriter) flushInternal() error { return err } - return w.writer.WriteMultiBuffer(NewMultiBufferValue(b)) + return w.writer.WriteMultiBuffer(MultiBuffer{b}) } // SetBuffered sets whether the internal buffer is used. If set to false, Flush() will be called to clear the buffer. diff --git a/common/buf/writer_test.go b/common/buf/writer_test.go index 51cc2dfe..15e9b2cb 100644 --- a/common/buf/writer_test.go +++ b/common/buf/writer_test.go @@ -25,7 +25,7 @@ func TestWriter(t *testing.T) { writer := NewBufferedWriter(NewWriter(writeBuffer)) writer.SetBuffered(false) - err := writer.WriteMultiBuffer(NewMultiBufferValue(lb)) + err := writer.WriteMultiBuffer(MultiBuffer{lb}) assert(err, IsNil) assert(writer.Flush(), IsNil) assert(expectedBytes, Equals, writeBuffer.Bytes()) diff --git a/common/crypto/auth.go b/common/crypto/auth.go index 91f739bc..f23c770a 100644 --- a/common/crypto/auth.go +++ b/common/crypto/auth.go @@ -328,7 +328,7 @@ func (w *AuthenticationWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { if mb.IsEmpty() { eb, err := w.seal([]byte{}) common.Must(err) - return w.writer.WriteMultiBuffer(buf.NewMultiBufferValue(eb)) + return w.writer.WriteMultiBuffer(buf.MultiBuffer{eb}) } if w.transferType == protocol.TransferTypeStream { diff --git a/common/crypto/chunk_test.go b/common/crypto/chunk_test.go index 90409fc3..13d0a27c 100644 --- a/common/crypto/chunk_test.go +++ b/common/crypto/chunk_test.go @@ -21,11 +21,11 @@ func TestChunkStreamIO(t *testing.T) { b := buf.New() b.WriteString("abcd") - common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))) + common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b})) b = buf.New() b.WriteString("efg") - common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))) + common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b})) common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{})) diff --git a/common/mux/mux_test.go b/common/mux/mux_test.go index 43918908..16dbe7c7 100644 --- a/common/mux/mux_test.go +++ b/common/mux/mux_test.go @@ -44,7 +44,7 @@ func TestReaderWriter(t *testing.T) { writePayload := func(writer *Writer, payload ...byte) error { b := buf.New() b.Write(payload) - return writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)) + return writer.WriteMultiBuffer(buf.MultiBuffer{b}) } assert(writePayload(writer, 'a', 'b', 'c', 'd'), IsNil) diff --git a/common/mux/reader.go b/common/mux/reader.go index d9ace3db..47835162 100644 --- a/common/mux/reader.go +++ b/common/mux/reader.go @@ -43,7 +43,7 @@ func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) { return nil, err } r.eof = true - return buf.NewMultiBufferValue(b), nil + return buf.MultiBuffer{b}, nil } // NewStreamReader creates a new StreamReader. diff --git a/common/mux/writer.go b/common/mux/writer.go index 482d3362..58643931 100644 --- a/common/mux/writer.go +++ b/common/mux/writer.go @@ -58,7 +58,7 @@ func (w *Writer) writeMetaOnly() error { if err := meta.WriteTo(b); err != nil { return err } - return w.writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)) + return w.writer.WriteMultiBuffer(buf.MultiBuffer{b}) } func writeMetaWithFrame(writer buf.Writer, meta FrameMetadata, data buf.MultiBuffer) error { @@ -96,7 +96,7 @@ func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error { if w.transferType == protocol.TransferTypeStream { chunk = mb.SliceBySize(8 * 1024) } else { - chunk = buf.NewMultiBufferValue(mb.SplitFirst()) + chunk = buf.MultiBuffer{mb.SplitFirst()} } if err := w.writeData(chunk); err != nil { return err @@ -119,6 +119,6 @@ func (w *Writer) Close() error { frame := buf.New() common.Must(meta.WriteTo(frame)) - w.writer.WriteMultiBuffer(buf.NewMultiBufferValue(frame)) // nolint: errcheck + w.writer.WriteMultiBuffer(buf.MultiBuffer{frame}) // nolint: errcheck return nil } diff --git a/proxy/blackhole/config.go b/proxy/blackhole/config.go index 6cd629a2..6c1e864f 100644 --- a/proxy/blackhole/config.go +++ b/proxy/blackhole/config.go @@ -29,7 +29,7 @@ func (*HTTPResponse) WriteTo(writer buf.Writer) int32 { b := buf.New() common.Must2(b.WriteString(http403response)) n := b.Len() - writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)) + writer.WriteMultiBuffer(buf.MultiBuffer{b}) return n } diff --git a/proxy/shadowsocks/ota_test.go b/proxy/shadowsocks/ota_test.go index 889b1055..28ac416f 100644 --- a/proxy/shadowsocks/ota_test.go +++ b/proxy/shadowsocks/ota_test.go @@ -30,7 +30,7 @@ func TestNormalChunkWriting(t *testing.T) { b := buf.New() b.Write([]byte{11, 12, 13, 14, 15, 16, 17, 18}) - common.Must(writer.WriteMultiBuffer(buf.NewMultiBufferValue(b))) + common.Must(writer.WriteMultiBuffer(buf.MultiBuffer{b})) if diff := cmp.Diff(buffer.Bytes(), []byte{0, 8, 39, 228, 69, 96, 133, 39, 254, 26, 201, 70, 11, 12, 13, 14, 15, 16, 17, 18}); diff != "" { t.Error(diff) } diff --git a/proxy/shadowsocks/protocol.go b/proxy/shadowsocks/protocol.go index 8c63b375..d8dcff68 100644 --- a/proxy/shadowsocks/protocol.go +++ b/proxy/shadowsocks/protocol.go @@ -147,7 +147,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Wri authenticator.Authenticate(authPayload, authBuffer) } - if err := w.WriteMultiBuffer(buf.NewMultiBufferValue(header)); err != nil { + if err := w.WriteMultiBuffer(buf.MultiBuffer{header}); err != nil { return nil, newError("failed to write header").Base(err) } @@ -301,7 +301,7 @@ func (v *UDPReader) ReadMultiBuffer() (buf.MultiBuffer, error) { buffer.Release() return nil, err } - return buf.NewMultiBufferValue(payload), nil + return buf.MultiBuffer{payload}, nil } type UDPWriter struct { diff --git a/proxy/shadowsocks/protocol_test.go b/proxy/shadowsocks/protocol_test.go index d60dbc7f..119496c5 100644 --- a/proxy/shadowsocks/protocol_test.go +++ b/proxy/shadowsocks/protocol_test.go @@ -118,7 +118,7 @@ func TestTCPRequest(t *testing.T) { writer, err := WriteTCPRequest(request, cache) assert(err, IsNil) - assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(data)), IsNil) + assert(writer.WriteMultiBuffer(buf.MultiBuffer{data}), IsNil) decodedRequest, reader, err := ReadTCPSession(request.User, cache) assert(err, IsNil) @@ -168,7 +168,7 @@ func TestUDPReaderWriter(t *testing.T) { { b := buf.New() common.Must2(b.WriteString("test payload")) - err := writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)) + err := writer.WriteMultiBuffer(buf.MultiBuffer{b}) assert(err, IsNil) payload, err := reader.ReadMultiBuffer() @@ -179,7 +179,7 @@ func TestUDPReaderWriter(t *testing.T) { { b := buf.New() common.Must2(b.WriteString("test payload 2")) - err := writer.WriteMultiBuffer(buf.NewMultiBufferValue(b)) + err := writer.WriteMultiBuffer(buf.MultiBuffer{b}) assert(err, IsNil) payload, err := reader.ReadMultiBuffer() diff --git a/proxy/socks/protocol.go b/proxy/socks/protocol.go index 6fd4db5f..bb8a024b 100644 --- a/proxy/socks/protocol.go +++ b/proxy/socks/protocol.go @@ -360,7 +360,7 @@ func (r *UDPReader) ReadMultiBuffer() (buf.MultiBuffer, error) { if _, err := DecodeUDPPacket(b); err != nil { return nil, err } - return buf.NewMultiBufferValue(b), nil + return buf.MultiBuffer{b}, nil } type UDPWriter struct { diff --git a/proxy/socks/protocol_test.go b/proxy/socks/protocol_test.go index 8ad14d33..ff9218f9 100644 --- a/proxy/socks/protocol_test.go +++ b/proxy/socks/protocol_test.go @@ -27,7 +27,7 @@ func TestUDPEncoding(t *testing.T) { content := []byte{'a'} payload := buf.New() payload.Write(content) - assert(writer.WriteMultiBuffer(buf.NewMultiBufferValue(payload)), IsNil) + assert(writer.WriteMultiBuffer(buf.MultiBuffer{payload}), IsNil) reader := NewUDPReader(b) diff --git a/testing/servers/tcp/tcp.go b/testing/servers/tcp/tcp.go index 83b1ac56..c5630253 100644 --- a/testing/servers/tcp/tcp.go +++ b/testing/servers/tcp/tcp.go @@ -76,7 +76,7 @@ func (server *Server) handleConnection(conn net.Conn) { return err } copy(b.Bytes(), server.MsgProcessor(b.Bytes())) - if err := pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)); err != nil { + if err := pWriter.WriteMultiBuffer(buf.MultiBuffer{b}); err != nil { return err } } diff --git a/transport/internet/udp/dispatcher.go b/transport/internet/udp/dispatcher.go index b1818170..792deddf 100644 --- a/transport/internet/udp/dispatcher.go +++ b/transport/internet/udp/dispatcher.go @@ -81,7 +81,7 @@ func (v *Dispatcher) Dispatch(ctx context.Context, destination net.Destination, conn := v.getInboundRay(ctx, destination) outputStream := conn.link.Writer if outputStream != nil { - if err := outputStream.WriteMultiBuffer(buf.NewMultiBufferValue(payload)); err != nil { + if err := outputStream.WriteMultiBuffer(buf.MultiBuffer{payload}); err != nil { newError("failed to write first UDP payload").Base(err).WriteToLog(session.ExportIDToError(ctx)) conn.cancel() return diff --git a/transport/pipe/pipe_test.go b/transport/pipe/pipe_test.go index 6ab78755..5fad05ca 100644 --- a/transport/pipe/pipe_test.go +++ b/transport/pipe/pipe_test.go @@ -19,7 +19,7 @@ func TestPipeReadWrite(t *testing.T) { payload := []byte{'a', 'b', 'c', 'd'} b := buf.New() b.Write(payload) - assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil) + assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil) rb, err := pReader.ReadMultiBuffer() assert(err, IsNil) @@ -33,7 +33,7 @@ func TestPipeCloseError(t *testing.T) { payload := []byte{'a', 'b', 'c', 'd'} b := buf.New() b.Write(payload) - assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil) + assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil) pWriter.CloseError() rb, err := pReader.ReadMultiBuffer() @@ -48,7 +48,7 @@ func TestPipeClose(t *testing.T) { payload := []byte{'a', 'b', 'c', 'd'} b := buf.New() b.Write(payload) - assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)), IsNil) + assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{b}), IsNil) assert(pWriter.Close(), IsNil) rb, err := pReader.ReadMultiBuffer() @@ -66,12 +66,12 @@ func TestPipeLimitZero(t *testing.T) { pReader, pWriter := New(WithSizeLimit(0)) bb := buf.New() bb.Write([]byte{'a', 'b'}) - assert(pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(bb)), IsNil) + assert(pWriter.WriteMultiBuffer(buf.MultiBuffer{bb}), IsNil) err := task.Run(task.Parallel(func() error { b := buf.New() b.Write([]byte{'c', 'd'}) - return pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)) + return pWriter.WriteMultiBuffer(buf.MultiBuffer{b}) }, func() error { time.Sleep(time.Second) @@ -104,7 +104,7 @@ func TestPipeWriteMultiThread(t *testing.T) { go func() { b := buf.New() b.WriteString("abcd") - pWriter.WriteMultiBuffer(buf.NewMultiBufferValue(b)) + pWriter.WriteMultiBuffer(buf.MultiBuffer{b}) wg.Done() }() }