Files
webrtc/pkg/ice/candidate.go
backkem 5798f394a3 ICE: Implement keep-alive
- Periodically send a keep-alive Binding Indication.
- Downgrade connection state to disconnected after timeout.

Resolves #163
2018-09-22 13:00:54 -07:00

76 lines
1.7 KiB
Go

package ice
import (
"math/rand"
"time"
)
// Preference enums when generate Priority
const (
HostCandidatePreference uint16 = 126
SrflxCandidatePreference uint16 = 100
)
// Candidate represents an ICE candidate
type Candidate interface {
GetBase() *CandidateBase
}
// CandidateBase represents an ICE candidate, a base with enough attributes
// for host candidates, see CandidateSrflx and CandidateRelay for more
type CandidateBase struct {
Protocol ProtoType
Address string
Port int
LastSent time.Time
LastReceived time.Time
}
func (c *CandidateBase) seen(outbound bool) {
if outbound {
c.LastSent = time.Now()
} else {
c.LastReceived = time.Now()
}
}
// Priority computes the priority for this ICE Candidate
func (c *CandidateBase) Priority(typePreference uint16, component uint16) uint16 {
localPreference := uint16(rand.New(rand.NewSource(time.Now().UnixNano())).Uint32() / 2)
return (2^24)*typePreference +
(2^8)*localPreference +
(2^0)*(256-component)
}
// CandidateHost is a Candidate of typ Host
type CandidateHost struct {
CandidateBase
}
// GetBase returns the CandidateBase, attributes shared between all Candidates
func (c *CandidateHost) GetBase() *CandidateBase {
return &c.CandidateBase
}
// Address for CandidateHost
func (c *CandidateHost) Address() string {
return c.CandidateBase.Address
}
// Port for CandidateHost
func (c *CandidateHost) Port() int {
return c.CandidateBase.Port
}
// CandidateSrflx is a Candidate of typ Server-Reflexive
type CandidateSrflx struct {
CandidateBase
RemoteAddress string
RemotePort int
}
// GetBase returns the CandidateBase, attributes shared between all Candidates
func (c *CandidateSrflx) GetBase() *CandidateBase {
return &c.CandidateBase
}