Skip to content

Commit

Permalink
Add UserData for Register.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Mar 19, 2021
1 parent c7568af commit 4d2a73d
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions examples/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func main() {
logger.Error(err)
}

go ua.SendRegister(profile, recipient, profile.Expires)
go ua.SendRegister(profile, recipient, profile.Expires, nil)
time.Sleep(time.Second * 3)

udp = createUdp()
Expand All @@ -129,7 +129,7 @@ func main() {
go ua.Invite(profile, called, recipient, &sdp)

time.Sleep(time.Second * 3)
go ua.SendRegister(profile, recipient, 0)
go ua.SendRegister(profile, recipient, 0, nil)

<-stop

Expand Down
2 changes: 1 addition & 1 deletion examples/register/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
logger.Error(err)
}

register, err := ua.SendRegister(profile, recipient, profile.Expires)
register, err := ua.SendRegister(profile, recipient, profile.Expires, nil)
if err != nil {
logger.Error(err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/account/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ func NewProfile(

//RegisterState .
type RegisterState struct {
Account Profile
Account *Profile
StatusCode sip.StatusCode
Reason string
Expiration uint32
Response sip.Response
UserData interface{}
}
18 changes: 11 additions & 7 deletions pkg/ua/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ type Register struct {
request *sip.Request
ctx context.Context
cancel context.CancelFunc
data interface{}
}

func NewRegister(ua *UserAgent, profile *account.Profile, recipient sip.SipUri) *Register {
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
Expand Down Expand Up @@ -85,14 +87,15 @@ func (r *Register) SendRegister(expires uint32) error {
reason = err.Error()
}

regState := account.RegisterState{
Account: *profile,
state := account.RegisterState{
Account: profile,
Response: nil,
StatusCode: sip.StatusCode(code),
Reason: reason,
Expiration: 0,
UserData: r.data,
}
ua.RegisterStateHandler(regState)
ua.RegisterStateHandler(state)
}
}
if resp != nil {
Expand All @@ -104,12 +107,13 @@ func (r *Register) SendRegister(expires uint32) error {
if len(hdrs) > 0 {
expires = uint32(*(hdrs[0]).(*sip.Expires))
}
regState := account.RegisterState{
Account: *profile,
state := account.RegisterState{
Account: profile,
Response: resp,
StatusCode: resp.StatusCode(),
Reason: resp.Reason(),
Expiration: expires,
UserData: r.data,
}
if expires > 0 {
go func() {
Expand All @@ -131,7 +135,7 @@ func (r *Register) SendRegister(expires uint32) error {
r.timer = nil
}
}
ua.RegisterStateHandler(regState)
ua.RegisterStateHandler(state)
}
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/ua/ua.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
type UserAgentConfig struct {
UserAgent string
SipStack *stack.SipStack
log log.Logger
}

//InviteSessionHandler .
Expand Down Expand Up @@ -133,8 +132,8 @@ func (ua *UserAgent) buildViaHopHeader(target sip.SipUri) *sip.ViaHop {
return viaHop
}

func (ua *UserAgent) SendRegister(profile *account.Profile, recipient sip.SipUri, expires uint32) (*Register, error) {
register := NewRegister(ua, profile, recipient)
func (ua *UserAgent) SendRegister(profile *account.Profile, recipient sip.SipUri, expires uint32, userdata interface{}) (*Register, error) {
register := NewRegister(ua, profile, recipient, userdata)
err := register.SendRegister(expires)
if err != nil {
ua.Log().Errorf("SendRegister failed, err => %v", err)
Expand Down

0 comments on commit 4d2a73d

Please sign in to comment.