/
candidatepair.go
53 lines (44 loc) · 1.21 KB
/
candidatepair.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
package ice
import (
"fmt"
"github.com/pions/pkg/stun"
)
func newCandidatePair(local, remote *Candidate) *candidatePair {
return &candidatePair{
remote: remote,
local: local,
}
}
// candidatePair represents a combination of a local and remote candidate
type candidatePair struct {
remote *Candidate
local *Candidate
}
func (p *candidatePair) Write(b []byte) (int, error) {
return p.local.writeTo(b, p.remote)
}
// keepaliveCandidate sends a STUN Binding Indication to the remote candidate
func (a *Agent) keepaliveCandidate(local, remote *Candidate) {
msg, err := stun.Build(stun.ClassIndication, stun.MethodBinding, stun.GenerateTransactionId(),
&stun.Username{Username: a.remoteUfrag + ":" + a.localUfrag},
&stun.MessageIntegrity{
Key: []byte(a.remotePwd),
},
&stun.Fingerprint{},
)
if err != nil {
fmt.Println(err)
return
}
a.sendSTUN(msg, local, remote)
}
func (a *Agent) sendSTUN(msg *stun.Message, local, remote *Candidate) {
_, err := local.writeTo(msg.Pack(), remote)
if err != nil {
// TODO: Determine if we should always drop the err
// E.g.: maybe handle for known valid pairs or to
// discard pairs faster.
_ = err
// fmt.Printf("failed to send STUN message: %v", err)
}
}