1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-30 05:56:54 -05:00

remove NewMultiBufferValue

This commit is contained in:
Darien Raymond 2018-11-16 11:08:12 +01:00
parent db5259e75b
commit a01fdc29a6
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
21 changed files with 33 additions and 38 deletions

View File

@ -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())

View File

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

View File

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

View File

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

View File

@ -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()

View File

@ -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.

View File

@ -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())

View File

@ -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 {

View File

@ -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{}))

View File

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

View File

@ -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.

View File

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

View File

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

View File

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

View File

@ -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 {

View File

@ -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()

View File

@ -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 {

View File

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

View File

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

View File

@ -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

View File

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