-
Notifications
You must be signed in to change notification settings - Fork 84
/
register.go
156 lines (139 loc) · 3.53 KB
/
register.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package ua
import (
"context"
"time"
"github.com/cloudwebrtc/go-sip-ua/pkg/account"
"github.com/cloudwebrtc/go-sip-ua/pkg/auth"
"github.com/ghettovoice/gosip/sip"
"github.com/ghettovoice/gosip/util"
)
type Register struct {
ua *UserAgent
timer *time.Timer
profile *account.Profile
authorizer *auth.ClientAuthorizer
recipient sip.SipUri
request *sip.Request
ctx context.Context
cancel context.CancelFunc
data interface{}
}
func NewRegister(ua *UserAgent, profile *account.Profile, recipient sip.SipUri, data interface{}) *Register {
r := &Register{
ua: ua,
profile: profile,
recipient: recipient,
request: nil,
data: data,
}
r.ctx, r.cancel = context.WithCancel(context.Background())
return r
}
func (r *Register) SendRegister(expires uint32) error {
ua := r.ua
profile := r.profile
recipient := r.recipient
from := &sip.Address{
Uri: profile.URI,
Params: sip.NewParams().Add("tag", sip.String{Str: util.RandString(8)}),
}
to := &sip.Address{
Uri: profile.URI,
}
contact := profile.Contact()
if r.request == nil || expires == 0 {
request, err := ua.buildRequest(sip.REGISTER, from, to, contact, recipient, nil)
if err != nil {
ua.Log().Errorf("Register: err = %v", err)
return err
}
expiresHeader := sip.Expires(expires)
(*request).AppendHeader(&expiresHeader)
r.request = request
} else {
cseq, _ := (*r.request).CSeq()
cseq.SeqNo++
cseq.MethodName = sip.REGISTER
(*r.request).RemoveHeader("Expires")
// replace Expires header.
expiresHeader := sip.Expires(expires)
(*r.request).AppendHeader(&expiresHeader)
}
if profile.AuthInfo != nil && r.authorizer == nil {
r.authorizer = auth.NewClientAuthorizer(profile.AuthInfo.AuthUser, profile.AuthInfo.Password)
}
resp, err := ua.RequestWithContext(r.ctx, *r.request, r.authorizer, true)
if err != nil {
ua.Log().Errorf("Request [%s] failed, err => %v", sip.REGISTER, err)
if ua.RegisterStateHandler != nil {
var code sip.StatusCode
var reason string
if _, ok := err.(*sip.RequestError); ok {
reqErr := err.(*sip.RequestError)
code = sip.StatusCode(reqErr.Code)
reason = reqErr.Reason
} else {
code = 500
reason = err.Error()
}
state := account.RegisterState{
Account: profile,
Response: nil,
StatusCode: sip.StatusCode(code),
Reason: reason,
Expiration: 0,
UserData: r.data,
}
ua.RegisterStateHandler(state)
}
}
if resp != nil {
stateCode := resp.StatusCode()
ua.Log().Debugf("%s resp %d => %s", sip.REGISTER, stateCode, resp.String())
if ua.RegisterStateHandler != nil {
var expires uint32 = 0
hdrs := resp.GetHeaders("Expires")
if len(hdrs) > 0 {
expires = uint32(*(hdrs[0]).(*sip.Expires))
}
state := account.RegisterState{
Account: profile,
Response: resp,
StatusCode: resp.StatusCode(),
Reason: resp.Reason(),
Expiration: expires,
UserData: r.data,
}
if expires > 0 {
go func() {
if r.timer == nil {
r.timer = time.NewTimer(time.Second * time.Duration(expires-10))
} else {
r.timer.Reset(time.Second * time.Duration(expires-10))
}
select {
case <-r.timer.C:
r.SendRegister(expires)
case <-r.ctx.Done():
return
}
}()
} else if expires == 0 {
if r.timer != nil {
r.timer.Stop()
r.timer = nil
}
r.request = nil
}
ua.RegisterStateHandler(state)
}
}
return nil
}
func (r *Register) Stop() {
if r.timer != nil {
r.timer.Stop()
r.timer = nil
}
r.cancel()
}