beacon.go 902 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 (
  8. "net"
  9. stdsync "sync"
  10. "github.com/thejerf/suture"
  11. )
  12. type recv struct {
  13. data []byte
  14. src net.Addr
  15. }
  16. type Interface interface {
  17. suture.Service
  18. Send(data []byte)
  19. Recv() ([]byte, net.Addr)
  20. Error() error
  21. }
  22. type readerFrom interface {
  23. ReadFrom([]byte) (int, net.Addr, error)
  24. }
  25. type errorHolder struct {
  26. err error
  27. mut stdsync.Mutex // uses stdlib sync as I want this to be trivially embeddable, and there is no risk of blocking
  28. }
  29. func (e *errorHolder) setError(err error) {
  30. e.mut.Lock()
  31. e.err = err
  32. e.mut.Unlock()
  33. }
  34. func (e *errorHolder) Error() error {
  35. e.mut.Lock()
  36. err := e.err
  37. e.mut.Unlock()
  38. return err
  39. }