Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(140)

Side by Side Diff: src/pkg/net/tcpsock_posix.go

Issue 6525048: code review 6525048: net: protocol specific listen functions return a proper... (Closed)
Patch Set: diff -r 284f6a1e531b https://go.googlecode.com/hg/ Created 11 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 // +build darwin freebsd linux netbsd openbsd windows 5 // +build darwin freebsd linux netbsd openbsd windows
6 6
7 // TCP sockets 7 // TCP sockets
8 8
9 package net 9 package net
10 10
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 // (Nagle's algorithm). The default is true (no delay), meaning 133 // (Nagle's algorithm). The default is true (no delay), meaning
134 // that data is sent as soon as possible after a Write. 134 // that data is sent as soon as possible after a Write.
135 func (c *TCPConn) SetNoDelay(noDelay bool) error { 135 func (c *TCPConn) SetNoDelay(noDelay bool) error {
136 if !c.ok() { 136 if !c.ok() {
137 return syscall.EINVAL 137 return syscall.EINVAL
138 } 138 }
139 return setNoDelay(c.fd, noDelay) 139 return setNoDelay(c.fd, noDelay)
140 } 140 }
141 141
142 // DialTCP connects to the remote address raddr on the network net, 142 // DialTCP connects to the remote address raddr on the network net,
143 // which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is used 143 // which must be "tcp", "tcp4" or "tcp6". If laddr is not nil, it is
iant 2012/10/13 17:40:49 Do not remove the comma after "tcp4". It is corre
144 // as the local address for the connection. 144 // used as the local address for the connection.
145 func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) { 145 func DialTCP(net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
146 switch net {
147 case "tcp", "tcp4", "tcp6":
148 default:
149 return nil, UnknownNetworkError(net)
150 }
146 if raddr == nil { 151 if raddr == nil {
147 return nil, &OpError{"dial", net, nil, errMissingAddress} 152 return nil, &OpError{"dial", net, nil, errMissingAddress}
148 } 153 }
149 154
150 fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.S OCK_STREAM, 0, "dial", sockaddrToTCP) 155 fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.S OCK_STREAM, 0, "dial", sockaddrToTCP)
151 156
152 // TCP has a rarely used mechanism called a 'simultaneous connection' in 157 // TCP has a rarely used mechanism called a 'simultaneous connection' in
153 // which Dial("tcp", addr1, addr2) run on the machine at addr1 can 158 // which Dial("tcp", addr1, addr2) run on the machine at addr1 can
154 // connect to a simultaneous Dial("tcp", addr2, addr1) run on the machin e 159 // connect to a simultaneous Dial("tcp", addr2, addr1) run on the machin e
155 // at addr2, without either machine executing Listen. If laddr == nil, 160 // at addr2, without either machine executing Listen. If laddr == nil,
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 return ok && e.Err == syscall.EADDRNOTAVAIL 218 return ok && e.Err == syscall.EADDRNOTAVAIL
214 } 219 }
215 220
216 // TCPListener is a TCP network listener. 221 // TCPListener is a TCP network listener.
217 // Clients should typically use variables of type Listener 222 // Clients should typically use variables of type Listener
218 // instead of assuming TCP. 223 // instead of assuming TCP.
219 type TCPListener struct { 224 type TCPListener struct {
220 fd *netFD 225 fd *netFD
221 } 226 }
222 227
223 // ListenTCP announces on the TCP address laddr and returns a TCP listener. 228 // ListenTCP announces on the TCP address laddr on the network net,
224 // Net must be "tcp", "tcp4", or "tcp6". 229 // which must be "tcp", "tcp4" or "tcp6" and returns a TCP listener.
iant 2012/10/13 17:40:49 Need comma after "tcp6".
225 // If laddr has a port of 0, it means to listen on some available port. 230 // If laddr has a port of 0, it means to listen on some available
226 // The caller can use l.Addr() to retrieve the chosen address. 231 // port. The caller can use Addr() of TCPListener to retrieve the
232 // chosen address.
227 func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) { 233 func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error) {
234 switch net {
235 case "tcp", "tcp4", "tcp6":
236 default:
237 return nil, UnknownNetworkError(net)
238 }
239 if laddr == nil {
240 return nil, &OpError{"listen", net, nil, errMissingAddress}
241 }
228 fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP) 242 fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP)
229 if err != nil { 243 if err != nil {
230 return nil, err 244 return nil, err
231 } 245 }
232 err = syscall.Listen(fd.sysfd, listenerBacklog) 246 err = syscall.Listen(fd.sysfd, listenerBacklog)
233 if err != nil { 247 if err != nil {
234 closesocket(fd.sysfd) 248 closesocket(fd.sysfd)
235 return nil, &OpError{"listen", net, laddr, err} 249 return nil, &OpError{"listen", net, laddr, err}
236 } 250 }
237 l := new(TCPListener) 251 l := new(TCPListener)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 if l == nil || l.fd == nil { 294 if l == nil || l.fd == nil {
281 return syscall.EINVAL 295 return syscall.EINVAL
282 } 296 }
283 return setDeadline(l.fd, t) 297 return setDeadline(l.fd, t)
284 } 298 }
285 299
286 // File returns a copy of the underlying os.File, set to blocking mode. 300 // File returns a copy of the underlying os.File, set to blocking mode.
287 // It is the caller's responsibility to close f when finished. 301 // It is the caller's responsibility to close f when finished.
288 // Closing l does not affect f, and closing f does not affect l. 302 // Closing l does not affect f, and closing f does not affect l.
289 func (l *TCPListener) File() (f *os.File, err error) { return l.fd.dup() } 303 func (l *TCPListener) File() (f *os.File, err error) { return l.fd.dup() }
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b