2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2011-08-17 11:28:29 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2012-11-08 09:35:16 -07:00
|
|
|
import (
|
2016-04-14 18:47:25 -06:00
|
|
|
"context"
|
2012-11-13 00:18:37 -07:00
|
|
|
"io"
|
|
|
|
"os"
|
2012-11-08 09:35:16 -07:00
|
|
|
)
|
2011-08-17 11:28:29 -06:00
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
|
|
|
|
return genericReadFrom(c, r)
|
2012-11-08 09:35:16 -07:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func dialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
|
|
if testHookDialTCP != nil {
|
|
|
|
return testHookDialTCP(ctx, net, laddr, raddr)
|
|
|
|
}
|
|
|
|
return doDialTCP(ctx, net, laddr, raddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doDialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
|
|
|
|
if d, _ := ctx.Deadline(); !d.IsZero() {
|
2016-04-15 20:36:42 -06:00
|
|
|
// TODO: deadline not implemented on Plan 9 (see golang.og/issue/11932)
|
2012-11-08 09:35:16 -07:00
|
|
|
}
|
2015-12-14 15:21:48 -07:00
|
|
|
// TODO(bradfitz,0intro): also use the cancel channel.
|
2011-08-17 11:28:29 -06:00
|
|
|
switch net {
|
|
|
|
case "tcp", "tcp4", "tcp6":
|
|
|
|
default:
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, UnknownNetworkError(net)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
if raddr == nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, errMissingAddress
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
fd, err := dialPlan9(net, laddr, raddr)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2012-11-13 00:18:37 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-02-19 18:11:17 -07:00
|
|
|
return newTCPConn(fd), nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil && ln.fd.ctl != nil }
|
2011-08-17 11:28:29 -06:00
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) accept() (*TCPConn, error) {
|
|
|
|
fd, err := ln.fd.acceptPlan9()
|
2012-11-13 00:18:37 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newTCPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) close() error {
|
|
|
|
if _, err := ln.fd.ctl.WriteString("hangup"); err != nil {
|
|
|
|
ln.fd.ctl.Close()
|
|
|
|
return err
|
2012-06-06 16:38:56 -06:00
|
|
|
}
|
2016-03-02 04:08:18 -07:00
|
|
|
if err := ln.fd.ctl.Close(); err != nil {
|
|
|
|
return err
|
2015-04-19 04:01:49 -06:00
|
|
|
}
|
|
|
|
return nil
|
2012-11-13 00:18:37 -07:00
|
|
|
}
|
|
|
|
|
2016-03-02 04:08:18 -07:00
|
|
|
func (ln *TCPListener) file() (*os.File, error) {
|
|
|
|
f, err := ln.dup()
|
2015-04-18 01:53:55 -06:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, err
|
2015-04-18 01:53:55 -06:00
|
|
|
}
|
2016-03-02 04:08:18 -07:00
|
|
|
return f, nil
|
2015-04-18 01:53:55 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func listenTCP(ctx context.Context, network string, laddr *TCPAddr) (*TCPListener, error) {
|
2016-03-02 04:08:18 -07:00
|
|
|
fd, err := listenPlan9(network, laddr)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2012-11-13 00:18:37 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-11-13 00:18:37 -07:00
|
|
|
return &TCPListener{fd}, nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|