X-Git-Url: http://www.git.cypherpunks.ru/?p=ucspi.git;a=blobdiff_plain;f=conn.go;h=480a2980c3ae4c47e76223429337f22a6cb6ee1f;hp=7a6ac4227f90b8ad8ab2882ebb3736489fecab61;hb=72fc7e6d14a5113b013514eecd3c5b3485671631;hpb=16b21a11cabc2846be21d3214664c6deda174ea2 diff --git a/conn.go b/conn.go index 7a6ac42..480a298 100644 --- a/conn.go +++ b/conn.go @@ -18,7 +18,7 @@ along with this program. If not, see . package ucspi import ( - "io" + "errors" "net" "os" "time" @@ -36,37 +36,33 @@ func (addr *Addr) Network() string { return "tcp" } func (addr *Addr) String() string { return addr.ip + ":" + addr.port } type Conn struct { - R *os.File - W *os.File - eof chan struct{} + R *os.File + W *os.File } -type ReadResult struct { - n int - err error +func NewConn(r, w *os.File) (*Conn, error) { + if r == nil { + return nil, errors.New("no R file descriptor") + } + if w == nil { + return nil, errors.New("no W file descriptor") + } + return &Conn{R: r, W: w}, nil } func (conn *Conn) Read(b []byte) (int, error) { - c := make(chan ReadResult) - go func() { - n, err := conn.R.Read(b) - c <- ReadResult{n, err} - }() - select { - case res := <-c: - return res.n, res.err - case <-conn.eof: - return 0, io.EOF - } + return conn.R.Read(b) } func (conn *Conn) Write(b []byte) (int, error) { return conn.W.Write(b) } func (conn *Conn) Close() error { - if err := conn.R.Close(); err != nil { - return err + errR := conn.R.Close() + errW := conn.W.Close() + if errR != nil { + return errR } - return os.Stdin.Close() + return errW } func (conn *Conn) LocalAddr() net.Addr { @@ -85,12 +81,6 @@ func (conn *Conn) SetDeadline(t time.Time) error { } func (conn *Conn) SetReadDeadline(t time.Time) error { - // An ugly hack to forcefully terminate pending read. - // net/http calls SetReadDeadline(aLongTimeAgo), but file - // descriptors are not capable to exit immediately that way. - if t.Equal(aLongTimeAgo) { - conn.eof <- struct{}{} - } return conn.R.SetReadDeadline(t) }