waitReadWithTimeout will wait full n bytes or until timeout.
(n int, timeout time.Duration)
| 482 | |
| 483 | // waitReadWithTimeout will wait full n bytes or until timeout. |
| 484 | func (c *connection) waitReadWithTimeout(n int, timeout time.Duration) (err error) { |
| 485 | if c.readTimer == nil { |
| 486 | c.readTimer = time.NewTimer(timeout) |
| 487 | } else { |
| 488 | c.readTimer.Reset(timeout) |
| 489 | } |
| 490 | |
| 491 | for c.inputBuffer.Len() < n { |
| 492 | switch c.status(closing) { |
| 493 | case poller: |
| 494 | // cannot return directly, stop timer first! |
| 495 | err = Exception(ErrEOF, "wait read") |
| 496 | goto RET |
| 497 | case user: |
| 498 | // cannot return directly, stop timer first! |
| 499 | err = Exception(ErrConnClosed, "wait read") |
| 500 | goto RET |
| 501 | default: |
| 502 | select { |
| 503 | case <-c.readTimer.C: |
| 504 | // double check if there is enough data to be read |
| 505 | if c.inputBuffer.Len() >= n { |
| 506 | return nil |
| 507 | } |
| 508 | return Exception(ErrReadTimeout, c.remoteAddr.String()) |
| 509 | case err = <-c.readTrigger: |
| 510 | if err != nil { |
| 511 | goto RET |
| 512 | } |
| 513 | continue |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | RET: |
| 518 | // clean timer.C |
| 519 | if !c.readTimer.Stop() { |
| 520 | <-c.readTimer.C |
| 521 | } |
| 522 | return err |
| 523 | } |
| 524 | |
| 525 | // flush writes data directly. |
| 526 | func (c *connection) flush() error { |