diff options
Diffstat (limited to 'src/pkg/net/tcpsock_posix.go')
-rw-r--r-- | src/pkg/net/tcpsock_posix.go | 88 |
1 files changed, 54 insertions, 34 deletions
diff --git a/src/pkg/net/tcpsock_posix.go b/src/pkg/net/tcpsock_posix.go index 35d536c31..65ec49303 100644 --- a/src/pkg/net/tcpsock_posix.go +++ b/src/pkg/net/tcpsock_posix.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// +build darwin freebsd linux openbsd windows +// +build darwin freebsd linux netbsd openbsd windows // TCP sockets @@ -12,6 +12,7 @@ import ( "io" "os" "syscall" + "time" ) // BUG(rsc): On OpenBSD, listening on the "tcp" network does not listen for @@ -39,7 +40,7 @@ func (a *TCPAddr) family() int { return syscall.AF_INET6 } -func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, os.Error) { +func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, error) { return ipToSockaddr(family, a.IP, a.Port) } @@ -67,7 +68,7 @@ func (c *TCPConn) ok() bool { return c != nil && c.fd != nil } // Implementation of the Conn interface - see Conn for documentation. // Read implements the net.Conn Read method. -func (c *TCPConn) Read(b []byte) (n int, err os.Error) { +func (c *TCPConn) Read(b []byte) (n int, err error) { if !c.ok() { return 0, os.EINVAL } @@ -75,7 +76,7 @@ func (c *TCPConn) Read(b []byte) (n int, err os.Error) { } // ReadFrom implements the io.ReaderFrom ReadFrom method. -func (c *TCPConn) ReadFrom(r io.Reader) (int64, os.Error) { +func (c *TCPConn) ReadFrom(r io.Reader) (int64, error) { if n, err, handled := sendFile(c.fd, r); handled { return n, err } @@ -83,7 +84,7 @@ func (c *TCPConn) ReadFrom(r io.Reader) (int64, os.Error) { } // Write implements the net.Conn Write method. -func (c *TCPConn) Write(b []byte) (n int, err os.Error) { +func (c *TCPConn) Write(b []byte) (n int, err error) { if !c.ok() { return 0, os.EINVAL } @@ -91,7 +92,7 @@ func (c *TCPConn) Write(b []byte) (n int, err os.Error) { } // Close closes the TCP connection. -func (c *TCPConn) Close() os.Error { +func (c *TCPConn) Close() error { if !c.ok() { return os.EINVAL } @@ -100,6 +101,24 @@ func (c *TCPConn) Close() os.Error { return err } +// CloseRead shuts down the reading side of the TCP connection. +// Most callers should just use Close. +func (c *TCPConn) CloseRead() error { + if !c.ok() { + return os.EINVAL + } + return c.fd.CloseRead() +} + +// CloseWrite shuts down the writing side of the TCP connection. +// Most callers should just use Close. +func (c *TCPConn) CloseWrite() error { + if !c.ok() { + return os.EINVAL + } + return c.fd.CloseWrite() +} + // LocalAddr returns the local network address, a *TCPAddr. func (c *TCPConn) LocalAddr() Addr { if !c.ok() { @@ -116,33 +135,33 @@ func (c *TCPConn) RemoteAddr() Addr { return c.fd.raddr } -// SetTimeout implements the net.Conn SetTimeout method. -func (c *TCPConn) SetTimeout(nsec int64) os.Error { +// SetDeadline implements the net.Conn SetDeadline method. +func (c *TCPConn) SetDeadline(t time.Time) error { if !c.ok() { return os.EINVAL } - return setTimeout(c.fd, nsec) + return setDeadline(c.fd, t) } -// SetReadTimeout implements the net.Conn SetReadTimeout method. -func (c *TCPConn) SetReadTimeout(nsec int64) os.Error { +// SetReadDeadline implements the net.Conn SetReadDeadline method. +func (c *TCPConn) SetReadDeadline(t time.Time) error { if !c.ok() { return os.EINVAL } - return setReadTimeout(c.fd, nsec) + return setReadDeadline(c.fd, t) } -// SetWriteTimeout implements the net.Conn SetWriteTimeout method. -func (c *TCPConn) SetWriteTimeout(nsec int64) os.Error { +// SetWriteDeadline implements the net.Conn SetWriteDeadline method. +func (c *TCPConn) SetWriteDeadline(t time.Time) error { if !c.ok() { return os.EINVAL } - return setWriteTimeout(c.fd, nsec) + return setWriteDeadline(c.fd, t) } // SetReadBuffer sets the size of the operating system's // receive buffer associated with the connection. -func (c *TCPConn) SetReadBuffer(bytes int) os.Error { +func (c *TCPConn) SetReadBuffer(bytes int) error { if !c.ok() { return os.EINVAL } @@ -151,7 +170,7 @@ func (c *TCPConn) SetReadBuffer(bytes int) os.Error { // SetWriteBuffer sets the size of the operating system's // transmit buffer associated with the connection. -func (c *TCPConn) SetWriteBuffer(bytes int) os.Error { +func (c *TCPConn) SetWriteBuffer(bytes int) error { if !c.ok() { return os.EINVAL } @@ -169,7 +188,7 @@ func (c *TCPConn) SetWriteBuffer(bytes int) os.Error { // // If sec > 0, Close blocks for at most sec seconds waiting for // data to be sent and acknowledged. -func (c *TCPConn) SetLinger(sec int) os.Error { +func (c *TCPConn) SetLinger(sec int) error { if !c.ok() { return os.EINVAL } @@ -178,7 +197,7 @@ func (c *TCPConn) SetLinger(sec int) os.Error { // SetKeepAlive sets whether the operating system should send // keepalive messages on the connection. -func (c *TCPConn) SetKeepAlive(keepalive bool) os.Error { +func (c *TCPConn) SetKeepAlive(keepalive bool) error { if !c.ok() { return os.EINVAL } @@ -189,7 +208,7 @@ func (c *TCPConn) SetKeepAlive(keepalive bool) os.Error { // packet transmission in hopes of sending fewer packets // (Nagle's algorithm). The default is true (no delay), meaning // that data is sent as soon as possible after a Write. -func (c *TCPConn) SetNoDelay(noDelay bool) os.Error { +func (c *TCPConn) SetNoDelay(noDelay bool) error { if !c.ok() { return os.EINVAL } @@ -199,14 +218,14 @@ func (c *TCPConn) SetNoDelay(noDelay bool) os.Error { // File returns a copy of the underlying os.File, set to blocking mode. // It is the caller's responsibility to close f when finished. // Closing c does not affect f, and closing f does not affect c. -func (c *TCPConn) File() (f *os.File, err os.Error) { return c.fd.dup() } +func (c *TCPConn) File() (f *os.File, err error) { return c.fd.dup() } // DialTCP connects to the remote address raddr on the network net, // which must be "tcp", "tcp4", or "tcp6". If laddr is not nil, it is used // as the local address for the connection. -func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err os.Error) { +func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err error) { if raddr == nil { - return nil, &OpError{"dial", "tcp", nil, errMissingAddress} + return nil, &OpError{"dial", net, nil, errMissingAddress} } fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP) if e != nil { @@ -226,15 +245,15 @@ type TCPListener struct { // Net must be "tcp", "tcp4", or "tcp6". // If laddr has a port of 0, it means to listen on some available port. // The caller can use l.Addr() to retrieve the chosen address. -func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) { +func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err error) { fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP) if err != nil { return nil, err } - errno := syscall.Listen(fd.sysfd, listenBacklog()) - if errno != 0 { + err = syscall.Listen(fd.sysfd, listenerBacklog) + if err != nil { closesocket(fd.sysfd) - return nil, &OpError{"listen", "tcp", laddr, os.Errno(errno)} + return nil, &OpError{"listen", net, laddr, err} } l = new(TCPListener) l.fd = fd @@ -243,7 +262,7 @@ func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) { // AcceptTCP accepts the next incoming call and returns the new connection // and the remote address. -func (l *TCPListener) AcceptTCP() (c *TCPConn, err os.Error) { +func (l *TCPListener) AcceptTCP() (c *TCPConn, err error) { if l == nil || l.fd == nil || l.fd.sysfd < 0 { return nil, os.EINVAL } @@ -256,7 +275,7 @@ func (l *TCPListener) AcceptTCP() (c *TCPConn, err os.Error) { // Accept implements the Accept method in the Listener interface; // it waits for the next call and returns a generic Conn. -func (l *TCPListener) Accept() (c Conn, err os.Error) { +func (l *TCPListener) Accept() (c Conn, err error) { c1, err := l.AcceptTCP() if err != nil { return nil, err @@ -266,7 +285,7 @@ func (l *TCPListener) Accept() (c Conn, err os.Error) { // Close stops listening on the TCP address. // Already Accepted connections are not closed. -func (l *TCPListener) Close() os.Error { +func (l *TCPListener) Close() error { if l == nil || l.fd == nil { return os.EINVAL } @@ -276,15 +295,16 @@ func (l *TCPListener) Close() os.Error { // Addr returns the listener's network address, a *TCPAddr. func (l *TCPListener) Addr() Addr { return l.fd.laddr } -// SetTimeout sets the deadline associated with the listener -func (l *TCPListener) SetTimeout(nsec int64) os.Error { +// SetDeadline sets the deadline associated with the listener. +// A zero time value disables the deadline. +func (l *TCPListener) SetDeadline(t time.Time) error { if l == nil || l.fd == nil { return os.EINVAL } - return setTimeout(l.fd, nsec) + return setDeadline(l.fd, t) } // File returns a copy of the underlying os.File, set to blocking mode. // It is the caller's responsibility to close f when finished. // Closing c does not affect f, and closing f does not affect c. -func (l *TCPListener) File() (f *os.File, err os.Error) { return l.fd.dup() } +func (l *TCPListener) File() (f *os.File, err error) { return l.fd.dup() } |