Commit 0ce90459 authored by Rémy Oudompheng's avatar Rémy Oudompheng

net: add CloseRead, CloseWrite methods to UnixConn.

Fixes #3345.

R=golang-dev, r, rsc, dave
CC=golang-dev, remy
https://golang.org/cl/6214061
parent 53bc1944
......@@ -6,6 +6,8 @@ package net
import (
"io"
"io/ioutil"
"os"
"runtime"
"testing"
"time"
......@@ -58,6 +60,59 @@ func TestShutdown(t *testing.T) {
}
}
func TestShutdownUnix(t *testing.T) {
if runtime.GOOS == "plan9" {
t.Logf("skipping test on %q", runtime.GOOS)
return
}
f, err := ioutil.TempFile("", "go_net_unixtest")
if err != nil {
t.Fatalf("TempFile: %s", err)
}
f.Close()
tmpname := f.Name()
os.Remove(tmpname)
ln, err := Listen("unix", tmpname)
if err != nil {
t.Fatalf("ListenUnix on %s: %s", tmpname, err)
}
defer os.Remove(tmpname)
go func() {
c, err := ln.Accept()
if err != nil {
t.Fatalf("Accept: %v", err)
}
var buf [10]byte
n, err := c.Read(buf[:])
if n != 0 || err != io.EOF {
t.Fatalf("server Read = %d, %v; want 0, io.EOF", n, err)
}
c.Write([]byte("response"))
c.Close()
}()
c, err := Dial("unix", tmpname)
if err != nil {
t.Fatalf("Dial: %v", err)
}
defer c.Close()
err = c.(*UnixConn).CloseWrite()
if err != nil {
t.Fatalf("CloseWrite: %v", err)
}
var buf [10]byte
n, err := c.Read(buf[:])
if err != nil {
t.Fatalf("client Read: %d, %v", n, err)
}
got := string(buf[:n])
if got != "response" {
t.Errorf("read = %q, want \"response\"", got)
}
}
func TestTCPListenClose(t *testing.T) {
ln, err := Listen("tcp", "127.0.0.1:0")
if err != nil {
......
......@@ -72,6 +72,18 @@ func (c *UnixConn) WriteTo(b []byte, addr Addr) (n int, err error) {
return
}
// CloseRead shuts down the reading side of the Unix domain connection.
// Most callers should just use Close.
func (c *UnixConn) CloseRead() error {
return syscall.EPLAN9
}
// CloseWrite shuts down the writing side of the Unix domain connection.
// Most callers should just use Close.
func (c *UnixConn) CloseWrite() error {
return syscall.EPLAN9
}
// DialUnix connects to the remote address raddr on the network net,
// which must be "unix" or "unixgram". If laddr is not nil, it is used
// as the local address for the connection.
......
......@@ -207,6 +207,24 @@ func (c *UnixConn) WriteMsgUnix(b, oob []byte, addr *UnixAddr) (n, oobn int, err
return c.fd.WriteMsg(b, oob, nil)
}
// CloseRead shuts down the reading side of the Unix domain connection.
// Most callers should just use Close.
func (c *UnixConn) CloseRead() error {
if !c.ok() {
return syscall.EINVAL
}
return c.fd.CloseRead()
}
// CloseWrite shuts down the writing side of the Unix domain connection.
// Most callers should just use Close.
func (c *UnixConn) CloseWrite() error {
if !c.ok() {
return syscall.EINVAL
}
return c.fd.CloseWrite()
}
// DialUnix connects to the remote address raddr on the network net,
// which must be "unix" or "unixgram". If laddr is not nil, it is used
// as the local address for the connection.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment