tcpsock_plan9.go 2.44 KB
Newer Older
Fazlul Shahriar's avatar
Fazlul Shahriar committed
1 2 3 4 5 6 7 8 9
// Copyright 2009 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.

// TCP for Plan 9

package net

import (
10
	"syscall"
Mikio Hara's avatar
Mikio Hara committed
11
	"time"
Fazlul Shahriar's avatar
Fazlul Shahriar committed
12 13 14 15 16 17 18 19
)

// TCPConn is an implementation of the Conn interface
// for TCP network connections.
type TCPConn struct {
	plan9Conn
}

20
// SetDeadline implements the Conn SetDeadline method.
Mikio Hara's avatar
Mikio Hara committed
21
func (c *TCPConn) SetDeadline(t time.Time) error {
22
	return syscall.EPLAN9
Mikio Hara's avatar
Mikio Hara committed
23 24
}

25
// SetReadDeadline implements the Conn SetReadDeadline method.
Mikio Hara's avatar
Mikio Hara committed
26
func (c *TCPConn) SetReadDeadline(t time.Time) error {
27
	return syscall.EPLAN9
Mikio Hara's avatar
Mikio Hara committed
28 29
}

30
// SetWriteDeadline implements the Conn SetWriteDeadline method.
Mikio Hara's avatar
Mikio Hara committed
31
func (c *TCPConn) SetWriteDeadline(t time.Time) error {
32
	return syscall.EPLAN9
Mikio Hara's avatar
Mikio Hara committed
33 34
}

Fazlul Shahriar's avatar
Fazlul Shahriar committed
35 36
// CloseRead shuts down the reading side of the TCP connection.
// Most callers should just use Close.
37
func (c *TCPConn) CloseRead() error {
Fazlul Shahriar's avatar
Fazlul Shahriar committed
38
	if !c.ok() {
Rob Pike's avatar
Rob Pike committed
39
		return syscall.EINVAL
Fazlul Shahriar's avatar
Fazlul Shahriar committed
40
	}
41
	return syscall.EPLAN9
Fazlul Shahriar's avatar
Fazlul Shahriar committed
42 43 44 45
}

// CloseWrite shuts down the writing side of the TCP connection.
// Most callers should just use Close.
46
func (c *TCPConn) CloseWrite() error {
Fazlul Shahriar's avatar
Fazlul Shahriar committed
47
	if !c.ok() {
Rob Pike's avatar
Rob Pike committed
48
		return syscall.EINVAL
Fazlul Shahriar's avatar
Fazlul Shahriar committed
49
	}
50
	return syscall.EPLAN9
Fazlul Shahriar's avatar
Fazlul Shahriar committed
51 52
}

Fazlul Shahriar's avatar
Fazlul Shahriar committed
53 54 55
// DialTCP connects to the remote address raddr on the network net,
// which must be "tcp", "tcp4", or "tcp6".  If laddr is not nil, it is used
// as the local address for the connection.
56
func DialTCP(net string, laddr, raddr *TCPAddr) (c *TCPConn, err error) {
Fazlul Shahriar's avatar
Fazlul Shahriar committed
57 58 59 60 61 62
	switch net {
	case "tcp", "tcp4", "tcp6":
	default:
		return nil, UnknownNetworkError(net)
	}
	if raddr == nil {
Mikio Hara's avatar
Mikio Hara committed
63
		return nil, &OpError{"dial", net, nil, errMissingAddress}
Fazlul Shahriar's avatar
Fazlul Shahriar committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	}
	c1, err := dialPlan9(net, laddr, raddr)
	if err != nil {
		return
	}
	return &TCPConn{*c1}, nil
}

// TCPListener is a TCP network listener.
// Clients should typically use variables of type Listener
// instead of assuming TCP.
type TCPListener struct {
	plan9Listener
}

// ListenTCP announces on the TCP address laddr and returns a TCP listener.
// Net must be "tcp", "tcp4", or "tcp6".
// If laddr has a port of 0, it means to listen on some available port.
// The caller can use l.Addr() to retrieve the chosen address.
83
func ListenTCP(net string, laddr *TCPAddr) (l *TCPListener, err error) {
Fazlul Shahriar's avatar
Fazlul Shahriar committed
84 85 86 87 88 89
	switch net {
	case "tcp", "tcp4", "tcp6":
	default:
		return nil, UnknownNetworkError(net)
	}
	if laddr == nil {
Mikio Hara's avatar
Mikio Hara committed
90
		return nil, &OpError{"listen", net, nil, errMissingAddress}
Fazlul Shahriar's avatar
Fazlul Shahriar committed
91 92 93 94 95 96 97
	}
	l1, err := listenPlan9(net, laddr)
	if err != nil {
		return
	}
	return &TCPListener{*l1}, nil
}