Skip to content

Commit

Permalink
Merge pull request #36 from multiformats/feat/udp
Browse files Browse the repository at this point in the history
Make PacketConn more idiomatic and direct
  • Loading branch information
Stebalien authored Aug 10, 2019
2 parents bd61b04 + 9feee51 commit 6942398
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
29 changes: 13 additions & 16 deletions net.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ type maEndpoints struct {
raddr ma.Multiaddr
}

var _ Conn = (*maConn)(nil)

// LocalMultiaddr returns the local address associated with
// this connection
func (c *maEndpoints) LocalMultiaddr() ma.Multiaddr {
Expand Down Expand Up @@ -310,14 +312,12 @@ func WrapNetListener(nl net.Listener) (Listener, error) {
// A PacketConn is a generic packet oriented network connection which uses an
// underlying net.PacketConn, wrapped with the locally bound Multiaddr.
type PacketConn interface {
Connection() net.PacketConn

Multiaddr() ma.Multiaddr
net.PacketConn

ReadFrom(b []byte) (int, ma.Multiaddr, error)
WriteTo(b []byte, maddr ma.Multiaddr) (int, error)
LocalMultiaddr() ma.Multiaddr

Close() error
ReadFromMultiaddr(b []byte) (int, ma.Multiaddr, error)
WriteToMultiaddr(b []byte, maddr ma.Multiaddr) (int, error)
}

// maPacketConn implements PacketConn
Expand All @@ -326,28 +326,25 @@ type maPacketConn struct {
laddr ma.Multiaddr
}

// Connection returns the embedded net.PacketConn.
func (l *maPacketConn) Connection() net.PacketConn {
return l.PacketConn
}
var _ PacketConn = (*maPacketConn)(nil)

// Multiaddr returns the bound local Multiaddr.
func (l *maPacketConn) Multiaddr() ma.Multiaddr {
// LocalMultiaddr returns the bound local Multiaddr.
func (l *maPacketConn) LocalMultiaddr() ma.Multiaddr {
return l.laddr
}

func (l *maPacketConn) ReadFrom(b []byte) (int, ma.Multiaddr, error) {
n, addr, err := l.PacketConn.ReadFrom(b)
func (l *maPacketConn) ReadFromMultiaddr(b []byte) (int, ma.Multiaddr, error) {
n, addr, err := l.ReadFrom(b)
maddr, _ := FromNetAddr(addr)
return n, maddr, err
}

func (l *maPacketConn) WriteTo(b []byte, maddr ma.Multiaddr) (int, error) {
func (l *maPacketConn) WriteToMultiaddr(b []byte, maddr ma.Multiaddr) (int, error) {
addr, err := ToNetAddr(maddr)
if err != nil {
return 0, err
}
return l.PacketConn.WriteTo(b, addr)
return l.WriteTo(b, addr)
}

// ListenPacket announces on the local network address laddr.
Expand Down
4 changes: 2 additions & 2 deletions net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ func TestListenPacketAndDial(t *testing.T) {
wg.Add(1)

go func() {
if !pc.Multiaddr().Equal(maddr) {
t.Fatal("connection multiaddr not equal:", maddr, pc.Multiaddr())
if !pc.LocalMultiaddr().Equal(maddr) {
t.Fatal("connection multiaddr not equal:", maddr, pc.LocalMultiaddr())
}

buffer := make([]byte, 1024)
Expand Down

0 comments on commit 6942398

Please sign in to comment.