mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-18 07:17:32 -05:00
rewrite SplitFirst
This commit is contained in:
parent
cb7646f682
commit
bcd5d026fe
@ -35,6 +35,7 @@ func MergeMulti(dest MultiBuffer, src MultiBuffer) (MultiBuffer, MultiBuffer) {
|
|||||||
return dest, src[:0]
|
return dest, src[:0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MergeBytes merges the given bytes into MultiBuffer and return the new address of the merged MultiBuffer.
|
||||||
func MergeBytes(dest MultiBuffer, src []byte) MultiBuffer {
|
func MergeBytes(dest MultiBuffer, src []byte) MultiBuffer {
|
||||||
n := len(dest)
|
n := len(dest)
|
||||||
if n > 0 && !(dest)[n-1].IsFull() {
|
if n > 0 && !(dest)[n-1].IsFull() {
|
||||||
@ -94,6 +95,8 @@ func ReadFrom(reader io.Reader) (MultiBuffer, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SplitBytes splits the given amount of bytes from the beginning of the MultiBuffer.
|
||||||
|
// It returns the new address of MultiBuffer leftover, and number of bytes written into the input byte slice.
|
||||||
func SplitBytes(mb MultiBuffer, b []byte) (MultiBuffer, int) {
|
func SplitBytes(mb MultiBuffer, b []byte) (MultiBuffer, int) {
|
||||||
totalBytes := 0
|
totalBytes := 0
|
||||||
|
|
||||||
@ -112,6 +115,18 @@ func SplitBytes(mb MultiBuffer, b []byte) (MultiBuffer, int) {
|
|||||||
return mb, totalBytes
|
return mb, totalBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SplitFirst splits the first Buffer from the beginning of the MultiBuffer.
|
||||||
|
func SplitFirst(mb MultiBuffer) (MultiBuffer, *Buffer) {
|
||||||
|
if len(mb) == 0 {
|
||||||
|
return mb, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
b := mb[0]
|
||||||
|
mb[0] = nil
|
||||||
|
mb = mb[1:]
|
||||||
|
return mb, b
|
||||||
|
}
|
||||||
|
|
||||||
// Len returns the total number of bytes in the MultiBuffer.
|
// Len returns the total number of bytes in the MultiBuffer.
|
||||||
func (mb MultiBuffer) Len() int32 {
|
func (mb MultiBuffer) Len() int32 {
|
||||||
if mb == nil {
|
if mb == nil {
|
||||||
@ -167,21 +182,12 @@ func (mb *MultiBuffer) SliceBySize(size int32) MultiBuffer {
|
|||||||
return slice
|
return slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// SplitFirst splits out the first Buffer in this MultiBuffer.
|
// MultiBufferContainer is a ReadWriteCloser wrapper over MultiBuffer.
|
||||||
func (mb *MultiBuffer) SplitFirst() *Buffer {
|
|
||||||
if len(*mb) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
b := (*mb)[0]
|
|
||||||
(*mb)[0] = nil
|
|
||||||
*mb = (*mb)[1:]
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
type MultiBufferContainer struct {
|
type MultiBufferContainer struct {
|
||||||
MultiBuffer
|
MultiBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read implements io.Reader.
|
||||||
func (c *MultiBufferContainer) Read(b []byte) (int, error) {
|
func (c *MultiBufferContainer) Read(b []byte) (int, error) {
|
||||||
if c.MultiBuffer.IsEmpty() {
|
if c.MultiBuffer.IsEmpty() {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
@ -192,23 +198,27 @@ func (c *MultiBufferContainer) Read(b []byte) (int, error) {
|
|||||||
return nBytes, nil
|
return nBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadMultiBuffer implements Reader.
|
||||||
func (c *MultiBufferContainer) ReadMultiBuffer() (MultiBuffer, error) {
|
func (c *MultiBufferContainer) ReadMultiBuffer() (MultiBuffer, error) {
|
||||||
mb := c.MultiBuffer
|
mb := c.MultiBuffer
|
||||||
c.MultiBuffer = nil
|
c.MultiBuffer = nil
|
||||||
return mb, nil
|
return mb, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write implements io.Writer.
|
||||||
func (c *MultiBufferContainer) Write(b []byte) (int, error) {
|
func (c *MultiBufferContainer) Write(b []byte) (int, error) {
|
||||||
c.MultiBuffer = MergeBytes(c.MultiBuffer, b)
|
c.MultiBuffer = MergeBytes(c.MultiBuffer, b)
|
||||||
return len(b), nil
|
return len(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteMultiBuffer implement Writer.
|
||||||
func (c *MultiBufferContainer) WriteMultiBuffer(b MultiBuffer) error {
|
func (c *MultiBufferContainer) WriteMultiBuffer(b MultiBuffer) error {
|
||||||
mb, _ := MergeMulti(c.MultiBuffer, b)
|
mb, _ := MergeMulti(c.MultiBuffer, b)
|
||||||
c.MultiBuffer = mb
|
c.MultiBuffer = mb
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close implement io.Closer.
|
||||||
func (c *MultiBufferContainer) Close() error {
|
func (c *MultiBufferContainer) Close() error {
|
||||||
c.MultiBuffer = ReleaseMulti(c.MultiBuffer)
|
c.MultiBuffer = ReleaseMulti(c.MultiBuffer)
|
||||||
return nil
|
return nil
|
||||||
|
@ -116,10 +116,10 @@ func TestAuthenticationReaderWriterPacket(t *testing.T) {
|
|||||||
mb, err := reader.ReadMultiBuffer()
|
mb, err := reader.ReadMultiBuffer()
|
||||||
assert(err, IsNil)
|
assert(err, IsNil)
|
||||||
|
|
||||||
b1 := mb.SplitFirst()
|
mb, b1 := buf.SplitFirst(mb)
|
||||||
assert(b1.String(), Equals, "abcd")
|
assert(b1.String(), Equals, "abcd")
|
||||||
|
|
||||||
b2 := mb.SplitFirst()
|
mb, b2 := buf.SplitFirst(mb)
|
||||||
assert(b2.String(), Equals, "efgh")
|
assert(b2.String(), Equals, "efgh")
|
||||||
|
|
||||||
assert(mb.IsEmpty(), IsTrue)
|
assert(mb.IsEmpty(), IsTrue)
|
||||||
|
@ -96,7 +96,9 @@ func (w *Writer) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
|||||||
if w.transferType == protocol.TransferTypeStream {
|
if w.transferType == protocol.TransferTypeStream {
|
||||||
chunk = mb.SliceBySize(8 * 1024)
|
chunk = mb.SliceBySize(8 * 1024)
|
||||||
} else {
|
} else {
|
||||||
chunk = buf.MultiBuffer{mb.SplitFirst()}
|
mb2, b := buf.SplitFirst(mb)
|
||||||
|
mb = mb2
|
||||||
|
chunk = buf.MultiBuffer{b}
|
||||||
}
|
}
|
||||||
if err := w.writeData(chunk); err != nil {
|
if err := w.writeData(chunk); err != nil {
|
||||||
return err
|
return err
|
||||||
|
Loading…
Reference in New Issue
Block a user