1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00

merge buf stream

This commit is contained in:
Darien Raymond 2017-03-27 11:26:44 +02:00
parent 14824c8451
commit f643344154
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 60 additions and 14 deletions

View File

@ -31,9 +31,10 @@ func (r *MergingReader) Read() (*Buffer, error) {
return b, nil return b, nil
} }
for {
b2, err := r.timeoutReader.ReadTimeout(0) b2, err := r.timeoutReader.ReadTimeout(0)
if err != nil { if err != nil {
return b, nil break
} }
nBytes := b.Append(b2.Bytes()) nBytes := b.Append(b2.Bytes())
@ -42,6 +43,8 @@ func (r *MergingReader) Read() (*Buffer, error) {
b2.Release() b2.Release()
} else { } else {
r.leftover = b2 r.leftover = b2
break
}
} }
return b, nil return b, nil

View File

@ -0,0 +1,33 @@
package buf_test
import (
"testing"
"context"
. "v2ray.com/core/common/buf"
"v2ray.com/core/testing/assert"
"v2ray.com/core/transport/ray"
)
func TestMergingReader(t *testing.T) {
assert := assert.On(t)
stream := ray.NewStream(context.Background())
b1 := New()
b1.AppendBytes('a', 'b', 'c')
stream.Write(b1)
b2 := New()
b2.AppendBytes('e', 'f', 'g')
stream.Write(b2)
b3 := New()
b3.AppendBytes('h', 'i', 'j')
stream.Write(b3)
reader := NewMergingReader(stream)
b, err := reader.Read()
assert.Error(err).IsNil()
assert.String(b.String()).Equals("abcefghij")
}

View File

@ -101,10 +101,13 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
return err return err
} }
bufferedWriter.SetBuffered(false) if err := bufferedWriter.SetBuffered(false); err != nil {
return err
}
requestDone := signal.ExecuteAsync(func() error { requestDone := signal.ExecuteAsync(func() error {
if err := buf.PipeUntilEOF(timer, outboundRay.OutboundInput(), bodyWriter); err != nil { mergedInput := buf.NewMergingReader(outboundRay.OutboundInput())
if err := buf.PipeUntilEOF(timer, mergedInput, bodyWriter); err != nil {
return err return err
} }
return nil return nil

View File

@ -124,9 +124,16 @@ func (v *Handler) Process(ctx context.Context, outboundRay ray.OutboundRay, dial
firstPayload.Release() firstPayload.Release()
} }
writer.SetBuffered(false) if err := writer.SetBuffered(false); err != nil {
return err
}
if err := buf.PipeUntilEOF(timer, input, bodyWriter); err != nil { var inputReader buf.Reader = input
if request.Command == protocol.RequestCommandTCP {
inputReader = buf.NewMergingReader(input)
}
if err := buf.PipeUntilEOF(timer, inputReader, bodyWriter); err != nil {
return err return err
} }