From f619079d868d6e69eacc1843d10e39e2aa0c8ff4 Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Wed, 9 Mar 2022 11:17:27 +0100 Subject: [PATCH] feat: expose some read-only methods on the underlying network.Stream interface --- conn.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/conn.go b/conn.go index fd3d5c3..857f601 100644 --- a/conn.go +++ b/conn.go @@ -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} @@ -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() +}