github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/pipe/pipe.go (about) 1 package pipe 2 3 import "io" 4 5 // DuplexPipeEnd is one end of a duplex pipe. 6 type DuplexPipeEnd struct { 7 r *io.PipeReader 8 w *io.PipeWriter 9 } 10 11 // NewDuplexPipe assembles 2 pipes together as a duplex pipe 12 // and returns both ends of the duplex pipe. 13 func NewDuplexPipe() (*DuplexPipeEnd, *DuplexPipeEnd) { 14 lr, lw := io.Pipe() 15 rr, rw := io.Pipe() 16 return &DuplexPipeEnd{ 17 r: lr, 18 w: rw, 19 }, &DuplexPipeEnd{ 20 r: rr, 21 w: lw, 22 } 23 } 24 25 func (p *DuplexPipeEnd) Read(b []byte) (int, error) { 26 return p.r.Read(b) 27 } 28 29 func (p *DuplexPipeEnd) Write(b []byte) (int, error) { 30 return p.w.Write(b) 31 } 32 33 func (p *DuplexPipeEnd) CloseRead() error { 34 return p.r.Close() 35 } 36 37 func (p *DuplexPipeEnd) CloseWrite() error { 38 return p.w.Close() 39 } 40 41 func (p *DuplexPipeEnd) Close() error { 42 p.r.Close() 43 p.w.Close() 44 return nil 45 } 46 47 func (p *DuplexPipeEnd) CloseReadWithError(err error) { 48 p.r.CloseWithError(err) 49 } 50 51 func (p *DuplexPipeEnd) CloseWriteWithError(err error) { 52 p.w.CloseWithError(err) 53 } 54 55 func (p *DuplexPipeEnd) CloseWithError(err error) { 56 p.r.CloseWithError(err) 57 p.w.CloseWithError(err) 58 }