Unverified Commit 69423986 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #36 from multiformats/feat/udp

Make PacketConn more idiomatic and direct
parents bd61b049 9feee519
......@@ -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 {
......@@ -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
......@@ -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.
......
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment