Skip to content

Commit

Permalink
feat: expose some read-only methods on the underlying network.Stream …
Browse files Browse the repository at this point in the history
…interface
  • Loading branch information
dirkmc committed Mar 9, 2022
1 parent 16a4ebf commit f619079
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ import (
"github.com/libp2p/go-libp2p-core/protocol"
)

// Stream is a subset of the read-only methods on the network.Stream interface
type Stream interface {
ID() string
Protocol() protocol.ID
Stat() network.Stat
}

// conn is an implementation of net.Conn which wraps
// libp2p streams.
type conn struct {
s network.Stream
}

var _ Stream = (*conn)(nil)

// newConn creates a conn given a libp2p stream
func newConn(s network.Stream) net.Conn {
return &conn{s}
Expand Down Expand Up @@ -80,3 +89,18 @@ func Dial(ctx context.Context, h host.Host, pid peer.ID, tag protocol.ID) (net.C
}
return newConn(s), nil
}

// ID returns the underlying network.Stream's ID
func (c *conn) ID() string {
return c.s.ID()
}

// Protocol returns the underlying network.Stream's Protocol
func (c *conn) Protocol() protocol.ID {
return c.s.Protocol()
}

// Stat returns the underlying network.Stream's Stat
func (c *conn) Stat() network.Stat {
return c.s.Stat()
}

0 comments on commit f619079

Please sign in to comment.