@@ -11,13 +11,13 @@ package unixutils
1111import (
1212 "time"
1313
14- "github.com/creack/goselect "
14+ "golang.org/x/sys/unix "
1515)
1616
1717// FDSet is a set of file descriptors suitable for a select call
1818type FDSet struct {
19- set goselect. FDSet
20- max uintptr
19+ set unix. FdSet
20+ max int
2121}
2222
2323// NewFDSet creates a set of file descriptors suitable for a Select call.
@@ -30,34 +30,33 @@ func NewFDSet(fds ...int) *FDSet {
3030// Add adds the file descriptors passed as parameter to the FDSet.
3131func (s * FDSet ) Add (fds ... int ) {
3232 for _ , fd := range fds {
33- f := uintptr (fd )
34- s .set .Set (f )
35- if f > s .max {
36- s .max = f
33+ s .set .Set (fd )
34+ if fd > s .max {
35+ s .max = fd
3736 }
3837 }
3938}
4039
4140// FDResultSets contains the result of a Select operation.
4241type FDResultSets struct {
43- readable * goselect. FDSet
44- writeable * goselect. FDSet
45- errors * goselect. FDSet
42+ readable * unix. FdSet
43+ writeable * unix. FdSet
44+ errors * unix. FdSet
4645}
4746
4847// IsReadable test if a file descriptor is ready to be read.
4948func (r * FDResultSets ) IsReadable (fd int ) bool {
50- return r .readable .IsSet (uintptr ( fd ) )
49+ return r .readable .IsSet (fd )
5150}
5251
5352// IsWritable test if a file descriptor is ready to be written.
5453func (r * FDResultSets ) IsWritable (fd int ) bool {
55- return r .writeable .IsSet (uintptr ( fd ) )
54+ return r .writeable .IsSet (fd )
5655}
5756
5857// IsError test if a file descriptor is in error state.
5958func (r * FDResultSets ) IsError (fd int ) bool {
60- return r .errors .IsSet (uintptr ( fd ) )
59+ return r .errors .IsSet (fd )
6160}
6261
6362// Select performs a select system call,
@@ -68,7 +67,7 @@ func (r *FDResultSets) IsError(fd int) bool {
6867// The function return an FDResultSets that contains all the file descriptor
6968// that have a pending read/write/error event.
7069func Select (rd , wr , er * FDSet , timeout time.Duration ) (* FDResultSets , error ) {
71- max := uintptr ( 0 )
70+ max := 0
7271 res := & FDResultSets {}
7372 if rd != nil {
7473 // fdsets are copied so the parameters are left untouched
@@ -96,6 +95,12 @@ func Select(rd, wr, er *FDSet, timeout time.Duration) (*FDResultSets, error) {
9695 }
9796 }
9897
99- err := goselect .Select (int (max + 1 ), res .readable , res .writeable , res .errors , timeout )
98+ var err error
99+ if timeout != - 1 {
100+ t := unix .NsecToTimeval (timeout .Nanoseconds ())
101+ _ , err = unix .Select (max + 1 , res .readable , res .writeable , res .errors , & t )
102+ } else {
103+ _ , err = unix .Select (max + 1 , res .readable , res .writeable , res .errors , nil )
104+ }
100105 return res , err
101106}
0 commit comments