]> Cypherpunks repositories - gostls13.git/commitdiff
net: add Pipe
authorRuss Cox <rsc@golang.org>
Mon, 26 Apr 2010 17:36:05 +0000 (10:36 -0700)
committerRuss Cox <rsc@golang.org>
Mon, 26 Apr 2010 17:36:05 +0000 (10:36 -0700)
R=r
CC=golang-dev
https://golang.org/cl/1004043

src/pkg/net/Makefile
src/pkg/net/pipe.go [new file with mode: 0644]
src/pkg/net/pipe_test.go [new file with mode: 0644]

index f5e78fb84c8f5964684134e44b8be8b7c71209ef..95360539b6e89c2eba42996092f0df914f7cf123 100644 (file)
@@ -16,6 +16,7 @@ GOFILES=\
        ipsock.go\
        net.go\
        parse.go\
+       pipe.go\
        port.go\
        sock.go\
        tcpsock.go\
diff --git a/src/pkg/net/pipe.go b/src/pkg/net/pipe.go
new file mode 100644 (file)
index 0000000..c0bbd35
--- /dev/null
@@ -0,0 +1,62 @@
+package net
+
+import (
+       "io"
+       "os"
+)
+
+// Pipe creates a synchronous, in-memory, full duplex
+// network connection; both ends implement the Conn interface.
+// Reads on one end are matched with writes on the other,
+// copying data directly between the two; there is no internal
+// buffering.
+func Pipe() (Conn, Conn) {
+       r1, w1 := io.Pipe()
+       r2, w2 := io.Pipe()
+
+       return &pipe{r1, w2}, &pipe{r2, w1}
+}
+
+type pipe struct {
+       *io.PipeReader
+       *io.PipeWriter
+}
+
+type pipeAddr int
+
+func (pipeAddr) Network() string {
+       return "pipe"
+}
+
+func (pipeAddr) String() string {
+       return "pipe"
+}
+
+func (p *pipe) Close() os.Error {
+       err := p.PipeReader.Close()
+       err1 := p.PipeWriter.Close()
+       if err == nil {
+               err = err1
+       }
+       return err
+}
+
+func (p *pipe) LocalAddr() Addr {
+       return pipeAddr(0)
+}
+
+func (p *pipe) RemoteAddr() Addr {
+       return pipeAddr(0)
+}
+
+func (p *pipe) SetTimeout(nsec int64) os.Error {
+       return os.NewError("net.Pipe does not support timeouts")
+}
+
+func (p *pipe) SetReadTimeout(nsec int64) os.Error {
+       return os.NewError("net.Pipe does not support timeouts")
+}
+
+func (p *pipe) SetWriteTimeout(nsec int64) os.Error {
+       return os.NewError("net.Pipe does not support timeouts")
+}
diff --git a/src/pkg/net/pipe_test.go b/src/pkg/net/pipe_test.go
new file mode 100644 (file)
index 0000000..7e4c6db
--- /dev/null
@@ -0,0 +1,57 @@
+// Copyright 2010 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.
+
+package net
+
+import (
+       "bytes"
+       "io"
+       "os"
+       "testing"
+)
+
+func checkWrite(t *testing.T, w io.Writer, data []byte, c chan int) {
+       n, err := w.Write(data)
+       if err != nil {
+               t.Errorf("write: %v", err)
+       }
+       if n != len(data) {
+               t.Errorf("short write: %d != %d", n, len(data))
+       }
+       c <- 0
+}
+
+func checkRead(t *testing.T, r io.Reader, data []byte, wantErr os.Error) {
+       buf := make([]byte, len(data)+10)
+       n, err := r.Read(buf)
+       if err != wantErr {
+               t.Errorf("read: %v", err)
+               return
+       }
+       if n != len(data) || !bytes.Equal(buf[0:n], data) {
+               t.Errorf("bad read: got %q", buf[0:n])
+               return
+       }
+}
+
+// Test a simple read/write/close sequence.
+// Assumes that the underlying io.Pipe implementation
+// is solid and we're just testing the net wrapping.
+
+func TestPipe(t *testing.T) {
+       c := make(chan int)
+       cli, srv := Pipe()
+       go checkWrite(t, cli, []byte("hello, world"), c)
+       checkRead(t, srv, []byte("hello, world"), nil)
+       <-c
+       go checkWrite(t, srv, []byte("line 2"), c)
+       checkRead(t, cli, []byte("line 2"), nil)
+       <-c
+       go checkWrite(t, cli, []byte("a third line"), c)
+       checkRead(t, srv, []byte("a third line"), nil)
+       <-c
+       go srv.Close()
+       checkRead(t, cli, nil, os.EOF)
+       cli.Close()
+}