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 // TCP sockets | 5 // TCP sockets |
6 | 6 |
7 package net | 7 package net |
8 | 8 |
9 import ( | 9 import ( |
10 "os" | 10 "os" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 func newTCPConn(fd *netFD) *TCPConn { | 74 func newTCPConn(fd *netFD) *TCPConn { |
75 c := &TCPConn{fd} | 75 c := &TCPConn{fd} |
76 setsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1) | 76 setsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1) |
77 return c | 77 return c |
78 } | 78 } |
79 | 79 |
80 func (c *TCPConn) ok() bool { return c != nil && c.fd != nil } | 80 func (c *TCPConn) ok() bool { return c != nil && c.fd != nil } |
81 | 81 |
82 // Implementation of the Conn interface - see Conn for documentation. | 82 // Implementation of the Conn interface - see Conn for documentation. |
83 | 83 |
84 // Read reads data from the TCP connection. | 84 // Read implements the net.Conn Read method. |
85 // | |
86 // Read can be made to time out and return err == os.EAGAIN | |
87 // after a fixed time limit; see SetTimeout and SetReadTimeout. | |
88 func (c *TCPConn) Read(b []byte) (n int, err os.Error) { | 85 func (c *TCPConn) Read(b []byte) (n int, err os.Error) { |
89 if !c.ok() { | 86 if !c.ok() { |
90 return 0, os.EINVAL | 87 return 0, os.EINVAL |
91 } | 88 } |
92 return c.fd.Read(b) | 89 return c.fd.Read(b) |
93 } | 90 } |
94 | 91 |
95 // Write writes data to the TCP connection. | 92 // Write implements the net.Conn Write method. |
96 // | |
97 // Write can be made to time out and return err == os.EAGAIN | |
98 // after a fixed time limit; see SetTimeout and SetReadTimeout. | |
99 func (c *TCPConn) Write(b []byte) (n int, err os.Error) { | 93 func (c *TCPConn) Write(b []byte) (n int, err os.Error) { |
100 if !c.ok() { | 94 if !c.ok() { |
101 return 0, os.EINVAL | 95 return 0, os.EINVAL |
102 } | 96 } |
103 return c.fd.Write(b) | 97 return c.fd.Write(b) |
104 } | 98 } |
105 | 99 |
106 // Close closes the TCP connection. | 100 // Close closes the TCP connection. |
107 func (c *TCPConn) Close() os.Error { | 101 func (c *TCPConn) Close() os.Error { |
108 if !c.ok() { | 102 if !c.ok() { |
(...skipping 13 matching lines...) Expand all Loading... |
122 } | 116 } |
123 | 117 |
124 // RemoteAddr returns the remote network address, a *TCPAddr. | 118 // RemoteAddr returns the remote network address, a *TCPAddr. |
125 func (c *TCPConn) RemoteAddr() Addr { | 119 func (c *TCPConn) RemoteAddr() Addr { |
126 if !c.ok() { | 120 if !c.ok() { |
127 return nil | 121 return nil |
128 } | 122 } |
129 return c.fd.raddr | 123 return c.fd.raddr |
130 } | 124 } |
131 | 125 |
132 // SetTimeout sets the read and write deadlines associated | 126 // SetTimeout implements the net.Conn SetTimeout method. |
133 // with the connection. | |
134 func (c *TCPConn) SetTimeout(nsec int64) os.Error { | 127 func (c *TCPConn) SetTimeout(nsec int64) os.Error { |
135 if !c.ok() { | 128 if !c.ok() { |
136 return os.EINVAL | 129 return os.EINVAL |
137 } | 130 } |
138 return setTimeout(c.fd, nsec) | 131 return setTimeout(c.fd, nsec) |
139 } | 132 } |
140 | 133 |
141 // SetReadTimeout sets the time (in nanoseconds) that | 134 // SetReadTimeout implements the net.Conn SetReadTimeout method. |
142 // Read will wait for data before returning os.EAGAIN. | |
143 // Setting nsec == 0 (the default) disables the deadline. | |
144 func (c *TCPConn) SetReadTimeout(nsec int64) os.Error { | 135 func (c *TCPConn) SetReadTimeout(nsec int64) os.Error { |
145 if !c.ok() { | 136 if !c.ok() { |
146 return os.EINVAL | 137 return os.EINVAL |
147 } | 138 } |
148 return setReadTimeout(c.fd, nsec) | 139 return setReadTimeout(c.fd, nsec) |
149 } | 140 } |
150 | 141 |
151 // SetWriteTimeout sets the time (in nanoseconds) that | 142 // SetWriteTimeout implements the net.Conn SetWriteTimeout method. |
152 // Write will wait to send its data before returning os.EAGAIN. | |
153 // Setting nsec == 0 (the default) disables the deadline. | |
154 // Even if write times out, it may return n > 0, indicating that | |
155 // some of the data was successfully written. | |
156 func (c *TCPConn) SetWriteTimeout(nsec int64) os.Error { | 143 func (c *TCPConn) SetWriteTimeout(nsec int64) os.Error { |
157 if !c.ok() { | 144 if !c.ok() { |
158 return os.EINVAL | 145 return os.EINVAL |
159 } | 146 } |
160 return setWriteTimeout(c.fd, nsec) | 147 return setWriteTimeout(c.fd, nsec) |
161 } | 148 } |
162 | 149 |
163 // SetReadBuffer sets the size of the operating system's | 150 // SetReadBuffer sets the size of the operating system's |
164 // receive buffer associated with the connection. | 151 // receive buffer associated with the connection. |
165 func (c *TCPConn) SetReadBuffer(bytes int) os.Error { | 152 func (c *TCPConn) SetReadBuffer(bytes int) os.Error { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 // Already Accepted connections are not closed. | 258 // Already Accepted connections are not closed. |
272 func (l *TCPListener) Close() os.Error { | 259 func (l *TCPListener) Close() os.Error { |
273 if l == nil || l.fd == nil { | 260 if l == nil || l.fd == nil { |
274 return os.EINVAL | 261 return os.EINVAL |
275 } | 262 } |
276 return l.fd.Close() | 263 return l.fd.Close() |
277 } | 264 } |
278 | 265 |
279 // Addr returns the listener's network address, a *TCPAddr. | 266 // Addr returns the listener's network address, a *TCPAddr. |
280 func (l *TCPListener) Addr() Addr { return l.fd.laddr } | 267 func (l *TCPListener) Addr() Addr { return l.fd.laddr } |
LEFT | RIGHT |