/
pair.go
96 lines (83 loc) · 1.88 KB
/
pair.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package ice
import (
"fmt"
)
type CandidatePair struct {
id string
local Candidate
remote Candidate
foundation string
component int
state CandidatePairState
nominated bool
// Number of failed connectivity checks for this pair.
failCount int
}
// Candidate pair states
type CandidatePairState int
const (
Frozen CandidatePairState = iota
Waiting
InProgress
Succeeded
Failed
)
func (state CandidatePairState) String() string {
switch state {
case Frozen:
return "Frozen"
case Waiting:
return "Waiting"
case InProgress:
return "In Progress"
case Succeeded:
return "Succeeded"
case Failed:
return "Failed"
default:
panic(fmt.Sprintf("Invalid CandidatePairState: %d", state))
}
}
func newCandidatePair(seq int, local, remote Candidate) *CandidatePair {
if local.component != remote.component {
log.Panicf("Candidates in pair have different components: %d != %d", local.component, remote.component)
}
id := fmt.Sprintf("Pair#%d", seq)
foundation := fmt.Sprintf("%s/%s", local.foundation, remote.foundation)
return &CandidatePair{
id: id,
local: local,
remote: remote,
foundation: foundation,
component: local.component,
state: Frozen,
}
}
func (p *CandidatePair) sendStun(msg *stunMessage, handler stunHandler) error {
return p.local.base.sendStun(msg, p.remote.address.netAddr(), handler)
}
func (p *CandidatePair) String() string {
return fmt.Sprintf("%s: %s -> %s [%s]", p.id, p.local.address, p.remote.address, p.state)
}
// TODO: Handle case where we're the controlling agent.
func (p *CandidatePair) Priority() uint64 {
G := uint64(p.remote.priority)
D := uint64(p.local.priority)
var B uint64 = 0
if G > D {
B = 1
}
return min(G, D)<<32 + max(G, D)<<1 + B
}
func min(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
func max(a, b uint64) uint64 {
if a > b {
return a
}
return b
}