github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/p2p/net/swarm/swarm_stream.go (about)

     1  package swarm
     2  
     3  import (
     4  	inet "github.com/ipfs/go-ipfs/p2p/net"
     5  
     6  	ps "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-peerstream"
     7  )
     8  
     9  // a Stream is a wrapper around a ps.Stream that exposes a way to get
    10  // our Conn and Swarm (instead of just the ps.Conn and ps.Swarm)
    11  type Stream ps.Stream
    12  
    13  // Stream returns the underlying peerstream.Stream
    14  func (s *Stream) Stream() *ps.Stream {
    15  	return (*ps.Stream)(s)
    16  }
    17  
    18  // Conn returns the Conn associated with this Stream, as an inet.Conn
    19  func (s *Stream) Conn() inet.Conn {
    20  	return s.SwarmConn()
    21  }
    22  
    23  // SwarmConn returns the Conn associated with this Stream, as a *Conn
    24  func (s *Stream) SwarmConn() *Conn {
    25  	return (*Conn)(s.Stream().Conn())
    26  }
    27  
    28  // Read reads bytes from a stream.
    29  func (s *Stream) Read(p []byte) (n int, err error) {
    30  	return s.Stream().Read(p)
    31  }
    32  
    33  // Write writes bytes to a stream, flushing for each call.
    34  func (s *Stream) Write(p []byte) (n int, err error) {
    35  	return s.Stream().Write(p)
    36  }
    37  
    38  // Close closes the stream, indicating this side is finished
    39  // with the stream.
    40  func (s *Stream) Close() error {
    41  	return s.Stream().Close()
    42  }
    43  
    44  func wrapStream(pss *ps.Stream) *Stream {
    45  	return (*Stream)(pss)
    46  }
    47  
    48  func wrapStreams(st []*ps.Stream) []*Stream {
    49  	out := make([]*Stream, len(st))
    50  	for i, s := range st {
    51  		out[i] = wrapStream(s)
    52  	}
    53  	return out
    54  }