LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // UDP sockets | 5 // UDP sockets |
6 | 6 |
7 package net | 7 package net |
8 | 8 |
9 import ( | 9 import ( |
10 "os" | 10 "os" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 type UDPConn struct { | 70 type UDPConn struct { |
71 fd *netFD | 71 fd *netFD |
72 } | 72 } |
73 | 73 |
74 func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{fd} } | 74 func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{fd} } |
75 | 75 |
76 func (c *UDPConn) ok() bool { return c != nil && c.fd != nil } | 76 func (c *UDPConn) ok() bool { return c != nil && c.fd != nil } |
77 | 77 |
78 // Implementation of the Conn interface - see Conn for documentation. | 78 // Implementation of the Conn interface - see Conn for documentation. |
79 | 79 |
80 // Read reads data from a single UDP packet on the connection. | 80 // Read implements the net.Conn Read method. |
81 // If the slice b is smaller than the arriving packet, | |
82 // the excess packet data may be discarded. | |
83 // | |
84 // Read can be made to time out and return err == os.EAGAIN | |
85 // after a fixed time limit; see SetTimeout and SetReadTimeout. | |
86 func (c *UDPConn) Read(b []byte) (n int, err os.Error) { | 81 func (c *UDPConn) Read(b []byte) (n int, err os.Error) { |
87 if !c.ok() { | 82 if !c.ok() { |
88 return 0, os.EINVAL | 83 return 0, os.EINVAL |
89 } | 84 } |
90 return c.fd.Read(b) | 85 return c.fd.Read(b) |
91 } | 86 } |
92 | 87 |
93 // Write writes data to the connection as a single UDP packet. | 88 // Write implements the net.Conn Write method. |
94 // | |
95 // Write can be made to time out and return err == os.EAGAIN | |
96 // after a fixed time limit; see SetTimeout and SetReadTimeout. | |
97 func (c *UDPConn) Write(b []byte) (n int, err os.Error) { | 89 func (c *UDPConn) Write(b []byte) (n int, err os.Error) { |
98 if !c.ok() { | 90 if !c.ok() { |
99 return 0, os.EINVAL | 91 return 0, os.EINVAL |
100 } | 92 } |
101 return c.fd.Write(b) | 93 return c.fd.Write(b) |
102 } | 94 } |
103 | 95 |
104 // Close closes the UDP connection. | 96 // Close closes the UDP connection. |
105 func (c *UDPConn) Close() os.Error { | 97 func (c *UDPConn) Close() os.Error { |
106 if !c.ok() { | 98 if !c.ok() { |
(...skipping 13 matching lines...) Expand all Loading... |
120 } | 112 } |
121 | 113 |
122 // RemoteAddr returns the remote network address, a *UDPAddr. | 114 // RemoteAddr returns the remote network address, a *UDPAddr. |
123 func (c *UDPConn) RemoteAddr() Addr { | 115 func (c *UDPConn) RemoteAddr() Addr { |
124 if !c.ok() { | 116 if !c.ok() { |
125 return nil | 117 return nil |
126 } | 118 } |
127 return c.fd.raddr | 119 return c.fd.raddr |
128 } | 120 } |
129 | 121 |
130 // SetTimeout sets the read and write deadlines associated | 122 // SetTimeout implements the net.Conn SetTimeout method. |
131 // with the connection. | |
132 func (c *UDPConn) SetTimeout(nsec int64) os.Error { | 123 func (c *UDPConn) SetTimeout(nsec int64) os.Error { |
133 if !c.ok() { | 124 if !c.ok() { |
134 return os.EINVAL | 125 return os.EINVAL |
135 } | 126 } |
136 return setTimeout(c.fd, nsec) | 127 return setTimeout(c.fd, nsec) |
137 } | 128 } |
138 | 129 |
139 // SetReadTimeout sets the time (in nanoseconds) that | 130 // SetReadTimeout implements the net.Conn SetReadTimeout method. |
140 // Read will wait for data before returning os.EAGAIN. | |
141 // Setting nsec == 0 (the default) disables the deadline. | |
142 func (c *UDPConn) SetReadTimeout(nsec int64) os.Error { | 131 func (c *UDPConn) SetReadTimeout(nsec int64) os.Error { |
143 if !c.ok() { | 132 if !c.ok() { |
144 return os.EINVAL | 133 return os.EINVAL |
145 } | 134 } |
146 return setReadTimeout(c.fd, nsec) | 135 return setReadTimeout(c.fd, nsec) |
147 } | 136 } |
148 | 137 |
149 // SetWriteTimeout sets the time (in nanoseconds) that | 138 // SetWriteTimeout implements the net.Conn SetWriteTimeout method. |
150 // Write will wait to send its data before returning os.EAGAIN. | |
151 // Setting nsec == 0 (the default) disables the deadline. | |
152 // Even if write times out, it may return n > 0, indicating that | |
153 // some of the data was successfully written. | |
154 func (c *UDPConn) SetWriteTimeout(nsec int64) os.Error { | 139 func (c *UDPConn) SetWriteTimeout(nsec int64) os.Error { |
155 if !c.ok() { | 140 if !c.ok() { |
156 return os.EINVAL | 141 return os.EINVAL |
157 } | 142 } |
158 return setWriteTimeout(c.fd, nsec) | 143 return setWriteTimeout(c.fd, nsec) |
159 } | 144 } |
160 | 145 |
161 // SetReadBuffer sets the size of the operating system's | 146 // SetReadBuffer sets the size of the operating system's |
162 // receive buffer associated with the connection. | 147 // receive buffer associated with the connection. |
163 func (c *UDPConn) SetReadBuffer(bytes int) os.Error { | 148 func (c *UDPConn) SetReadBuffer(bytes int) os.Error { |
(...skipping 11 matching lines...) Expand all Loading... |
175 } | 160 } |
176 return setWriteBuffer(c.fd, bytes) | 161 return setWriteBuffer(c.fd, bytes) |
177 } | 162 } |
178 | 163 |
179 // UDP-specific methods. | 164 // UDP-specific methods. |
180 | 165 |
181 // ReadFromUDP reads a UDP packet from c, copying the payload into b. | 166 // ReadFromUDP reads a UDP packet from c, copying the payload into b. |
182 // It returns the number of bytes copied into b and the return address | 167 // It returns the number of bytes copied into b and the return address |
183 // that was on the packet. | 168 // that was on the packet. |
184 // | 169 // |
185 // ReadFromUDP can be made to time out and return err == os.EAGAIN | 170 // ReadFromUDP can be made to time out and return an error with Timeout() == tru
e |
186 // after a fixed time limit; see SetTimeout and SetReadTimeout. | 171 // after a fixed time limit; see SetTimeout and SetReadTimeout. |
187 func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err os.Error) { | 172 func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err os.Error) { |
188 if !c.ok() { | 173 if !c.ok() { |
189 return 0, nil, os.EINVAL | 174 return 0, nil, os.EINVAL |
190 } | 175 } |
191 n, sa, err := c.fd.ReadFrom(b) | 176 n, sa, err := c.fd.ReadFrom(b) |
192 switch sa := sa.(type) { | 177 switch sa := sa.(type) { |
193 case *syscall.SockaddrInet4: | 178 case *syscall.SockaddrInet4: |
194 addr = &UDPAddr{&sa.Addr, sa.Port} | 179 addr = &UDPAddr{&sa.Addr, sa.Port} |
195 case *syscall.SockaddrInet6: | 180 case *syscall.SockaddrInet6: |
196 addr = &UDPAddr{&sa.Addr, sa.Port} | 181 addr = &UDPAddr{&sa.Addr, sa.Port} |
197 } | 182 } |
198 return | 183 return |
199 } | 184 } |
200 | 185 |
201 // ReadFrom reads a UDP packet from c, copying the payload into b. | 186 // ReadFrom implements the net.PacketConn ReadFrom method. |
202 // It returns the number of bytes copied into b and the return address | |
203 // that was on the packet. | |
204 // | |
205 // ReadFrom can be made to time out and return err == os.EAGAIN | |
206 // after a fixed time limit; see SetTimeout and SetReadTimeout. | |
207 func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) { | 187 func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) { |
208 if !c.ok() { | 188 if !c.ok() { |
209 return 0, nil, os.EINVAL | 189 return 0, nil, os.EINVAL |
210 } | 190 } |
211 n, uaddr, err := c.ReadFromUDP(b) | 191 n, uaddr, err := c.ReadFromUDP(b) |
212 return n, uaddr.toAddr(), err | 192 return n, uaddr.toAddr(), err |
213 } | 193 } |
214 | 194 |
215 // WriteToUDP writes a UDP packet to addr via c, copying the payload from b. | 195 // WriteToUDP writes a UDP packet to addr via c, copying the payload from b. |
216 // | 196 // |
217 // WriteToUDP can be made to time out and return err == os.EAGAIN | 197 // WriteToUDP can be made to time out and return |
218 // after a fixed time limit; see SetTimeout and SetWriteTimeout. | 198 // an error with Timeout() == true after a fixed time limit; |
219 // On packet-oriented connections such as UDP, write timeouts are rare. | 199 // see SetTimeout and SetWriteTimeout. |
| 200 // On packet-oriented connections, write timeouts are rare. |
220 func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err os.Error) { | 201 func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err os.Error) { |
221 if !c.ok() { | 202 if !c.ok() { |
222 return 0, os.EINVAL | 203 return 0, os.EINVAL |
223 } | 204 } |
224 » sa, err := addr.sockaddr(c.fd.family) | 205 » sa, err1 := addr.sockaddr(c.fd.family) |
225 » if err != nil { | 206 » if err1 != nil { |
226 » » return 0, err | 207 » » return 0, &OpError{Op: "write", Net: "udp", Addr: addr, Error: e
rr1} |
227 } | 208 } |
228 return c.fd.WriteTo(b, sa) | 209 return c.fd.WriteTo(b, sa) |
229 } | 210 } |
230 | 211 |
231 // WriteTo writes a UDP packet with payload b to addr via c. | 212 // WriteTo implements the net.PacketConn WriteTo method. |
232 // | |
233 // WriteTo can be made to time out and return err == os.EAGAIN | |
234 // after a fixed time limit; see SetTimeout and SetWriteTimeout. | |
235 // On packet-oriented connections such as UDP, write timeouts are rare. | |
236 func (c *UDPConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) { | 213 func (c *UDPConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) { |
237 if !c.ok() { | 214 if !c.ok() { |
238 return 0, os.EINVAL | 215 return 0, os.EINVAL |
239 } | 216 } |
240 a, ok := addr.(*UDPAddr) | 217 a, ok := addr.(*UDPAddr) |
241 if !ok { | 218 if !ok { |
242 return 0, &OpError{"writeto", "udp", addr, os.EINVAL} | 219 return 0, &OpError{"writeto", "udp", addr, os.EINVAL} |
243 } | 220 } |
244 return c.WriteToUDP(b, a) | 221 return c.WriteToUDP(b, a) |
245 } | 222 } |
(...skipping 29 matching lines...) Expand all Loading... |
275 } | 252 } |
276 if laddr == nil { | 253 if laddr == nil { |
277 return nil, &OpError{"listen", "udp", nil, errMissingAddress} | 254 return nil, &OpError{"listen", "udp", nil, errMissingAddress} |
278 } | 255 } |
279 fd, e := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_DGRAM, 0,
"dial", sockaddrToUDP) | 256 fd, e := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_DGRAM, 0,
"dial", sockaddrToUDP) |
280 if e != nil { | 257 if e != nil { |
281 return nil, e | 258 return nil, e |
282 } | 259 } |
283 return newUDPConn(fd), nil | 260 return newUDPConn(fd), nil |
284 } | 261 } |
LEFT | RIGHT |