High level interface for low level file descriptor polling
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

poll.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package poll
  2. import (
  3. "errors"
  4. "os"
  5. "strings"
  6. "syscall"
  7. )
  8. var (
  9. ErrClosed = errors.New("poll: closed")
  10. ErrRegistered = errors.New("poll: file descriptor already registered")
  11. ErrNotRegistered = errors.New("poll: file descriptor not registered")
  12. ErrNotFiler = errors.New("poll: could not get file descriptor")
  13. )
  14. // Event mask
  15. type Event uint16
  16. // Event types
  17. const (
  18. EventRead Event = 1 << iota
  19. EventWrite
  20. EventOneShot
  21. EventEdgeTriggered
  22. EventHUP
  23. EventReadHUP
  24. EventWriteHUP
  25. EventError
  26. EventClosed Event = 0x8000
  27. )
  28. var eventNames = map[Event]string{
  29. EventRead: "read",
  30. EventWrite: "write",
  31. EventOneShot: "one-shot",
  32. EventEdgeTriggered: "edge-triggered",
  33. EventReadHUP: "read-hup",
  34. EventWriteHUP: "write-hup",
  35. EventError: "error",
  36. EventClosed: "closed",
  37. }
  38. func (event Event) String() string {
  39. var s []string
  40. for _, mask := range []Event{
  41. EventRead,
  42. EventWrite,
  43. EventOneShot,
  44. EventEdgeTriggered,
  45. EventReadHUP,
  46. EventWriteHUP,
  47. EventError,
  48. EventClosed,
  49. } {
  50. if event&mask == mask {
  51. s = append(s, eventNames[mask])
  52. }
  53. }
  54. return strings.Join(s, "|")
  55. }
  56. // Poller describes a poll capable interface
  57. type Poller interface {
  58. Start(*Descriptor, func(Event)) error
  59. Stop(*Descriptor) error
  60. Resume(*Descriptor) error
  61. }
  62. // Config contains the base poller configuration
  63. type Config struct {
  64. OnWaitError func(error)
  65. }
  66. // filer implements a method of retrieving the underlying file
  67. type filer interface {
  68. File() (*os.File, error)
  69. }
  70. func isTemporaryError(err error) bool {
  71. if errno, ok := err.(syscall.Errno); ok {
  72. return errno.Temporary()
  73. }
  74. return false
  75. }