Mikio Hara | 471e43c | 2011-11-16 06:59:08 +0900 | [diff] [blame] | 1 | // Copyright 2010 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 | |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 5 | package net |
| 6 | |
| 7 | import ( |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 8 | "errors" |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 9 | "io" |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 10 | "time" |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 11 | ) |
| 12 | |
| 13 | // Pipe creates a synchronous, in-memory, full duplex |
| 14 | // network connection; both ends implement the Conn interface. |
| 15 | // Reads on one end are matched with writes on the other, |
| 16 | // copying data directly between the two; there is no internal |
| 17 | // buffering. |
| 18 | func Pipe() (Conn, Conn) { |
| 19 | r1, w1 := io.Pipe() |
| 20 | r2, w2 := io.Pipe() |
| 21 | |
| 22 | return &pipe{r1, w2}, &pipe{r2, w1} |
| 23 | } |
| 24 | |
| 25 | type pipe struct { |
| 26 | *io.PipeReader |
| 27 | *io.PipeWriter |
| 28 | } |
| 29 | |
| 30 | type pipeAddr int |
| 31 | |
| 32 | func (pipeAddr) Network() string { |
| 33 | return "pipe" |
| 34 | } |
| 35 | |
| 36 | func (pipeAddr) String() string { |
| 37 | return "pipe" |
| 38 | } |
| 39 | |
Russ Cox | eb69292 | 2011-11-01 22:05:34 -0400 | [diff] [blame] | 40 | func (p *pipe) Close() error { |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 41 | err := p.PipeReader.Close() |
| 42 | err1 := p.PipeWriter.Close() |
| 43 | if err == nil { |
| 44 | err = err1 |
| 45 | } |
| 46 | return err |
| 47 | } |
| 48 | |
| 49 | func (p *pipe) LocalAddr() Addr { |
| 50 | return pipeAddr(0) |
| 51 | } |
| 52 | |
| 53 | func (p *pipe) RemoteAddr() Addr { |
| 54 | return pipeAddr(0) |
| 55 | } |
| 56 | |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 57 | func (p *pipe) SetDeadline(t time.Time) error { |
| 58 | return errors.New("net.Pipe does not support deadlines") |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 61 | func (p *pipe) SetReadDeadline(t time.Time) error { |
| 62 | return errors.New("net.Pipe does not support deadlines") |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Brad Fitzpatrick | b71883e | 2012-01-18 16:24:06 -0800 | [diff] [blame] | 65 | func (p *pipe) SetWriteDeadline(t time.Time) error { |
| 66 | return errors.New("net.Pipe does not support deadlines") |
Russ Cox | 1d18e89 | 2010-04-26 10:36:05 -0700 | [diff] [blame] | 67 | } |