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 // The net package provides a portable interface to Unix | 5 // The net package provides a portable interface to Unix |
6 // networks sockets, including TCP/IP, UDP, domain name | 6 // networks sockets, including TCP/IP, UDP, domain name |
7 // resolution, and Unix domain sockets. | 7 // resolution, and Unix domain sockets. |
8 package net | 8 package net |
9 | 9 |
10 // TODO(rsc): | 10 // TODO(rsc): |
11 // support for raw ethernet sockets | 11 // support for raw ethernet sockets |
12 | 12 |
13 import "os" | 13 import "os" |
14 | 14 |
15 // Addr represents a network end point address. | 15 // Addr represents a network end point address. |
16 type Addr interface { | 16 type Addr interface { |
17 Network() string // name of the network | 17 Network() string // name of the network |
18 String() string // string form of address | 18 String() string // string form of address |
19 } | 19 } |
20 | 20 |
21 // Conn is a generic stream-oriented network connection. | 21 // Conn is a generic stream-oriented network connection. |
22 type Conn interface { | 22 type Conn interface { |
23 // Read reads data from the connection. | 23 // Read reads data from the connection. |
24 » // Read can be made to time out and return err == os.EAGAIN | 24 » // Read can be made to time out and return a net.Error with Timeout() ==
true |
25 // after a fixed time limit; see SetTimeout and SetReadTimeout. | 25 // after a fixed time limit; see SetTimeout and SetReadTimeout. |
26 Read(b []byte) (n int, err os.Error) | 26 Read(b []byte) (n int, err os.Error) |
27 | 27 |
28 // Write writes data to the connection. | 28 // Write writes data to the connection. |
29 » // Write can be made to time out and return err == os.EAGAIN | 29 » // Write can be made to time out and return a net.Error with Timeout() =
= true |
30 » // after a fixed time limit; see SetTimeout and SetReadTimeout. | 30 » // after a fixed time limit; see SetTimeout and SetWriteTimeout. |
31 Write(b []byte) (n int, err os.Error) | 31 Write(b []byte) (n int, err os.Error) |
32 | 32 |
33 // Close closes the connection. | 33 // Close closes the connection. |
| 34 // The error returned is an os.Error to satisfy io.Closer; |
34 Close() os.Error | 35 Close() os.Error |
35 | 36 |
36 // LocalAddr returns the local network address. | 37 // LocalAddr returns the local network address. |
37 LocalAddr() Addr | 38 LocalAddr() Addr |
38 | 39 |
39 // RemoteAddr returns the remote network address. | 40 // RemoteAddr returns the remote network address. |
40 RemoteAddr() Addr | 41 RemoteAddr() Addr |
41 | 42 |
42 // SetTimeout sets the read and write deadlines associated | 43 // SetTimeout sets the read and write deadlines associated |
43 // with the connection. | 44 // with the connection. |
44 SetTimeout(nsec int64) os.Error | 45 SetTimeout(nsec int64) os.Error |
45 | 46 |
46 // SetReadTimeout sets the time (in nanoseconds) that | 47 // SetReadTimeout sets the time (in nanoseconds) that |
47 » // Read will wait for data before returning os.EAGAIN. | 48 » // Read will wait for data before returning an error with Timeout() == t
rue. |
48 // Setting nsec == 0 (the default) disables the deadline. | 49 // Setting nsec == 0 (the default) disables the deadline. |
49 SetReadTimeout(nsec int64) os.Error | 50 SetReadTimeout(nsec int64) os.Error |
50 | 51 |
51 // SetWriteTimeout sets the time (in nanoseconds) that | 52 // SetWriteTimeout sets the time (in nanoseconds) that |
52 » // Write will wait to send its data before returning os.EAGAIN. | 53 » // Write will wait to send its data before returning an error with Timeo
ut() == true. |
53 // Setting nsec == 0 (the default) disables the deadline. | 54 // Setting nsec == 0 (the default) disables the deadline. |
54 // Even if write times out, it may return n > 0, indicating that | 55 // Even if write times out, it may return n > 0, indicating that |
55 // some of the data was successfully written. | 56 // some of the data was successfully written. |
56 SetWriteTimeout(nsec int64) os.Error | 57 SetWriteTimeout(nsec int64) os.Error |
| 58 } |
| 59 |
| 60 // An Error represents a network error. |
| 61 type Error interface { |
| 62 os.Error |
| 63 Timeout() bool // Is the error a timeout? |
| 64 Temporary() bool // Is the error temporary? |
57 } | 65 } |
58 | 66 |
59 // PacketConn is a generic packet-oriented network connection. | 67 // PacketConn is a generic packet-oriented network connection. |
60 type PacketConn interface { | 68 type PacketConn interface { |
61 // ReadFrom reads a packet from the connection, | 69 // ReadFrom reads a packet from the connection, |
62 // copying the payload into b. It returns the number of | 70 // copying the payload into b. It returns the number of |
63 // bytes copied into b and the return address that | 71 // bytes copied into b and the return address that |
64 // was on the packet. | 72 // was on the packet. |
65 » // ReadFrom can be made to time out and return err == os.EAGAIN | 73 » // ReadFrom can be made to time out and return |
66 » // after a fixed time limit; see SetTimeout and SetReadTimeout. | 74 » // an error with Timeout() == true after a fixed time limit; |
| 75 » // see SetTimeout and SetReadTimeout. |
67 ReadFrom(b []byte) (n int, addr Addr, err os.Error) | 76 ReadFrom(b []byte) (n int, addr Addr, err os.Error) |
68 | 77 |
69 // WriteTo writes a packet with payload b to addr. | 78 // WriteTo writes a packet with payload b to addr. |
70 » // WriteTo can be made to time out and return err == os.EAGAIN | 79 » // WriteTo can be made to time out and return |
71 » // after a fixed time limit; see SetTimeout and SetWriteTimeout. | 80 » // an error with Timeout() == true after a fixed time limit; |
| 81 » // see SetTimeout and SetWriteTimeout. |
72 // On packet-oriented connections, write timeouts are rare. | 82 // On packet-oriented connections, write timeouts are rare. |
73 WriteTo(b []byte, addr Addr) (n int, err os.Error) | 83 WriteTo(b []byte, addr Addr) (n int, err os.Error) |
74 | 84 |
75 // Close closes the connection. | 85 // Close closes the connection. |
| 86 // The error returned is an os.Error to satisfy io.Closer; |
76 Close() os.Error | 87 Close() os.Error |
77 | 88 |
78 // LocalAddr returns the local network address. | 89 // LocalAddr returns the local network address. |
79 LocalAddr() Addr | 90 LocalAddr() Addr |
80 | 91 |
81 // SetTimeout sets the read and write deadlines associated | 92 // SetTimeout sets the read and write deadlines associated |
82 // with the connection. | 93 // with the connection. |
83 SetTimeout(nsec int64) os.Error | 94 SetTimeout(nsec int64) os.Error |
84 | 95 |
85 // SetReadTimeout sets the time (in nanoseconds) that | 96 // SetReadTimeout sets the time (in nanoseconds) that |
86 » // Read will wait for data before returning os.EAGAIN. | 97 » // Read will wait for data before returning an error with Timeout() == t
rue. |
87 // Setting nsec == 0 (the default) disables the deadline. | 98 // Setting nsec == 0 (the default) disables the deadline. |
88 SetReadTimeout(nsec int64) os.Error | 99 SetReadTimeout(nsec int64) os.Error |
89 | 100 |
90 // SetWriteTimeout sets the time (in nanoseconds) that | 101 // SetWriteTimeout sets the time (in nanoseconds) that |
91 » // Write will wait to send its data before returning os.EAGAIN. | 102 » // Write will wait to send its data before returning an error with Timeo
ut() == true. |
92 // Setting nsec == 0 (the default) disables the deadline. | 103 // Setting nsec == 0 (the default) disables the deadline. |
93 // Even if write times out, it may return n > 0, indicating that | 104 // Even if write times out, it may return n > 0, indicating that |
94 // some of the data was successfully written. | 105 // some of the data was successfully written. |
95 SetWriteTimeout(nsec int64) os.Error | 106 SetWriteTimeout(nsec int64) os.Error |
96 } | 107 } |
97 | 108 |
98 // A Listener is a generic network listener for stream-oriented protocols. | 109 // A Listener is a generic network listener for stream-oriented protocols. |
99 // Accept waits for the next connection and Close closes the connection. | |
100 type Listener interface { | 110 type Listener interface { |
| 111 // Accept waits for and returns the next connection to the listener. |
101 Accept() (c Conn, err os.Error) | 112 Accept() (c Conn, err os.Error) |
| 113 |
| 114 // Close closes the listener. |
| 115 // The error returned is an os.Error to satisfy io.Closer; |
102 Close() os.Error | 116 Close() os.Error |
103 » Addr() Addr // Listener's network address | 117 |
| 118 » // Addr returns the listener's network address. |
| 119 » Addr() Addr |
104 } | 120 } |
105 | 121 |
106 // Dial connects to the remote address raddr on the network net. | 122 // Dial connects to the remote address raddr on the network net. |
107 // If the string laddr is not empty, it is used as the local address | 123 // If the string laddr is not empty, it is used as the local address |
108 // for the connection. | 124 // for the connection. |
109 // | 125 // |
110 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), | 126 // Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), |
111 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" | 127 // "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" |
112 // (IPv4-only) and "ip6" IPv6-only). | 128 // (IPv4-only) and "ip6" IPv6-only). |
113 // | 129 // |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 if e.Net != "" { | 305 if e.Net != "" { |
290 s += " " + e.Net | 306 s += " " + e.Net |
291 } | 307 } |
292 if e.Addr != nil { | 308 if e.Addr != nil { |
293 s += " " + e.Addr.String() | 309 s += " " + e.Addr.String() |
294 } | 310 } |
295 s += ": " + e.Error.String() | 311 s += ": " + e.Error.String() |
296 return s | 312 return s |
297 } | 313 } |
298 | 314 |
| 315 type temporary interface { |
| 316 Temporary() bool |
| 317 } |
| 318 |
| 319 func (e *OpError) Temporary() bool { |
| 320 t, ok := e.Error.(temporary) |
| 321 return ok && t.Temporary() |
| 322 } |
| 323 |
| 324 type timeout interface { |
| 325 Timeout() bool |
| 326 } |
| 327 |
| 328 func (e *OpError) Timeout() bool { |
| 329 t, ok := e.Error.(timeout) |
| 330 return ok && t.Timeout() |
| 331 } |
| 332 |
299 type AddrError struct { | 333 type AddrError struct { |
300 Error string | 334 Error string |
301 Addr string | 335 Addr string |
302 } | 336 } |
303 | 337 |
304 func (e *AddrError) String() string { | 338 func (e *AddrError) String() string { |
305 s := e.Error | 339 s := e.Error |
306 if e.Addr != "" { | 340 if e.Addr != "" { |
307 s += " " + e.Addr | 341 s += " " + e.Addr |
308 } | 342 } |
309 return s | 343 return s |
310 } | 344 } |
311 | 345 |
| 346 func (e *AddrError) Temporary() bool { |
| 347 return false |
| 348 } |
| 349 |
| 350 func (e *AddrError) Timeout() bool { |
| 351 return false |
| 352 } |
| 353 |
312 type UnknownNetworkError string | 354 type UnknownNetworkError string |
313 | 355 |
314 func (e UnknownNetworkError) String() string { return "unknown network " + strin
g(e) } | 356 func (e UnknownNetworkError) String() string { return "unknown network " + stri
ng(e) } |
| 357 func (e UnknownNetworkError) Temporary() bool { return false } |
| 358 func (e UnknownNetworkError) Timeout() bool { return false } |
LEFT | RIGHT |