annotate libgo/go/net/fd_plan9.go @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // Copyright 2009 The Go Authors. All rights reserved.
kono
parents:
diff changeset
2 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
3 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 package net
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 import (
kono
parents:
diff changeset
8 "internal/poll"
kono
parents:
diff changeset
9 "io"
kono
parents:
diff changeset
10 "os"
kono
parents:
diff changeset
11 "syscall"
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
12 "time"
111
kono
parents:
diff changeset
13 )
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 // Network file descriptor.
kono
parents:
diff changeset
16 type netFD struct {
kono
parents:
diff changeset
17 pfd poll.FD
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 // immutable until Close
kono
parents:
diff changeset
20 net string
kono
parents:
diff changeset
21 n string
kono
parents:
diff changeset
22 dir string
kono
parents:
diff changeset
23 listen, ctl, data *os.File
kono
parents:
diff changeset
24 laddr, raddr Addr
kono
parents:
diff changeset
25 isStream bool
kono
parents:
diff changeset
26 }
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 var netdir = "/net" // default network
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 func newFD(net, name string, listen, ctl, data *os.File, laddr, raddr Addr) (*netFD, error) {
kono
parents:
diff changeset
31 ret := &netFD{
kono
parents:
diff changeset
32 net: net,
kono
parents:
diff changeset
33 n: name,
kono
parents:
diff changeset
34 dir: netdir + "/" + net + "/" + name,
kono
parents:
diff changeset
35 listen: listen,
kono
parents:
diff changeset
36 ctl: ctl, data: data,
kono
parents:
diff changeset
37 laddr: laddr,
kono
parents:
diff changeset
38 raddr: raddr,
kono
parents:
diff changeset
39 }
kono
parents:
diff changeset
40 ret.pfd.Destroy = ret.destroy
kono
parents:
diff changeset
41 return ret, nil
kono
parents:
diff changeset
42 }
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 func (fd *netFD) init() error {
kono
parents:
diff changeset
45 // stub for future fd.pd.Init(fd)
kono
parents:
diff changeset
46 return nil
kono
parents:
diff changeset
47 }
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 func (fd *netFD) name() string {
kono
parents:
diff changeset
50 var ls, rs string
kono
parents:
diff changeset
51 if fd.laddr != nil {
kono
parents:
diff changeset
52 ls = fd.laddr.String()
kono
parents:
diff changeset
53 }
kono
parents:
diff changeset
54 if fd.raddr != nil {
kono
parents:
diff changeset
55 rs = fd.raddr.String()
kono
parents:
diff changeset
56 }
kono
parents:
diff changeset
57 return fd.net + ":" + ls + "->" + rs
kono
parents:
diff changeset
58 }
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 func (fd *netFD) ok() bool { return fd != nil && fd.ctl != nil }
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 func (fd *netFD) destroy() {
kono
parents:
diff changeset
63 if !fd.ok() {
kono
parents:
diff changeset
64 return
kono
parents:
diff changeset
65 }
kono
parents:
diff changeset
66 err := fd.ctl.Close()
kono
parents:
diff changeset
67 if fd.data != nil {
kono
parents:
diff changeset
68 if err1 := fd.data.Close(); err1 != nil && err == nil {
kono
parents:
diff changeset
69 err = err1
kono
parents:
diff changeset
70 }
kono
parents:
diff changeset
71 }
kono
parents:
diff changeset
72 if fd.listen != nil {
kono
parents:
diff changeset
73 if err1 := fd.listen.Close(); err1 != nil && err == nil {
kono
parents:
diff changeset
74 err = err1
kono
parents:
diff changeset
75 }
kono
parents:
diff changeset
76 }
kono
parents:
diff changeset
77 fd.ctl = nil
kono
parents:
diff changeset
78 fd.data = nil
kono
parents:
diff changeset
79 fd.listen = nil
kono
parents:
diff changeset
80 }
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 func (fd *netFD) Read(b []byte) (n int, err error) {
kono
parents:
diff changeset
83 if !fd.ok() || fd.data == nil {
kono
parents:
diff changeset
84 return 0, syscall.EINVAL
kono
parents:
diff changeset
85 }
kono
parents:
diff changeset
86 n, err = fd.pfd.Read(fd.data.Read, b)
kono
parents:
diff changeset
87 if fd.net == "udp" && err == io.EOF {
kono
parents:
diff changeset
88 n = 0
kono
parents:
diff changeset
89 err = nil
kono
parents:
diff changeset
90 }
kono
parents:
diff changeset
91 return
kono
parents:
diff changeset
92 }
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 func (fd *netFD) Write(b []byte) (n int, err error) {
kono
parents:
diff changeset
95 if !fd.ok() || fd.data == nil {
kono
parents:
diff changeset
96 return 0, syscall.EINVAL
kono
parents:
diff changeset
97 }
kono
parents:
diff changeset
98 return fd.pfd.Write(fd.data.Write, b)
kono
parents:
diff changeset
99 }
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 func (fd *netFD) closeRead() error {
kono
parents:
diff changeset
102 if !fd.ok() {
kono
parents:
diff changeset
103 return syscall.EINVAL
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105 return syscall.EPLAN9
kono
parents:
diff changeset
106 }
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 func (fd *netFD) closeWrite() error {
kono
parents:
diff changeset
109 if !fd.ok() {
kono
parents:
diff changeset
110 return syscall.EINVAL
kono
parents:
diff changeset
111 }
kono
parents:
diff changeset
112 return syscall.EPLAN9
kono
parents:
diff changeset
113 }
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 func (fd *netFD) Close() error {
kono
parents:
diff changeset
116 if err := fd.pfd.Close(); err != nil {
kono
parents:
diff changeset
117 return err
kono
parents:
diff changeset
118 }
kono
parents:
diff changeset
119 if !fd.ok() {
kono
parents:
diff changeset
120 return syscall.EINVAL
kono
parents:
diff changeset
121 }
kono
parents:
diff changeset
122 if fd.net == "tcp" {
kono
parents:
diff changeset
123 // The following line is required to unblock Reads.
kono
parents:
diff changeset
124 _, err := fd.ctl.WriteString("close")
kono
parents:
diff changeset
125 if err != nil {
kono
parents:
diff changeset
126 return err
kono
parents:
diff changeset
127 }
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129 err := fd.ctl.Close()
kono
parents:
diff changeset
130 if fd.data != nil {
kono
parents:
diff changeset
131 if err1 := fd.data.Close(); err1 != nil && err == nil {
kono
parents:
diff changeset
132 err = err1
kono
parents:
diff changeset
133 }
kono
parents:
diff changeset
134 }
kono
parents:
diff changeset
135 if fd.listen != nil {
kono
parents:
diff changeset
136 if err1 := fd.listen.Close(); err1 != nil && err == nil {
kono
parents:
diff changeset
137 err = err1
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139 }
kono
parents:
diff changeset
140 fd.ctl = nil
kono
parents:
diff changeset
141 fd.data = nil
kono
parents:
diff changeset
142 fd.listen = nil
kono
parents:
diff changeset
143 return err
kono
parents:
diff changeset
144 }
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 // This method is only called via Conn.
kono
parents:
diff changeset
147 func (fd *netFD) dup() (*os.File, error) {
kono
parents:
diff changeset
148 if !fd.ok() || fd.data == nil {
kono
parents:
diff changeset
149 return nil, syscall.EINVAL
kono
parents:
diff changeset
150 }
kono
parents:
diff changeset
151 return fd.file(fd.data, fd.dir+"/data")
kono
parents:
diff changeset
152 }
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 func (l *TCPListener) dup() (*os.File, error) {
kono
parents:
diff changeset
155 if !l.fd.ok() {
kono
parents:
diff changeset
156 return nil, syscall.EINVAL
kono
parents:
diff changeset
157 }
kono
parents:
diff changeset
158 return l.fd.file(l.fd.ctl, l.fd.dir+"/ctl")
kono
parents:
diff changeset
159 }
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 func (fd *netFD) file(f *os.File, s string) (*os.File, error) {
kono
parents:
diff changeset
162 dfd, err := syscall.Dup(int(f.Fd()), -1)
kono
parents:
diff changeset
163 if err != nil {
kono
parents:
diff changeset
164 return nil, os.NewSyscallError("dup", err)
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166 return os.NewFile(uintptr(dfd), s), nil
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 func setReadBuffer(fd *netFD, bytes int) error {
kono
parents:
diff changeset
170 return syscall.EPLAN9
kono
parents:
diff changeset
171 }
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 func setWriteBuffer(fd *netFD, bytes int) error {
kono
parents:
diff changeset
174 return syscall.EPLAN9
kono
parents:
diff changeset
175 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
176
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
177 func (fd *netFD) SetDeadline(t time.Time) error {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
178 return fd.pfd.SetDeadline(t)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
179 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
180
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
181 func (fd *netFD) SetReadDeadline(t time.Time) error {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
182 return fd.pfd.SetReadDeadline(t)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
183 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
184
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
185 func (fd *netFD) SetWriteDeadline(t time.Time) error {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
186 return fd.pfd.SetWriteDeadline(t)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
187 }