1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00

direct multibuf reading from kcp engine

This commit is contained in:
Darien Raymond 2017-04-20 15:02:35 +02:00
parent 114f390a69
commit f01b0552a9
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 70 additions and 25 deletions

View File

@ -23,3 +23,13 @@ func TestMultiBufferRead(t *testing.T) {
assert.Int(nBytes).Equals(4) assert.Int(nBytes).Equals(4)
assert.Bytes(bs[:nBytes]).Equals([]byte("abcd")) assert.Bytes(bs[:nBytes]).Equals([]byte("abcd"))
} }
func TestMultiBufferAppend(t *testing.T) {
assert := assert.On(t)
var mb MultiBuffer
b := New()
b.AppendBytes('a', 'b')
mb.Append(b)
assert.Int(mb.Len()).Equals(2)
}

View File

@ -169,6 +169,7 @@ type SystemConnection interface {
} }
var ( var (
_ buf.MultiBufferReader = (*Connection)(nil)
_ buf.MultiBufferWriter = (*Connection)(nil) _ buf.MultiBufferWriter = (*Connection)(nil)
) )
@ -264,6 +265,43 @@ func (v *Connection) OnDataOutput() {
} }
} }
// ReadMultiBuffer implements buf.MultiBufferReader.
func (v *Connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
if v == nil {
return nil, io.EOF
}
for {
if v.State().Is(StateReadyToClose, StateTerminating, StateTerminated) {
return nil, io.EOF
}
mb := v.receivingWorker.ReadMultiBuffer()
if !mb.IsEmpty() {
return mb, nil
}
if v.State() == StatePeerTerminating {
return nil, io.EOF
}
duration := time.Minute
if !v.rd.IsZero() {
duration = v.rd.Sub(time.Now())
if duration < 0 {
return nil, ErrIOTimeout
}
}
select {
case <-v.dataInput:
case <-time.After(duration):
if !v.rd.IsZero() && v.rd.Before(time.Now()) {
return nil, ErrIOTimeout
}
}
}
}
// Read implements the Conn Read method. // Read implements the Conn Read method.
func (v *Connection) Read(b []byte) (int, error) { func (v *Connection) Read(b []byte) (int, error) {
if v == nil { if v == nil {

View File

@ -149,7 +149,7 @@ func (v *AckList) Flush(current uint32, rto uint32) {
type ReceivingWorker struct { type ReceivingWorker struct {
sync.RWMutex sync.RWMutex
conn *Connection conn *Connection
leftOver *buf.Buffer leftOver buf.MultiBuffer
window *ReceivingWindow window *ReceivingWindow
acklist *AckList acklist *AckList
nextNumber uint32 nextNumber uint32
@ -196,42 +196,39 @@ func (v *ReceivingWorker) ProcessSegment(seg *DataSegment) {
} }
} }
func (v *ReceivingWorker) Read(b []byte) int { func (v *ReceivingWorker) ReadMultiBuffer() buf.MultiBuffer {
if v.leftOver != nil {
mb := v.leftOver
v.leftOver = nil
return mb
}
mb := buf.NewMultiBuffer()
v.Lock() v.Lock()
defer v.Unlock() defer v.Unlock()
for {
total := 0
if v.leftOver != nil {
nBytes := copy(b, v.leftOver.Bytes())
if nBytes < v.leftOver.Len() {
v.leftOver.SliceFrom(nBytes)
return nBytes
}
v.leftOver.Release()
v.leftOver = nil
total += nBytes
}
for total < len(b) {
seg := v.window.RemoveFirst() seg := v.window.RemoveFirst()
if seg == nil { if seg == nil {
break break
} }
v.window.Advance() v.window.Advance()
v.nextNumber++ v.nextNumber++
mb.Append(seg.Data)
nBytes := copy(b[total:], seg.Data.Bytes())
total += nBytes
if nBytes < seg.Data.Len() {
seg.Data.SliceFrom(nBytes)
v.leftOver = seg.Data
seg.Data = nil seg.Data = nil
seg.Release() seg.Release()
break
} }
seg.Release()
return mb
} }
return total
func (v *ReceivingWorker) Read(b []byte) int {
mb := v.ReadMultiBuffer()
nBytes, _ := mb.Read(b)
if !mb.IsEmpty() {
v.leftOver = mb
}
return nBytes
} }
func (w *ReceivingWorker) IsDataAvailable() bool { func (w *ReceivingWorker) IsDataAvailable() bool {