Browse Source

discovery: rework discovery handling

bboozzoo/golang
Maciek Borzecki 7 years ago
parent
commit
a5c696c2da
1 changed files with 26 additions and 10 deletions
  1. +26
    -10
      discovery/listener.go

+ 26
- 10
discovery/listener.go View File

@ -15,6 +15,8 @@ import (
"context" "context"
"net" "net"
"github.com/pkg/errors"
"github.com/bboozzoo/mconnect/logger" "github.com/bboozzoo/mconnect/logger"
"github.com/bboozzoo/mconnect/protocol/packet" "github.com/bboozzoo/mconnect/protocol/packet"
) )
@ -39,26 +41,40 @@ func NewListener() (*Listener, error) {
return listener, nil return listener, nil
} }
func (l *Listener) Receive(ctx context.Context) {
// Discovery conveys the received discovery information
type Discovery struct {
// Packet is the original packet received
Packet *packet.Packet
// Identity is the parsed identity data
Identity *packet.Identity
// From is the address the packet was received from
From *net.UDPAddr
}
// Receive blocks waiting to receive a discovery packet. Once received, it will
// parse the packet and return a result.
func (l *Listener) Receive(ctx context.Context) (*Discovery, error) {
log := logger.FromContext(ctx) log := logger.FromContext(ctx)
buf := make([]byte, 4096) buf := make([]byte, 4096)
count, addr, err := l.conn.ReadFromUDP(buf) count, addr, err := l.conn.ReadFromUDP(buf)
if err != nil { if err != nil {
log.Printf("listen failed: %v", err)
return nil, errors.Wrap(err, "failed to receive a packet")
} }
log.Printf("got %v bytes from %v", count, addr)
log.Printf("data:\n%s", string(buf))
log.Debugf("got %v bytes from %v", count, addr)
log.Debugf("data:\n%s", string(buf))
p, err := packet.FromData(buf) p, err := packet.FromData(buf)
if err != nil { if err != nil {
log.Errorf("failed to parse packet: %v", err)
return
return nil, errors.Wrap(err, "failed to parse packet")
} }
log.Printf("packet: %+v", p)
identity, err := p.AsIdentity() identity, err := p.AsIdentity()
if err != nil { if err != nil {
log.Errorf("failed to parse identity packet: %v", err)
return
return nil, errors.Wrap(err, "failed to parse as identity packet")
}
discovery := &Discovery{
Packet: p,
Identity: identity,
From: addr,
} }
log.Printf("identity: %+v", identity)
return discovery, nil
} }

Loading…
Cancel
Save