candidate_relay.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package ice
  2. import (
  3. "net"
  4. )
  5. // CandidateRelay ...
  6. type CandidateRelay struct {
  7. candidateBase
  8. relayProtocol string
  9. onClose func() error
  10. }
  11. // CandidateRelayConfig is the config required to create a new CandidateRelay
  12. type CandidateRelayConfig struct {
  13. CandidateID string
  14. Network string
  15. Address string
  16. Port int
  17. Component uint16
  18. Priority uint32
  19. Foundation string
  20. RelAddr string
  21. RelPort int
  22. RelayProtocol string
  23. OnClose func() error
  24. }
  25. // NewCandidateRelay creates a new relay candidate
  26. func NewCandidateRelay(config *CandidateRelayConfig) (*CandidateRelay, error) {
  27. candidateID := config.CandidateID
  28. if candidateID == "" {
  29. candidateID = globalCandidateIDGenerator.Generate()
  30. }
  31. ip := net.ParseIP(config.Address)
  32. if ip == nil {
  33. return nil, ErrAddressParseFailed
  34. }
  35. networkType, err := determineNetworkType(config.Network, ip)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return &CandidateRelay{
  40. candidateBase: candidateBase{
  41. id: candidateID,
  42. networkType: networkType,
  43. candidateType: CandidateTypeRelay,
  44. address: config.Address,
  45. port: config.Port,
  46. resolvedAddr: &net.UDPAddr{IP: ip, Port: config.Port},
  47. component: config.Component,
  48. foundationOverride: config.Foundation,
  49. priorityOverride: config.Priority,
  50. relatedAddress: &CandidateRelatedAddress{
  51. Address: config.RelAddr,
  52. Port: config.RelPort,
  53. },
  54. },
  55. relayProtocol: config.RelayProtocol,
  56. onClose: config.OnClose,
  57. }, nil
  58. }
  59. // RelayProtocol returns the protocol used between the endpoint and the relay server.
  60. func (c *CandidateRelay) RelayProtocol() string {
  61. return c.relayProtocol
  62. }
  63. func (c *CandidateRelay) close() error {
  64. err := c.candidateBase.close()
  65. if c.onClose != nil {
  66. err = c.onClose()
  67. c.onClose = nil
  68. }
  69. return err
  70. }