better mc debug output

This commit is contained in:
Jakob Borg 2014-05-11 18:20:14 -03:00
parent 7a41362d90
commit e4f266883a

View File

@ -10,10 +10,15 @@ type recv struct {
src net.Addr src net.Addr
} }
type dst struct {
intf string
conn *net.UDPConn
}
type Beacon struct { type Beacon struct {
group string group string
port int port int
conns []*net.UDPConn conns []dst
inbox chan []byte inbox chan []byte
outbox chan recv outbox chan recv
} }
@ -61,25 +66,25 @@ func (b *Beacon) run() {
dlog.Printf("failed to listen for multicast group on %q: %v", intf.Name, err) dlog.Printf("failed to listen for multicast group on %q: %v", intf.Name, err)
} }
} else { } else {
b.conns = append(b.conns, conn) b.conns = append(b.conns, dst{intf.Name, conn})
if debug { if debug {
dlog.Printf("listening for multicast group on %q", intf.Name) dlog.Printf("listening for multicast group on %q", intf.Name)
} }
} }
} }
for _, conn := range b.conns { for _, dst := range b.conns {
conn := conn dst := dst
go func() { go func() {
for { for {
var bs = make([]byte, 1500) var bs = make([]byte, 1500)
n, addr, err := conn.ReadFrom(bs) n, addr, err := dst.conn.ReadFrom(bs)
if err != nil { if err != nil {
dlog.Println(err) dlog.Println(err)
return return
} }
if debug { if debug {
dlog.Printf("recv %d bytes from %s on %v", n, addr, conn) dlog.Printf("recv %d bytes from %s on %s", n, addr, dst.intf)
} }
b.outbox <- recv{bs[:n], addr} b.outbox <- recv{bs[:n], addr}
} }
@ -88,14 +93,14 @@ func (b *Beacon) run() {
go func() { go func() {
for bs := range b.inbox { for bs := range b.inbox {
for _, conn := range b.conns { for _, dst := range b.conns {
_, err := conn.WriteTo(bs, group) _, err := dst.conn.WriteTo(bs, group)
if err != nil { if err != nil {
dlog.Println(err) dlog.Println(err)
return return
} }
if debug { if debug {
dlog.Printf("sent %d bytes to %s on %v", len(bs), group, conn) dlog.Printf("sent %d bytes to %s on %s", len(bs), group, dst.intf)
} }
} }
} }