Skip to content

Commit

Permalink
Improve the RegisterStateHandler process.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Mar 30, 2021
1 parent e949025 commit f7bb264
Showing 1 changed file with 65 additions and 57 deletions.
122 changes: 65 additions & 57 deletions pkg/ua/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,75 +80,83 @@ func (r *Register) SendRegister(expires uint32) error {

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,
}
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.Log().Debugf("Request [%s], has error %v, state => %v", sip.REGISTER, err, state)

if ua.RegisterStateHandler != nil {
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")

var expires uint32 = 0
hdrs := resp.GetHeaders("Expires")
if len(hdrs) > 0 {
expires = uint32(*(hdrs[0]).(*sip.Expires))
} else {
hdrs = resp.GetHeaders("Contact")
if len(hdrs) > 0 {
expires = uint32(*(hdrs[0]).(*sip.Expires))
} else {
hdrs = resp.GetHeaders("Contact")
if len(hdrs) > 0 {
if cexpires, cexpirescok := (hdrs[0].(*sip.ContactHeader)).Params.Get("expires"); cexpirescok {
cexpiresint, _ := strconv.Atoi(cexpires.String())
expires = uint32(cexpiresint)
}
if cexpires, cexpirescok := (hdrs[0].(*sip.ContactHeader)).Params.Get("expires"); cexpirescok {
cexpiresint, _ := strconv.Atoi(cexpires.String())
expires = uint32(cexpiresint)
}
}
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
}
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
}
r.request = nil
}()
} else if expires == 0 {
if r.timer != nil {
r.timer.Stop()
r.timer = nil
}
r.request = nil
}

ua.Log().Debugf("Request [%s], response: state => %v", sip.REGISTER, state)

if ua.RegisterStateHandler != nil {
ua.RegisterStateHandler(state)
}
}
Expand Down

0 comments on commit f7bb264

Please sign in to comment.