beacon.go 831 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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 https://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 errorHolder struct {
  23. err error
  24. mut stdsync.Mutex // uses stdlib sync as I want this to be trivially embeddable, and there is no risk of blocking
  25. }
  26. func (e *errorHolder) setError(err error) {
  27. e.mut.Lock()
  28. e.err = err
  29. e.mut.Unlock()
  30. }
  31. func (e *errorHolder) Error() error {
  32. e.mut.Lock()
  33. err := e.err
  34. e.mut.Unlock()
  35. return err
  36. }