Skip to content

Commit

Permalink
Expose some read-only methods on the underlying Stream interface (#67)
Browse files Browse the repository at this point in the history
* feat: expose some read-only methods on the underlying network.Stream interface

* refactor: override network.Stream instead of calling its methods individually

Co-authored-by: Jacob Heun <jacobheun@gmail.com>
  • Loading branch information
dirkmc and jacobheun authored Aug 18, 2022
1 parent 9d9dab5 commit 3f2ab62
Showing 1 changed file with 3 additions and 42 deletions.
45 changes: 3 additions & 42 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gostream
import (
"context"
"net"
"time"

"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
Expand All @@ -14,60 +13,22 @@ import (
// conn is an implementation of net.Conn which wraps
// libp2p streams.
type conn struct {
s network.Stream
network.Stream
}

// newConn creates a conn given a libp2p stream
func newConn(s network.Stream) net.Conn {
return &conn{s}
}

// Read reads data from the connection.
func (c *conn) Read(b []byte) (n int, err error) {
return c.s.Read(b)
}

// Write writes data to the connection.
func (c *conn) Write(b []byte) (n int, err error) {
return c.s.Write(b)
}

// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
func (c *conn) Close() error {
return c.s.Close()
}

// LocalAddr returns the local network address.
func (c *conn) LocalAddr() net.Addr {
return &addr{c.s.Conn().LocalPeer()}
return &addr{c.Stream.Conn().LocalPeer()}
}

// RemoteAddr returns the remote network address.
func (c *conn) RemoteAddr() net.Addr {
return &addr{c.s.Conn().RemotePeer()}
}

// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
// See https://golang.org/pkg/net/#Conn for more details.
func (c *conn) SetDeadline(t time.Time) error {
return c.s.SetDeadline(t)
}

// SetReadDeadline sets the deadline for future Read calls.
// A zero value for t means Read will not time out.
func (c *conn) SetReadDeadline(t time.Time) error {
return c.s.SetReadDeadline(t)
}

// SetWriteDeadline sets the deadline for future Write calls.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
func (c *conn) SetWriteDeadline(t time.Time) error {
return c.s.SetWriteDeadline(t)
return &addr{c.Stream.Conn().RemotePeer()}
}

// Dial opens a stream to the destination address
Expand Down

0 comments on commit 3f2ab62

Please sign in to comment.