2017-11-02 18:08:18 +03:00
|
|
|
// Copyright 2017 Joshua J Baker. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-10-28 22:23:13 +03:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-11-09 02:45:30 +03:00
|
|
|
func AddRead(p, fd int, readon, writeon *bool) error {
|
|
|
|
if readon != nil {
|
|
|
|
if *readon {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*readon = true
|
|
|
|
}
|
|
|
|
if writeon == nil || !*writeon {
|
|
|
|
return syscall.EpollCtl(p, syscall.EPOLL_CTL_ADD, fd,
|
|
|
|
&syscall.EpollEvent{Fd: int32(fd),
|
|
|
|
Events: syscall.EPOLLIN,
|
|
|
|
})
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
|
|
|
return syscall.EpollCtl(p, syscall.EPOLL_CTL_MOD, fd,
|
|
|
|
&syscall.EpollEvent{Fd: int32(fd),
|
|
|
|
Events: syscall.EPOLLIN | syscall.EPOLLOUT,
|
|
|
|
})
|
|
|
|
}
|
2017-11-09 02:45:30 +03:00
|
|
|
func DelRead(p, fd int, readon, writeon *bool) error {
|
|
|
|
if readon != nil {
|
|
|
|
if !*readon {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*readon = false
|
|
|
|
}
|
|
|
|
if writeon == nil || !*writeon {
|
|
|
|
return syscall.EpollCtl(p, syscall.EPOLL_CTL_DEL, fd,
|
|
|
|
&syscall.EpollEvent{Fd: int32(fd),
|
|
|
|
Events: syscall.EPOLLIN,
|
|
|
|
})
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
|
|
|
return syscall.EpollCtl(p, syscall.EPOLL_CTL_MOD, fd,
|
|
|
|
&syscall.EpollEvent{Fd: int32(fd),
|
2017-11-09 02:45:30 +03:00
|
|
|
Events: syscall.EPOLLOUT,
|
2017-10-28 22:23:13 +03:00
|
|
|
})
|
|
|
|
}
|
2017-11-09 02:45:30 +03:00
|
|
|
|
|
|
|
func AddWrite(p, fd int, readon, writeon *bool) error {
|
|
|
|
if writeon != nil {
|
|
|
|
if *writeon {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*writeon = true
|
|
|
|
}
|
|
|
|
if readon == nil || !*readon {
|
|
|
|
return syscall.EpollCtl(p, syscall.EPOLL_CTL_ADD, fd,
|
|
|
|
&syscall.EpollEvent{Fd: int32(fd),
|
|
|
|
Events: syscall.EPOLLOUT,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return syscall.EpollCtl(p, syscall.EPOLL_CTL_MOD, fd,
|
|
|
|
&syscall.EpollEvent{Fd: int32(fd),
|
|
|
|
Events: syscall.EPOLLIN | syscall.EPOLLOUT,
|
|
|
|
})
|
2017-10-28 22:23:13 +03:00
|
|
|
}
|
2017-11-09 02:45:30 +03:00
|
|
|
func DelWrite(p, fd int, readon, writeon *bool) error {
|
|
|
|
if writeon != nil {
|
|
|
|
if !*writeon {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*writeon = false
|
|
|
|
}
|
|
|
|
if readon == nil || !*readon {
|
|
|
|
return syscall.EpollCtl(p, syscall.EPOLL_CTL_DEL, fd,
|
|
|
|
&syscall.EpollEvent{Fd: int32(fd),
|
|
|
|
Events: syscall.EPOLLOUT,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return syscall.EpollCtl(p, syscall.EPOLL_CTL_MOD, fd,
|
2017-10-28 22:23:13 +03:00
|
|
|
&syscall.EpollEvent{Fd: int32(fd),
|
|
|
|
Events: syscall.EPOLLIN,
|
|
|
|
})
|
|
|
|
}
|
2017-11-09 02:45:30 +03:00
|
|
|
func MakePoll() (p int, err error) {
|
|
|
|
return syscall.EpollCreate1(0)
|
|
|
|
}
|
2017-10-28 22:23:13 +03:00
|
|
|
func MakeEvents(n int) interface{} {
|
|
|
|
return make([]syscall.EpollEvent, n)
|
|
|
|
}
|
|
|
|
func Wait(p int, evs interface{}, timeout time.Duration) (n int, err error) {
|
|
|
|
if timeout < 0 {
|
|
|
|
timeout = 0
|
|
|
|
}
|
|
|
|
ts := int(timeout / time.Millisecond)
|
|
|
|
return syscall.EpollWait(p, evs.([]syscall.EpollEvent), ts)
|
|
|
|
}
|
|
|
|
func GetFD(evs interface{}, i int) int {
|
|
|
|
return int(evs.([]syscall.EpollEvent)[i].Fd)
|
|
|
|
}
|