2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Waiting for FDs via select(2).
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2013-02-06 23:40:18 +01:00
|
|
|
"errors"
|
2010-12-03 05:34:57 +01:00
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pollster struct {
|
2011-10-23 21:04:37 +02:00
|
|
|
readFds, writeFds, repeatFds *syscall.FdSet
|
2011-12-06 19:13:04 +01:00
|
|
|
maxFd int
|
|
|
|
readyReadFds, readyWriteFds *syscall.FdSet
|
|
|
|
nReady int
|
|
|
|
lastFd int
|
2013-02-06 23:40:18 +01:00
|
|
|
closed bool
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-06 19:13:04 +01:00
|
|
|
func newpollster() (p *pollster, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
p = new(pollster)
|
2011-10-23 21:04:37 +02:00
|
|
|
p.readFds = new(syscall.FdSet)
|
|
|
|
p.writeFds = new(syscall.FdSet)
|
|
|
|
p.repeatFds = new(syscall.FdSet)
|
|
|
|
p.readyReadFds = new(syscall.FdSet)
|
|
|
|
p.readyWriteFds = new(syscall.FdSet)
|
2010-12-03 05:34:57 +01:00
|
|
|
p.maxFd = -1
|
|
|
|
p.nReady = 0
|
|
|
|
p.lastFd = 0
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2011-12-06 19:13:04 +01:00
|
|
|
func (p *pollster) AddFD(fd int, mode int, repeat bool) (bool, error) {
|
2011-03-28 20:35:53 +02:00
|
|
|
// pollServer is locked.
|
|
|
|
|
2013-02-06 23:40:18 +01:00
|
|
|
if p.closed {
|
|
|
|
return false, errors.New("pollster closed")
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
if mode == 'r' {
|
|
|
|
syscall.FDSet(fd, p.readFds)
|
|
|
|
} else {
|
|
|
|
syscall.FDSet(fd, p.writeFds)
|
|
|
|
}
|
|
|
|
|
|
|
|
if repeat {
|
|
|
|
syscall.FDSet(fd, p.repeatFds)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fd > p.maxFd {
|
|
|
|
p.maxFd = fd
|
|
|
|
}
|
|
|
|
|
2011-03-28 20:35:53 +02:00
|
|
|
return true, nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2013-02-06 23:40:18 +01:00
|
|
|
func (p *pollster) DelFD(fd int, mode int) bool {
|
2011-03-28 20:35:53 +02:00
|
|
|
// pollServer is locked.
|
|
|
|
|
2013-02-06 23:40:18 +01:00
|
|
|
if p.closed {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
if mode == 'r' {
|
|
|
|
if !syscall.FDIsSet(fd, p.readFds) {
|
|
|
|
print("Select unexpected fd=", fd, " for read\n")
|
2013-02-06 23:40:18 +01:00
|
|
|
return false
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
syscall.FDClr(fd, p.readFds)
|
|
|
|
} else {
|
|
|
|
if !syscall.FDIsSet(fd, p.writeFds) {
|
|
|
|
print("Select unexpected fd=", fd, " for write\n")
|
2013-02-06 23:40:18 +01:00
|
|
|
return false
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
syscall.FDClr(fd, p.writeFds)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't matter if not already present.
|
|
|
|
syscall.FDClr(fd, p.repeatFds)
|
|
|
|
|
|
|
|
// We don't worry about maxFd here.
|
2013-02-06 23:40:18 +01:00
|
|
|
|
|
|
|
return true
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-06 19:13:04 +01:00
|
|
|
func (p *pollster) WaitFD(s *pollServer, nsec int64) (fd int, mode int, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
if p.nReady == 0 {
|
|
|
|
var timeout *syscall.Timeval
|
|
|
|
var tv syscall.Timeval
|
|
|
|
timeout = nil
|
|
|
|
if nsec > 0 {
|
|
|
|
tv = syscall.NsecToTimeval(nsec)
|
|
|
|
timeout = &tv
|
|
|
|
}
|
|
|
|
|
2011-12-13 20:18:34 +01:00
|
|
|
var n int
|
|
|
|
var e error
|
2011-10-23 21:04:37 +02:00
|
|
|
var tmpReadFds, tmpWriteFds syscall.FdSet
|
2010-12-03 05:34:57 +01:00
|
|
|
for {
|
2013-02-06 23:40:18 +01:00
|
|
|
if p.closed {
|
|
|
|
return -1, 0, errors.New("pollster closed")
|
|
|
|
}
|
|
|
|
|
2011-10-23 21:04:37 +02:00
|
|
|
// Temporary syscall.FdSet's into which the values are copied
|
2010-12-03 05:34:57 +01:00
|
|
|
// because select mutates the values.
|
|
|
|
tmpReadFds = *p.readFds
|
|
|
|
tmpWriteFds = *p.writeFds
|
|
|
|
|
2011-03-28 20:35:53 +02:00
|
|
|
s.Unlock()
|
2011-12-06 19:13:04 +01:00
|
|
|
n, e = syscall.Select(p.maxFd+1, &tmpReadFds, &tmpWriteFds, nil, timeout)
|
2011-03-28 20:35:53 +02:00
|
|
|
s.Lock()
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
if e != syscall.EINTR {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2012-04-25 06:26:12 +02:00
|
|
|
if e == syscall.EBADF {
|
|
|
|
// Some file descriptor has been closed.
|
|
|
|
tmpReadFds = syscall.FdSet{}
|
|
|
|
tmpWriteFds = syscall.FdSet{}
|
|
|
|
n = 0
|
|
|
|
for i := 0; i < p.maxFd+1; i++ {
|
|
|
|
if syscall.FDIsSet(i, p.readFds) {
|
|
|
|
var s syscall.Stat_t
|
|
|
|
if syscall.Fstat(i, &s) == syscall.EBADF {
|
|
|
|
syscall.FDSet(i, &tmpReadFds)
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
} else if syscall.FDIsSet(i, p.writeFds) {
|
|
|
|
var s syscall.Stat_t
|
|
|
|
if syscall.Fstat(i, &s) == syscall.EBADF {
|
|
|
|
syscall.FDSet(i, &tmpWriteFds)
|
|
|
|
n++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if e != nil {
|
2010-12-03 05:34:57 +01:00
|
|
|
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
|
2011-12-06 19:13:04 +01:00
|
|
|
for i := p.lastFd; i < p.maxFd+1; i++ {
|
2010-12-03 05:34:57 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-12-06 19:13:04 +01:00
|
|
|
func (p *pollster) Close() error {
|
2013-02-06 23:40:18 +01:00
|
|
|
p.closed = true
|
2010-12-03 05:34:57 +01:00
|
|
|
return nil
|
|
|
|
}
|