| // Copyright 2012 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // +build darwin freebsd linux netbsd openbsd windows |
| |
| // Base posix socket functions. |
| |
| package net |
| |
| import ( |
| "os" |
| "syscall" |
| "time" |
| ) |
| |
| type conn struct { |
| fd *netFD |
| } |
| |
| func (c *conn) ok() bool { return c != nil && c.fd != nil } |
| |
| // Implementation of the Conn interface - see Conn for documentation. |
| |
| // Read implements the Conn Read method. |
| func (c *conn) Read(b []byte) (int, error) { |
| if !c.ok() { |
| return 0, syscall.EINVAL |
| } |
| return c.fd.Read(b) |
| } |
| |
| // Write implements the Conn Write method. |
| func (c *conn) Write(b []byte) (int, error) { |
| if !c.ok() { |
| return 0, syscall.EINVAL |
| } |
| return c.fd.Write(b) |
| } |
| |
| // LocalAddr returns the local network address. |
| func (c *conn) LocalAddr() Addr { |
| if !c.ok() { |
| return nil |
| } |
| return c.fd.laddr |
| } |
| |
| // RemoteAddr returns the remote network address. |
| func (c *conn) RemoteAddr() Addr { |
| if !c.ok() { |
| return nil |
| } |
| return c.fd.raddr |
| } |
| |
| // SetDeadline implements the Conn SetDeadline method. |
| func (c *conn) SetDeadline(t time.Time) error { |
| if !c.ok() { |
| return syscall.EINVAL |
| } |
| return setDeadline(c.fd, t) |
| } |
| |
| // SetReadDeadline implements the Conn SetReadDeadline method. |
| func (c *conn) SetReadDeadline(t time.Time) error { |
| if !c.ok() { |
| return syscall.EINVAL |
| } |
| return setReadDeadline(c.fd, t) |
| } |
| |
| // SetWriteDeadline implements the Conn SetWriteDeadline method. |
| func (c *conn) SetWriteDeadline(t time.Time) error { |
| if !c.ok() { |
| return syscall.EINVAL |
| } |
| return setWriteDeadline(c.fd, t) |
| } |
| |
| // SetReadBuffer sets the size of the operating system's |
| // receive buffer associated with the connection. |
| func (c *conn) SetReadBuffer(bytes int) error { |
| if !c.ok() { |
| return syscall.EINVAL |
| } |
| return setReadBuffer(c.fd, bytes) |
| } |
| |
| // SetWriteBuffer sets the size of the operating system's |
| // transmit buffer associated with the connection. |
| func (c *conn) SetWriteBuffer(bytes int) error { |
| if !c.ok() { |
| return syscall.EINVAL |
| } |
| return setWriteBuffer(c.fd, bytes) |
| } |
| |
| // 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 *conn) File() (f *os.File, err error) { return c.fd.dup() } |
| |
| // Close closes the connection. |
| func (c *conn) Close() error { |
| if !c.ok() { |
| return syscall.EINVAL |
| } |
| return c.fd.Close() |
| } |