beacon.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package beacon
  16. import "net"
  17. type recv struct {
  18. data []byte
  19. src net.Addr
  20. }
  21. type Interface interface {
  22. Send(data []byte)
  23. Recv() ([]byte, net.Addr)
  24. }
  25. func genericReader(conn *net.UDPConn, outbox chan<- recv) {
  26. bs := make([]byte, 65536)
  27. for {
  28. n, addr, err := conn.ReadFrom(bs)
  29. if err != nil {
  30. l.Warnln("multicast read:", err)
  31. return
  32. }
  33. if debug {
  34. l.Debugf("recv %d bytes from %s", n, addr)
  35. }
  36. c := make([]byte, n)
  37. copy(c, bs)
  38. select {
  39. case outbox <- recv{c, addr}:
  40. default:
  41. if debug {
  42. l.Debugln("dropping message")
  43. }
  44. }
  45. }
  46. }