github.com/Unheilbar/quorum@v1.0.0/p2p/simulations/pipes/pipes.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package pipes 18 19 import ( 20 "net" 21 ) 22 23 // NetPipe wraps net.Pipe in a signature returning an error 24 func NetPipe() (net.Conn, net.Conn, error) { 25 p1, p2 := net.Pipe() 26 return p1, p2, nil 27 } 28 29 // TCPPipe creates an in process full duplex pipe based on a localhost TCP socket 30 func TCPPipe() (net.Conn, net.Conn, error) { 31 l, err := net.Listen("tcp", "127.0.0.1:0") 32 if err != nil { 33 return nil, nil, err 34 } 35 defer l.Close() 36 37 var aconn net.Conn 38 aerr := make(chan error, 1) 39 go func() { 40 var err error 41 aconn, err = l.Accept() 42 aerr <- err 43 }() 44 45 dconn, err := net.Dial("tcp", l.Addr().String()) 46 if err != nil { 47 <-aerr 48 return nil, nil, err 49 } 50 if err := <-aerr; err != nil { 51 dconn.Close() 52 return nil, nil, err 53 } 54 return aconn, dconn, nil 55 }