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_linux.go 1000B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // +build linux
  2. package poll
  3. import "golang.org/x/sys/unix"
  4. func New(c *Config) (Poller, error) {
  5. epoll, err := newEpoll(&epollConfig{
  6. OnWaitError: c.OnWaitError,
  7. })
  8. if err != nil {
  9. return nil, err
  10. }
  11. return poller{epoll}, nil
  12. }
  13. // poller is a wrapper that implements the Poller interface
  14. type poller struct {
  15. *epoll
  16. }
  17. func (p poller) Start(desc *Descriptor, cb func(Event)) error {
  18. return p.Add(desc.fd(), desc.Event.toEpollEvent(), func(ep epollEvent) {
  19. var ev Event
  20. if ep&unix.EPOLLHUP != 0 {
  21. ev |= EventHUP
  22. }
  23. if ep&unix.EPOLLRDHUP != 0 {
  24. ev |= EventReadHUP
  25. }
  26. if ep&unix.EPOLLIN != 0 {
  27. ev |= EventRead
  28. }
  29. if ep&unix.EPOLLOUT != 0 {
  30. ev |= EventWrite
  31. }
  32. if ep&unix.EPOLLERR != 0 {
  33. ev |= EventError
  34. }
  35. if ep&EPOLLCLOSED != 0 {
  36. ev |= EventClosed
  37. }
  38. cb(ev)
  39. })
  40. }
  41. func (p poller) Resume(desc *Descriptor) error {
  42. return p.Mod(desc.fd(), desc.Event.toEpollEvent())
  43. }
  44. func (p poller) Stop(desc *Descriptor) error {
  45. return p.Del(desc.fd())
  46. }