Imported Upstream version 4.7.3
[platform/upstream/gcc48.git] / libgo / go / net / net_posix.go
1 // Copyright 2012 The Go Authors.  All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build darwin freebsd linux netbsd openbsd windows
6
7 // Base posix socket functions.
8
9 package net
10
11 import (
12         "os"
13         "syscall"
14         "time"
15 )
16
17 type conn struct {
18         fd *netFD
19 }
20
21 func (c *conn) ok() bool { return c != nil && c.fd != nil }
22
23 // Implementation of the Conn interface - see Conn for documentation.
24
25 // Read implements the Conn Read method.
26 func (c *conn) Read(b []byte) (int, error) {
27         if !c.ok() {
28                 return 0, syscall.EINVAL
29         }
30         return c.fd.Read(b)
31 }
32
33 // Write implements the Conn Write method.
34 func (c *conn) Write(b []byte) (int, error) {
35         if !c.ok() {
36                 return 0, syscall.EINVAL
37         }
38         return c.fd.Write(b)
39 }
40
41 // LocalAddr returns the local network address.
42 func (c *conn) LocalAddr() Addr {
43         if !c.ok() {
44                 return nil
45         }
46         return c.fd.laddr
47 }
48
49 // RemoteAddr returns the remote network address.
50 func (c *conn) RemoteAddr() Addr {
51         if !c.ok() {
52                 return nil
53         }
54         return c.fd.raddr
55 }
56
57 // SetDeadline implements the Conn SetDeadline method.
58 func (c *conn) SetDeadline(t time.Time) error {
59         if !c.ok() {
60                 return syscall.EINVAL
61         }
62         return setDeadline(c.fd, t)
63 }
64
65 // SetReadDeadline implements the Conn SetReadDeadline method.
66 func (c *conn) SetReadDeadline(t time.Time) error {
67         if !c.ok() {
68                 return syscall.EINVAL
69         }
70         return setReadDeadline(c.fd, t)
71 }
72
73 // SetWriteDeadline implements the Conn SetWriteDeadline method.
74 func (c *conn) SetWriteDeadline(t time.Time) error {
75         if !c.ok() {
76                 return syscall.EINVAL
77         }
78         return setWriteDeadline(c.fd, t)
79 }
80
81 // SetReadBuffer sets the size of the operating system's
82 // receive buffer associated with the connection.
83 func (c *conn) SetReadBuffer(bytes int) error {
84         if !c.ok() {
85                 return syscall.EINVAL
86         }
87         return setReadBuffer(c.fd, bytes)
88 }
89
90 // SetWriteBuffer sets the size of the operating system's
91 // transmit buffer associated with the connection.
92 func (c *conn) SetWriteBuffer(bytes int) error {
93         if !c.ok() {
94                 return syscall.EINVAL
95         }
96         return setWriteBuffer(c.fd, bytes)
97 }
98
99 // File returns a copy of the underlying os.File, set to blocking mode.
100 // It is the caller's responsibility to close f when finished.
101 // Closing c does not affect f, and closing f does not affect c.
102 func (c *conn) File() (f *os.File, err error) { return c.fd.dup() }
103
104 // Close closes the connection.
105 func (c *conn) Close() error {
106         if !c.ok() {
107                 return syscall.EINVAL
108         }
109         return c.fd.Close()
110 }