github.com/coreos/mantle@v0.13.0/network/bufnet/pipe.go (about)

     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  // Licensed under the same terms as Go itself:
     5  // https://github.com/golang/go/blob/master/LICENSE
     6  
     7  package bufnet
     8  
     9  import (
    10  	"errors"
    11  	"net"
    12  	"time"
    13  
    14  	"github.com/coreos/mantle/lang/bufpipe"
    15  )
    16  
    17  // Pipe creates a synchronous, in-memory, full duplex
    18  // network connection with unlimited buffering.
    19  // Both ends implement the Conn interface.
    20  func Pipe() (net.Conn, net.Conn) {
    21  	r1, w1 := bufpipe.Pipe()
    22  	r2, w2 := bufpipe.Pipe()
    23  
    24  	return &pipe{r1, w2}, &pipe{r2, w1}
    25  }
    26  
    27  // FixedPipe creates a synchronous, in-memory, full duplex
    28  // network connection with fixed-size buffers that have
    29  // at least the specified size.
    30  // Both ends implement the Conn interface.
    31  func FixedPipe(size int) (net.Conn, net.Conn) {
    32  	r1, w1 := bufpipe.FixedPipe(size)
    33  	r2, w2 := bufpipe.FixedPipe(size)
    34  
    35  	return &pipe{r1, w2}, &pipe{r2, w1}
    36  }
    37  
    38  type pipe struct {
    39  	*bufpipe.PipeReader
    40  	*bufpipe.PipeWriter
    41  }
    42  
    43  type pipeAddr int
    44  
    45  func (pipeAddr) Network() string {
    46  	return "pipe"
    47  }
    48  
    49  func (pipeAddr) String() string {
    50  	return "pipe"
    51  }
    52  
    53  func (p *pipe) Close() error {
    54  	err := p.PipeReader.Close()
    55  	err1 := p.PipeWriter.Close()
    56  	if err == nil {
    57  		err = err1
    58  	}
    59  	return err
    60  }
    61  
    62  func (p *pipe) LocalAddr() net.Addr {
    63  	return pipeAddr(0)
    64  }
    65  
    66  func (p *pipe) RemoteAddr() net.Addr {
    67  	return pipeAddr(0)
    68  }
    69  
    70  func (p *pipe) SetDeadline(t time.Time) error {
    71  	return &net.OpError{Op: "set", Net: "pipe", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
    72  }
    73  
    74  func (p *pipe) SetReadDeadline(t time.Time) error {
    75  	return &net.OpError{Op: "set", Net: "pipe", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
    76  }
    77  
    78  func (p *pipe) SetWriteDeadline(t time.Time) error {
    79  	return &net.OpError{Op: "set", Net: "pipe", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
    80  }