timed_io.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package net
  2. import (
  3. "io"
  4. "net"
  5. "time"
  6. )
  7. var (
  8. emptyTime time.Time
  9. )
  10. type TimeOutReader struct {
  11. timeout int
  12. connection net.Conn
  13. worker io.Reader
  14. }
  15. func NewTimeOutReader(timeout int, connection net.Conn) *TimeOutReader {
  16. reader := &TimeOutReader{
  17. connection: connection,
  18. timeout: -100,
  19. }
  20. reader.SetTimeOut(timeout)
  21. return reader
  22. }
  23. func (reader *TimeOutReader) Read(p []byte) (int, error) {
  24. return reader.worker.Read(p)
  25. }
  26. func (reader *TimeOutReader) GetTimeOut() int {
  27. return reader.timeout
  28. }
  29. func (reader *TimeOutReader) SetTimeOut(value int) {
  30. if value == reader.timeout {
  31. return
  32. }
  33. reader.timeout = value
  34. if value > 0 {
  35. reader.worker = &timedReaderWorker{
  36. timeout: value,
  37. connection: reader.connection,
  38. }
  39. } else {
  40. reader.worker = &noOpReaderWorker{
  41. connection: reader.connection,
  42. }
  43. }
  44. }
  45. type timedReaderWorker struct {
  46. timeout int
  47. connection net.Conn
  48. }
  49. func (this *timedReaderWorker) Read(p []byte) (int, error) {
  50. deadline := time.Duration(this.timeout) * time.Second
  51. this.connection.SetReadDeadline(time.Now().Add(deadline))
  52. nBytes, err := this.connection.Read(p)
  53. this.connection.SetReadDeadline(emptyTime)
  54. return nBytes, err
  55. }
  56. type noOpReaderWorker struct {
  57. connection net.Conn
  58. }
  59. func (this *noOpReaderWorker) Read(p []byte) (int, error) {
  60. return this.connection.Read(p)
  61. }