From 3a1cf06dc170ac373b24d9427bc85b7e23875d7f Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Thu, 22 Nov 2018 12:31:11 +0100 Subject: [PATCH] add WriteMultiBuffer for interConn --- transport/internet/quic/conn.go | 35 ++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/transport/internet/quic/conn.go b/transport/internet/quic/conn.go index 01a5bf55..cd113008 100644 --- a/transport/internet/quic/conn.go +++ b/transport/internet/quic/conn.go @@ -7,8 +7,9 @@ import ( "time" quic "github.com/lucas-clemente/quic-go" - "v2ray.com/core/common" + "v2ray.com/core/common/buf" + "v2ray.com/core/common/bytespool" "v2ray.com/core/common/net" "v2ray.com/core/transport/internet" ) @@ -142,6 +143,38 @@ func (c *interConn) Read(b []byte) (int, error) { return c.stream.Read(b) } +func (c *interConn) WriteMultiBuffer(mb buf.MultiBuffer) error { + if mb.IsEmpty() { + return nil + } + + if len(mb) == 1 { + _, err := c.Write(mb[0].Bytes()) + buf.ReleaseMulti(mb) + return err + } + + b := bytespool.Alloc(32 * 1024) + defer bytespool.Free(b) + + reader := buf.MultiBufferContainer{ + MultiBuffer: mb, + } + defer reader.Close() + + for { + nBytes, err := reader.Read(b) + if err != nil { + break + } + if _, err := c.Write(b[:nBytes]); err != nil { + return err + } + } + + return nil +} + func (c *interConn) Write(b []byte) (int, error) { return c.stream.Write(b) }