本文整理汇总了Golang中syscall.Select函数的典型用法代码示例。如果您正苦于以下问题:Golang Select函数的具体用法?Golang Select怎么用?Golang Select使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Select函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: doSelect
func doSelect(nfd int, r *syscall.FdSet, w *syscall.FdSet, e *syscall.FdSet, timeout *syscall.Timeval) (changed bool, err error) {
err = syscall.Select(nfd, r, w, e, timeout)
if err != nil {
return false, err
}
return true, nil
}
示例2: WaitRead
// WaitRead returns true if f can be readed without blocking or false if not or
// error.
func (s *syn) WaitRead(f *os.File) (bool, error) {
pfd := s.pr.Fd()
ffd := f.Fd()
nfd := 1
if pfd < ffd {
nfd += int(ffd)
} else {
nfd += int(pfd)
}
s.m.Lock()
for {
var r fdset
r.Set(ffd)
r.Set(pfd)
n, err := syscall.Select(nfd, r.Sys(), nil, nil, nil)
if err != nil {
return false, err
}
if n > 0 {
if r.IsSet(pfd) {
// Command waits for access f.
s.m.Unlock()
return false, nil
}
return true, nil
}
}
}
示例3: WaitEvent
// WaitEvent can return dvb.ErrOverflow. If deadline is non zero time WaitEvent
// returns true if it doesn't receive any event up to deatline.
func (f API3) WaitEvent(ev *Event, deadline time.Time) (bool, error) {
fd := f.Fd()
if !deadline.IsZero() {
timeout := deadline.Sub(time.Now())
if timeout <= 0 {
return true, nil
}
var r syscall.FdSet
r.Bits[fd/64] = 1 << (fd % 64)
tv := syscall.NsecToTimeval(int64(timeout))
n, err := syscall.Select(int(fd+1), &r, nil, nil, &tv)
if err != nil {
return false, Error{"get", "event (select)", err}
}
if n == 0 {
return true, nil
}
}
_, _, e := syscall.Syscall(
syscall.SYS_IOCTL,
fd,
_FE_GET_EVENT,
uintptr(unsafe.Pointer(ev)),
)
if e != 0 {
if e == syscall.EOVERFLOW {
return false, dvb.ErrOverflow
}
return false, Error{"get", "event", e}
}
return false, nil
}
示例4: main
func main() {
var (
rset, wset, eset syscall.FdSet
still_running, curl_timeout int = 0, 0
err error
)
ch1 := curl.EasyInit()
ch2 := curl.EasyInit()
ch1.Setopt(curl.OPT_URL, "http://www.163.com")
ch1.Setopt(curl.OPT_HEADER, 0)
ch1.Setopt(curl.OPT_VERBOSE, true)
ch2.Setopt(curl.OPT_URL, "http://www.baidu.com")
ch2.Setopt(curl.OPT_HEADER, 0)
ch2.Setopt(curl.OPT_VERBOSE, true)
mh := curl.MultiInit()
mh.AddHandle(ch1)
mh.AddHandle(ch2)
for {
FD_ZERO(&rset)
FD_ZERO(&wset)
FD_ZERO(&eset)
timeout := syscall.Timeval{Sec: 1, Usec: 0}
curl_timeout, err = mh.Timeout()
if err != nil {
fmt.Printf("Error multi_timeout: %s\n", err)
}
if curl_timeout >= 0 {
timeout.Sec = int64(curl_timeout / 1000)
if timeout.Sec > 1 {
timeout.Sec = 1
} else {
timeout.Usec = int64((curl_timeout % 1000)) * 1000
}
}
max_fd, err := mh.Fdset(&rset, &wset, &eset)
if err != nil {
fmt.Printf("Error FDSET: %s\n", err)
}
_, err = syscall.Select(int(max_fd+1), &rset, &wset, &eset, &timeout)
if err != nil {
fmt.Printf("Error select: %s\n", err)
} else {
still_running, err = mh.Perform()
if still_running > 0 {
fmt.Printf("Still running: %d\n", still_running)
} else {
break
}
}
}
}
示例5: WaitFD
func (p *pollster) WaitFD(nsec int64) (fd int, mode int, err os.Error) {
if p.nReady == 0 {
var timeout *syscall.Timeval
var tv syscall.Timeval
timeout = nil
if nsec > 0 {
tv = syscall.NsecToTimeval(nsec)
timeout = &tv
}
var n, e int
var tmpReadFds, tmpWriteFds syscall.FdSet_t
for {
// Temporary syscall.FdSet_ts into which the values are copied
// because select mutates the values.
tmpReadFds = *p.readFds
tmpWriteFds = *p.writeFds
n, e = syscall.Select(p.maxFd+1, &tmpReadFds, &tmpWriteFds, nil, timeout)
if e != syscall.EINTR {
break
}
}
if e != 0 {
return -1, 0, os.NewSyscallError("select", e)
}
if n == 0 {
return -1, 0, nil
}
p.nReady = n
*p.readyReadFds = tmpReadFds
*p.readyWriteFds = tmpWriteFds
p.lastFd = 0
}
flag := false
for i := p.lastFd; i < p.maxFd+1; i++ {
if syscall.FDIsSet(i, p.readyReadFds) {
flag = true
mode = 'r'
syscall.FDClr(i, p.readyReadFds)
} else if syscall.FDIsSet(i, p.readyWriteFds) {
flag = true
mode = 'w'
syscall.FDClr(i, p.readyWriteFds)
}
if flag {
if !syscall.FDIsSet(i, p.repeatFds) {
p.DelFD(i, mode)
}
p.nReady--
p.lastFd = i
return i, mode, nil
}
}
// Will not reach here. Just to shut up the compiler.
return -1, 0, nil
}
示例6: uxSelect
func uxSelect(nfd int, r, w, e *fdSet, tv *syscall.Timeval) (n int, err error) {
return syscall.Select(nfd,
(*syscall.FdSet)(r),
(*syscall.FdSet)(w),
(*syscall.FdSet)(e),
tv)
}
示例7: waitUntilReadable
// waitUntilReadable blocks until fd is readable.
func waitUntilReadable(fd int) error {
var fds syscall.FdSet
fds.Bits[0] = 1 << uint(fd)
for {
_, err := syscall.Select(fd+1, &fds, nil, nil, nil)
if err != nil {
if err == EINTR {
continue
}
return err
}
return nil // readable
}
}
示例8: output
func output(fd uintptr, out <-chan string, outready chan<- bool) {
set := syscall.FdSet{}
for {
set.Bits[fd/64] = int32(fd) % 64
err := syscall.Select(int(fd+1), nil, &set, nil, nil)
Assert(err == nil, "Select() for write on fd ", fd, ":", err)
outready <- true
cmd := <-out
_, err = syscall.Write(int(fd), []byte(cmd))
Assert(err == nil, "Write() on fd ", fd, ":", err)
}
}
示例9: fdSelect
func (w *Watcher) fdSelect() {
timeval := &syscall.Timeval{
Sec: 1,
Usec: 0,
}
fdset := w.fds.FdSet()
n, err := syscall.Select(int(w.fds[0]+1), nil, nil, fdset, timeval)
if err != nil {
fmt.Printf("failed to call syscall.Select, %s", err)
os.Exit(1)
}
if n != 0 {
w.notify(fdset)
}
}
示例10: uxSelect
func uxSelect(nfd int, r, w, e *fdSet, tv *syscall.Timeval) (n int, err error) {
// The Go syscall.Select for the BSD unixes is buggy. It
// returns only the error and not the number of active file
// descriptors. To cope with this, we return "nfd" as the
// number of active file-descriptors. This can cause
// significant performance degradation but there's nothing
// else we can do.
err = syscall.Select(nfd,
(*syscall.FdSet)(r),
(*syscall.FdSet)(w),
(*syscall.FdSet)(e),
tv)
if err != nil {
return 0, err
}
return nfd, nil
}
示例11: Select
func Select(nfds int, readFds, writeFds *syscall.FdSet, timespec *Timespec) (int, error) {
timeout := &syscall.Timeval{Sec: int64(timespec.Sec), Usec: int32(timespec.Nsec / 1000)}
if err := syscall.Select(nfds, readFds, writeFds, nil, timeout); err != nil {
return 0, err
} else {
max := 0
read := Fdset32{readFds.Bits}
write := Fdset32{writeFds.Bits}
fds := append(read.Fds(), write.Fds()...)
for _, v := range fds {
if v > max {
max = v
}
}
return max + 1, nil
}
}
示例12: packetReader
func packetReader(t *DarwinTunTap) {
var exit bool
packet := make([]byte, 65535)
fds := new(syscall.FdSet)
fd := int(t.file.Fd())
FD_SET(fd, fds)
for {
// On Mac OS X, reading from the tun/tap device will do strange
// things. We need to use syscall.Select.
syscall.Select(fd+1, fds, nil, nil, nil)
// We need to check whether we're to exit here - before the .Read() call
// below, since the syscall above could have been terminated by the
// closure of the associated fd. We do a non-blocking select that will
// fall through to the default case if there is nothing to read from our
// exit channel to determine this.
select {
case exit = <-t.exit:
default:
}
if exit {
log.Printf("Exit signal received\n")
break
}
n, err := t.file.Read(packet)
if err == io.EOF {
break
} else if err != nil {
log.Printf("Error reading from tuntap: %s\n", err)
// This wait is to stop us from getting stuck in an infinite loop
// of reads that all error, and consuming 100% CPU forever.
<-time.After(100 * time.Millisecond)
continue
}
t.packets <- packet[0:n]
}
// This needs to be the last thing in the function.
log.Printf("Done")
t.finished <- true
}
示例13: serverInstance
/*-----------------------------------------------------------------------------
-- FUNCTION: read
--
-- DATE: February 6, 2016
--
-- REVISIONS: February 11, 2016 - Modified for select
--
-- DESIGNER: Marc Vouve
--
-- PROGRAMMER: Marc Vouve
--
-- INTERFACE: func fdSET(p *syscall.FdSet, i int)
--
-- RETURNS: void
--
-- NOTES: reimplementation of C macro
------------------------------------------------------------------------------*/
func serverInstance(srvInfo serverInfo) {
var fdSet, rSet syscall.FdSet
client := make(map[int]connectionInfo)
highClient := srvInfo.listener
fdZERO(&fdSet)
fdSET(&fdSet, srvInfo.listener)
for {
copy(rSet.Bits[:], fdSet.Bits[:])
_, err := syscall.Select(highClient+1, &rSet, nil, nil, nil)
if err != nil {
log.Println("err", err)
return // block shouldn't be hit under normal conditions. If it does something is really wrong.
}
if fdISSET(&rSet, srvInfo.listener) { // new client
newClient, err := newConnection(srvInfo.listener)
if err == nil {
client[newClient.FileDescriptor] = newClient
fdSET(&fdSet, newClient.FileDescriptor)
srvInfo.serverConnection <- 1
if newClient.FileDescriptor > highClient {
highClient = newClient.FileDescriptor
}
}
}
for conn := range client {
socketfd := client[conn].FileDescriptor
if fdISSET(&rSet, socketfd) { // existing connection
connect := client[conn]
err := handleData(&connect)
client[conn] = connect
if err != nil {
if err != io.EOF {
log.Println(err)
}
fdCLEAR(&fdSet, client[conn].FileDescriptor)
endConnection(srvInfo, client[conn])
delete(client, conn)
}
}
}
}
}
示例14: Wait
func (g *GPIO) Wait() (bool, error) {
if g.fd == 0 {
fd, err := syscall.Open(g.valuePath, syscall.O_RDONLY, 0666)
if err != nil {
return false, err
}
g.fd = fd
g.fdSet = new(syscall.FdSet)
FD_SET(g.fd, g.fdSet)
g.buf = make([]byte, 64)
syscall.Read(g.fd, g.buf)
}
syscall.Select(g.fd+1, nil, nil, g.fdSet, nil)
syscall.Seek(g.fd, 0, 0)
_, err := syscall.Read(g.fd, g.buf)
if err != nil {
return false, err
}
return string(g.buf[:2]) == "1\n", nil
}
示例15: loop
func (p *poller) loop(timeout time.Duration) error {
var rset, wset syscall.FdSet
var numfd int
set(&rset, p.pr.Fd(), &numfd)
tv := toTimeval(timeout)
n, err := syscall.Select(numfd+1, &rset, &wset, nil, &tv)
if err != nil {
return err
}
for fd := uintptr(0); n > 0 && fd <= uintptr(numfd); fd++ {
if isset(&rset, fd) {
n--
log.Println(fd, "read")
}
if isset(&wset, fd) {
n--
log.Println(fd, "write")
}
}
return nil
}