Browse Source

fix kcp for not sending data immediately

pull/861/head
Darien Raymond 7 years ago
parent
commit
0a6a9547a0
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 27
      transport/internet/kcp/connection.go

27
transport/internet/kcp/connection.go

@ -338,10 +338,15 @@ func (c *Connection) waitForDataOutput() error {
// Write implements io.Writer. // Write implements io.Writer.
func (c *Connection) Write(b []byte) (int, error) { func (c *Connection) Write(b []byte) (int, error) {
totalWritten := 0 updatePending := false
defer func() {
if updatePending {
c.dataUpdater.WakeUp()
}
}()
for { for {
dataWritten := false totalWritten := 0
for { for {
if c == nil || c.State() != StateActive { if c == nil || c.State() != StateActive {
return totalWritten, io.ErrClosedPipe return totalWritten, io.ErrClosedPipe
@ -354,15 +359,16 @@ func (c *Connection) Write(b []byte) (int, error) {
break break
} }
dataWritten = true updatePending = true
if totalWritten == len(b) { if totalWritten == len(b) {
return totalWritten, nil return totalWritten, nil
} }
} }
if dataWritten { if updatePending {
c.dataUpdater.WakeUp() c.dataUpdater.WakeUp()
updatePending = false
} }
if err := c.waitForDataOutput(); err != nil { if err := c.waitForDataOutput(); err != nil {
@ -375,8 +381,14 @@ func (c *Connection) Write(b []byte) (int, error) {
func (c *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error { func (c *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
defer mb.Release() defer mb.Release()
updatePending := false
defer func() {
if updatePending {
c.dataUpdater.WakeUp()
}
}()
for { for {
dataWritten := false
for { for {
if c == nil || c.State() != StateActive { if c == nil || c.State() != StateActive {
return io.ErrClosedPipe return io.ErrClosedPipe
@ -387,14 +399,15 @@ func (c *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
}) { }) {
break break
} }
dataWritten = true updatePending = true
if mb.IsEmpty() { if mb.IsEmpty() {
return nil return nil
} }
} }
if dataWritten { if updatePending {
c.dataUpdater.WakeUp() c.dataUpdater.WakeUp()
updatePending = false
} }
if err := c.waitForDataOutput(); err != nil { if err := c.waitForDataOutput(); err != nil {

Loading…
Cancel
Save