Home | History | Annotate | Download | only in net
      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 package net
      6 
      7 import (
      8 	"context"
      9 	"io"
     10 	"os"
     11 )
     12 
     13 func (c *TCPConn) readFrom(r io.Reader) (int64, error) {
     14 	return genericReadFrom(c, r)
     15 }
     16 
     17 func dialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
     18 	if testHookDialTCP != nil {
     19 		return testHookDialTCP(ctx, net, laddr, raddr)
     20 	}
     21 	return doDialTCP(ctx, net, laddr, raddr)
     22 }
     23 
     24 func doDialTCP(ctx context.Context, net string, laddr, raddr *TCPAddr) (*TCPConn, error) {
     25 	switch net {
     26 	case "tcp", "tcp4", "tcp6":
     27 	default:
     28 		return nil, UnknownNetworkError(net)
     29 	}
     30 	if raddr == nil {
     31 		return nil, errMissingAddress
     32 	}
     33 	fd, err := dialPlan9(ctx, net, laddr, raddr)
     34 	if err != nil {
     35 		return nil, err
     36 	}
     37 	return newTCPConn(fd), nil
     38 }
     39 
     40 func (ln *TCPListener) ok() bool { return ln != nil && ln.fd != nil && ln.fd.ctl != nil }
     41 
     42 func (ln *TCPListener) accept() (*TCPConn, error) {
     43 	fd, err := ln.fd.acceptPlan9()
     44 	if err != nil {
     45 		return nil, err
     46 	}
     47 	return newTCPConn(fd), nil
     48 }
     49 
     50 func (ln *TCPListener) close() error {
     51 	if _, err := ln.fd.ctl.WriteString("hangup"); err != nil {
     52 		ln.fd.ctl.Close()
     53 		return err
     54 	}
     55 	if err := ln.fd.ctl.Close(); err != nil {
     56 		return err
     57 	}
     58 	return nil
     59 }
     60 
     61 func (ln *TCPListener) file() (*os.File, error) {
     62 	f, err := ln.dup()
     63 	if err != nil {
     64 		return nil, err
     65 	}
     66 	return f, nil
     67 }
     68 
     69 func listenTCP(ctx context.Context, network string, laddr *TCPAddr) (*TCPListener, error) {
     70 	fd, err := listenPlan9(ctx, network, laddr)
     71 	if err != nil {
     72 		return nil, err
     73 	}
     74 	return &TCPListener{fd}, nil
     75 }
     76