mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-02 23:47:07 -05:00
merge duplicated code
This commit is contained in:
parent
5ba41e50ec
commit
88e757e33f
@ -192,6 +192,25 @@ func SplitSize(mb MultiBuffer, size int32) (MultiBuffer, MultiBuffer) {
|
|||||||
return mb, r
|
return mb, r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WriteMultiBuffer writes all buffers from the MultiBuffer to the Writer one by one, and return error if any, with leftover MultiBuffer.
|
||||||
|
func WriteMultiBuffer(writer io.Writer, mb MultiBuffer) (MultiBuffer, error) {
|
||||||
|
for {
|
||||||
|
mb2, b := SplitFirst(mb)
|
||||||
|
mb = mb2
|
||||||
|
if b == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := writer.Write(b.Bytes())
|
||||||
|
b.Release()
|
||||||
|
if err != nil {
|
||||||
|
return mb, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
|
@ -219,19 +219,9 @@ type SequentialWriter struct {
|
|||||||
|
|
||||||
// WriteMultiBuffer implements Writer.
|
// WriteMultiBuffer implements Writer.
|
||||||
func (w *SequentialWriter) WriteMultiBuffer(mb MultiBuffer) error {
|
func (w *SequentialWriter) WriteMultiBuffer(mb MultiBuffer) error {
|
||||||
defer ReleaseMulti(mb)
|
mb, err := WriteMultiBuffer(w.Writer, mb)
|
||||||
|
ReleaseMulti(mb)
|
||||||
for _, b := range mb {
|
return err
|
||||||
if b.IsEmpty() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := WriteAllBytes(w.Writer, b.Bytes()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type noOpWriter byte
|
type noOpWriter byte
|
||||||
|
@ -172,21 +172,9 @@ func (c *interConn) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|||||||
|
|
||||||
func (c *interConn) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
func (c *interConn) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
||||||
mb = buf.Compact(mb)
|
mb = buf.Compact(mb)
|
||||||
|
mb, err := buf.WriteMultiBuffer(c, mb)
|
||||||
for {
|
buf.ReleaseMulti(mb)
|
||||||
mb2, b := buf.SplitFirst(mb)
|
return err
|
||||||
mb = mb2
|
|
||||||
if b == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := buf.WriteAllBytes(c, b.Bytes()); err != nil {
|
|
||||||
buf.ReleaseMulti(mb)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *interConn) Write(b []byte) (int, error) {
|
func (c *interConn) Write(b []byte) (int, error) {
|
||||||
|
@ -70,21 +70,9 @@ func (c *connection) Write(b []byte) (int, error) {
|
|||||||
|
|
||||||
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
func (c *connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
|
||||||
mb = buf.Compact(mb)
|
mb = buf.Compact(mb)
|
||||||
|
mb, err := buf.WriteMultiBuffer(c, mb)
|
||||||
for {
|
buf.ReleaseMulti(mb)
|
||||||
mb2, b := buf.SplitFirst(mb)
|
return err
|
||||||
mb = mb2
|
|
||||||
if b == nil {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := buf.WriteAllBytes(c, b.Bytes()); err != nil {
|
|
||||||
buf.ReleaseMulti(mb)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *connection) Close() error {
|
func (c *connection) Close() error {
|
||||||
|
Loading…
Reference in New Issue
Block a user