Retro68/gcc/libgo/go/net/fd_plan9.go

188 lines
3.6 KiB
Go
Raw Normal View History

// Copyright 2009 The Go Authors. All rights reserved.
2014-09-21 17:33:12 +00:00
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package net
import (
2018-12-28 15:30:48 +00:00
"internal/poll"
2014-09-21 17:33:12 +00:00
"io"
"os"
"syscall"
2019-06-02 15:48:37 +00:00
"time"
2014-09-21 17:33:12 +00:00
)
2017-04-10 11:32:00 +00:00
// Network file descriptor.
2014-09-21 17:33:12 +00:00
type netFD struct {
2018-12-28 15:30:48 +00:00
pfd poll.FD
2015-08-28 15:33:40 +00:00
// immutable until Close
net string
n string
dir string
listen, ctl, data *os.File
laddr, raddr Addr
isStream bool
2014-09-21 17:33:12 +00:00
}
2018-12-28 15:30:48 +00:00
var netdir = "/net" // default network
2014-09-21 17:33:12 +00:00
func newFD(net, name string, listen, ctl, data *os.File, laddr, raddr Addr) (*netFD, error) {
2018-12-28 15:30:48 +00:00
ret := &netFD{
net: net,
n: name,
dir: netdir + "/" + net + "/" + name,
listen: listen,
ctl: ctl, data: data,
laddr: laddr,
raddr: raddr,
2018-12-28 15:30:48 +00:00
}
ret.pfd.Destroy = ret.destroy
return ret, nil
2015-08-28 15:33:40 +00:00
}
func (fd *netFD) init() error {
// stub for future fd.pd.Init(fd)
return nil
}
func (fd *netFD) name() string {
var ls, rs string
if fd.laddr != nil {
ls = fd.laddr.String()
}
if fd.raddr != nil {
rs = fd.raddr.String()
}
2017-04-10 11:32:00 +00:00
return fd.net + ":" + ls + "->" + rs
2014-09-21 17:33:12 +00:00
}
func (fd *netFD) ok() bool { return fd != nil && fd.ctl != nil }
2015-08-28 15:33:40 +00:00
func (fd *netFD) destroy() {
if !fd.ok() {
return
}
err := fd.ctl.Close()
if fd.data != nil {
if err1 := fd.data.Close(); err1 != nil && err == nil {
err = err1
}
}
if fd.listen != nil {
if err1 := fd.listen.Close(); err1 != nil && err == nil {
err = err1
}
}
2015-08-28 15:33:40 +00:00
fd.ctl = nil
fd.data = nil
fd.listen = nil
2015-08-28 15:33:40 +00:00
}
2014-09-21 17:33:12 +00:00
func (fd *netFD) Read(b []byte) (n int, err error) {
if !fd.ok() || fd.data == nil {
return 0, syscall.EINVAL
}
2018-12-28 15:30:48 +00:00
n, err = fd.pfd.Read(fd.data.Read, b)
2017-04-10 11:32:00 +00:00
if fd.net == "udp" && err == io.EOF {
2014-09-21 17:33:12 +00:00
n = 0
err = nil
}
return
}
func (fd *netFD) Write(b []byte) (n int, err error) {
if !fd.ok() || fd.data == nil {
return 0, syscall.EINVAL
}
2018-12-28 15:30:48 +00:00
return fd.pfd.Write(fd.data.Write, b)
2014-09-21 17:33:12 +00:00
}
2015-08-28 15:33:40 +00:00
func (fd *netFD) closeRead() error {
2014-09-21 17:33:12 +00:00
if !fd.ok() {
return syscall.EINVAL
}
return syscall.EPLAN9
}
2015-08-28 15:33:40 +00:00
func (fd *netFD) closeWrite() error {
2014-09-21 17:33:12 +00:00
if !fd.ok() {
return syscall.EINVAL
}
return syscall.EPLAN9
}
func (fd *netFD) Close() error {
2018-12-28 15:30:48 +00:00
if err := fd.pfd.Close(); err != nil {
return err
2015-08-28 15:33:40 +00:00
}
2014-09-21 17:33:12 +00:00
if !fd.ok() {
return syscall.EINVAL
}
2017-04-10 11:32:00 +00:00
if fd.net == "tcp" {
// The following line is required to unblock Reads.
_, err := fd.ctl.WriteString("close")
if err != nil {
return err
}
2017-04-10 11:32:00 +00:00
}
2014-09-21 17:33:12 +00:00
err := fd.ctl.Close()
if fd.data != nil {
if err1 := fd.data.Close(); err1 != nil && err == nil {
err = err1
}
}
if fd.listen != nil {
if err1 := fd.listen.Close(); err1 != nil && err == nil {
err = err1
}
}
2014-09-21 17:33:12 +00:00
fd.ctl = nil
fd.data = nil
fd.listen = nil
2014-09-21 17:33:12 +00:00
return err
}
// This method is only called via Conn.
func (fd *netFD) dup() (*os.File, error) {
if !fd.ok() || fd.data == nil {
return nil, syscall.EINVAL
}
return fd.file(fd.data, fd.dir+"/data")
}
func (l *TCPListener) dup() (*os.File, error) {
if !l.fd.ok() {
return nil, syscall.EINVAL
}
return l.fd.file(l.fd.ctl, l.fd.dir+"/ctl")
}
func (fd *netFD) file(f *os.File, s string) (*os.File, error) {
dfd, err := syscall.Dup(int(f.Fd()), -1)
if err != nil {
2017-04-10 11:32:00 +00:00
return nil, os.NewSyscallError("dup", err)
2014-09-21 17:33:12 +00:00
}
return os.NewFile(uintptr(dfd), s), nil
}
func setReadBuffer(fd *netFD, bytes int) error {
return syscall.EPLAN9
}
func setWriteBuffer(fd *netFD, bytes int) error {
return syscall.EPLAN9
}
2019-06-02 15:48:37 +00:00
func (fd *netFD) SetDeadline(t time.Time) error {
return fd.pfd.SetDeadline(t)
}
func (fd *netFD) SetReadDeadline(t time.Time) error {
return fd.pfd.SetReadDeadline(t)
}
func (fd *netFD) SetWriteDeadline(t time.Time) error {
return fd.pfd.SetWriteDeadline(t)
}