beacon.go 822 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package beacon
  7. import "net"
  8. type recv struct {
  9. data []byte
  10. src net.Addr
  11. }
  12. type Interface interface {
  13. Send(data []byte)
  14. Recv() ([]byte, net.Addr)
  15. }
  16. func genericReader(conn *net.UDPConn, outbox chan<- recv) {
  17. bs := make([]byte, 65536)
  18. for {
  19. n, addr, err := conn.ReadFrom(bs)
  20. if err != nil {
  21. l.Warnln("multicast read:", err)
  22. return
  23. }
  24. if debug {
  25. l.Debugf("recv %d bytes from %s", n, addr)
  26. }
  27. c := make([]byte, n)
  28. copy(c, bs)
  29. select {
  30. case outbox <- recv{c, addr}:
  31. default:
  32. if debug {
  33. l.Debugln("dropping message")
  34. }
  35. }
  36. }
  37. }