OSDN Git Service

Remove the types float and complex.
[pf3gnuchains/gcc-fork.git] / libgo / go / net / tcpsock.go
1 // Copyright 2009 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 // TCP sockets
6
7 package net
8
9 import (
10         "os"
11         "syscall"
12 )
13
14 func sockaddrToTCP(sa syscall.Sockaddr) Addr {
15         switch sa := sa.(type) {
16         case *syscall.SockaddrInet4:
17                 return &TCPAddr{sa.Addr[0:], sa.Port}
18         case *syscall.SockaddrInet6:
19                 return &TCPAddr{sa.Addr[0:], sa.Port}
20         }
21         return nil
22 }
23
24 // TCPAddr represents the address of a TCP end point.
25 type TCPAddr struct {
26         IP   IP
27         Port int
28 }
29
30 // Network returns the address's network name, "tcp".
31 func (a *TCPAddr) Network() string { return "tcp" }
32
33 func (a *TCPAddr) String() string {
34         if a == nil {
35                 return "<nil>"
36         }
37         return joinHostPort(a.IP.String(), itoa(a.Port))
38 }
39
40 func (a *TCPAddr) family() int {
41         if a == nil || len(a.IP) <= 4 {
42                 return syscall.AF_INET
43         }
44         if ip := a.IP.To4(); ip != nil {
45                 return syscall.AF_INET
46         }
47         return syscall.AF_INET6
48 }
49
50 func (a *TCPAddr) sockaddr(family int) (syscall.Sockaddr, os.Error) {
51         return ipToSockaddr(family, a.IP, a.Port)
52 }
53
54 func (a *TCPAddr) toAddr() sockaddr {
55         if a == nil { // nil *TCPAddr
56                 return nil // nil interface
57         }
58         return a
59 }
60
61 // ResolveTCPAddr parses addr as a TCP address of the form
62 // host:port and resolves domain names or port names to
63 // numeric addresses.  A literal IPv6 host address must be
64 // enclosed in square brackets, as in "[::]:80".
65 func ResolveTCPAddr(addr string) (*TCPAddr, os.Error) {
66         ip, port, err := hostPortToIP("tcp", addr)
67         if err != nil {
68                 return nil, err
69         }
70         return &TCPAddr{ip, port}, nil
71 }
72
73 // TCPConn is an implementation of the Conn interface
74 // for TCP network connections.
75 type TCPConn struct {
76         fd *netFD
77 }
78
79 func newTCPConn(fd *netFD) *TCPConn {
80         c := &TCPConn{fd}
81         c.SetNoDelay(true)
82         return c
83 }
84
85 func (c *TCPConn) ok() bool { return c != nil && c.fd != nil }
86
87 // Implementation of the Conn interface - see Conn for documentation.
88
89 // Read implements the net.Conn Read method.
90 func (c *TCPConn) Read(b []byte) (n int, err os.Error) {
91         if !c.ok() {
92                 return 0, os.EINVAL
93         }
94         return c.fd.Read(b)
95 }
96
97 // Write implements the net.Conn Write method.
98 func (c *TCPConn) Write(b []byte) (n int, err os.Error) {
99         if !c.ok() {
100                 return 0, os.EINVAL
101         }
102         return c.fd.Write(b)
103 }
104
105 // Close closes the TCP connection.
106 func (c *TCPConn) Close() os.Error {
107         if !c.ok() {
108                 return os.EINVAL
109         }
110         err := c.fd.Close()
111         c.fd = nil
112         return err
113 }
114
115 // LocalAddr returns the local network address, a *TCPAddr.
116 func (c *TCPConn) LocalAddr() Addr {
117         if !c.ok() {
118                 return nil
119         }
120         return c.fd.laddr
121 }
122
123 // RemoteAddr returns the remote network address, a *TCPAddr.
124 func (c *TCPConn) RemoteAddr() Addr {
125         if !c.ok() {
126                 return nil
127         }
128         return c.fd.raddr
129 }
130
131 // SetTimeout implements the net.Conn SetTimeout method.
132 func (c *TCPConn) SetTimeout(nsec int64) os.Error {
133         if !c.ok() {
134                 return os.EINVAL
135         }
136         return setTimeout(c.fd, nsec)
137 }
138
139 // SetReadTimeout implements the net.Conn SetReadTimeout method.
140 func (c *TCPConn) SetReadTimeout(nsec int64) os.Error {
141         if !c.ok() {
142                 return os.EINVAL
143         }
144         return setReadTimeout(c.fd, nsec)
145 }
146
147 // SetWriteTimeout implements the net.Conn SetWriteTimeout method.
148 func (c *TCPConn) SetWriteTimeout(nsec int64) os.Error {
149         if !c.ok() {
150                 return os.EINVAL
151         }
152         return setWriteTimeout(c.fd, nsec)
153 }
154
155 // SetReadBuffer sets the size of the operating system's
156 // receive buffer associated with the connection.
157 func (c *TCPConn) SetReadBuffer(bytes int) os.Error {
158         if !c.ok() {
159                 return os.EINVAL
160         }
161         return setReadBuffer(c.fd, bytes)
162 }
163
164 // SetWriteBuffer sets the size of the operating system's
165 // transmit buffer associated with the connection.
166 func (c *TCPConn) SetWriteBuffer(bytes int) os.Error {
167         if !c.ok() {
168                 return os.EINVAL
169         }
170         return setWriteBuffer(c.fd, bytes)
171 }
172
173 // SetLinger sets the behavior of Close() on a connection
174 // which still has data waiting to be sent or to be acknowledged.
175 //
176 // If sec < 0 (the default), Close returns immediately and
177 // the operating system finishes sending the data in the background.
178 //
179 // If sec == 0, Close returns immediately and the operating system
180 // discards any unsent or unacknowledged data.
181 //
182 // If sec > 0, Close blocks for at most sec seconds waiting for
183 // data to be sent and acknowledged.
184 func (c *TCPConn) SetLinger(sec int) os.Error {
185         if !c.ok() {
186                 return os.EINVAL
187         }
188         return setLinger(c.fd, sec)
189 }
190
191 // SetKeepAlive sets whether the operating system should send
192 // keepalive messages on the connection.
193 func (c *TCPConn) SetKeepAlive(keepalive bool) os.Error {
194         if !c.ok() {
195                 return os.EINVAL
196         }
197         return setKeepAlive(c.fd, keepalive)
198 }
199
200 // SetNoDelay controls whether the operating system should delay
201 // packet transmission in hopes of sending fewer packets
202 // (Nagle's algorithm).  The default is true (no delay), meaning
203 // that data is sent as soon as possible after a Write.
204 func (c *TCPConn) SetNoDelay(noDelay bool) os.Error {
205         if !c.ok() {
206                 return os.EINVAL
207         }
208         return setNoDelay(c.fd, noDelay)
209 }
210
211 // File returns a copy of the underlying os.File, set to blocking mode.
212 // It is the caller's responsibility to close f when finished.
213 // Closing c does not affect f, and closing f does not affect c.
214 func (c *TCPConn) File() (f *os.File, err os.Error) { return c.fd.dup() }
215
216 // DialTCP is like Dial but can only connect to TCP networks
217 // and returns a TCPConn structure.
218 func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err os.Error) {
219         if raddr == nil {
220                 return nil, &OpError{"dial", "tcp", nil, errMissingAddress}
221         }
222         fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_STREAM, 0, "dial", sockaddrToTCP)
223         if e != nil {
224                 return nil, e
225         }
226         return newTCPConn(fd), nil
227 }
228
229 // TCPListener is a TCP network listener.
230 // Clients should typically use variables of type Listener
231 // instead of assuming TCP.
232 type TCPListener struct {
233         fd *netFD
234 }
235
236 // ListenTCP announces on the TCP address laddr and returns a TCP listener.
237 // Net must be "tcp", "tcp4", or "tcp6".
238 // If laddr has a port of 0, it means to listen on some available port.
239 // The caller can use l.Addr() to retrieve the chosen address.
240 func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err os.Error) {
241         fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_STREAM, 0, "listen", sockaddrToTCP)
242         if err != nil {
243                 return nil, err
244         }
245         errno := syscall.Listen(fd.sysfd, listenBacklog())
246         if errno != 0 {
247                 closesocket(fd.sysfd)
248                 return nil, &OpError{"listen", "tcp", laddr, os.Errno(errno)}
249         }
250         l = new(TCPListener)
251         l.fd = fd
252         return l, nil
253 }
254
255 // AcceptTCP accepts the next incoming call and returns the new connection
256 // and the remote address.
257 func (l *TCPListener) AcceptTCP() (c *TCPConn, err os.Error) {
258         if l == nil || l.fd == nil || l.fd.sysfd < 0 {
259                 return nil, os.EINVAL
260         }
261         fd, err := l.fd.accept(sockaddrToTCP)
262         if err != nil {
263                 return nil, err
264         }
265         return newTCPConn(fd), nil
266 }
267
268 // Accept implements the Accept method in the Listener interface;
269 // it waits for the next call and returns a generic Conn.
270 func (l *TCPListener) Accept() (c Conn, err os.Error) {
271         c1, err := l.AcceptTCP()
272         if err != nil {
273                 return nil, err
274         }
275         return c1, nil
276 }
277
278 // Close stops listening on the TCP address.
279 // Already Accepted connections are not closed.
280 func (l *TCPListener) Close() os.Error {
281         if l == nil || l.fd == nil {
282                 return os.EINVAL
283         }
284         return l.fd.Close()
285 }
286
287 // Addr returns the listener's network address, a *TCPAddr.
288 func (l *TCPListener) Addr() Addr { return l.fd.laddr }
289
290 // File returns a copy of the underlying os.File, set to blocking mode.
291 // It is the caller's responsibility to close f when finished.
292 // Closing c does not affect f, and closing f does not affect c.
293 func (l *TCPListener) File() (f *os.File, err os.Error) { return l.fd.dup() }