-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathdialog.go
141 lines (115 loc) · 3 KB
/
dialog.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
package sipgo
import (
"context"
"errors"
"fmt"
"sync/atomic"
"github.com/emiago/sipgo/sip"
)
var (
ErrDialogOutsideDialog = errors.New("Call/Transaction Outside Dialog")
ErrDialogDoesNotExists = errors.New("Call/Transaction Does Not Exist")
ErrDialogInviteNoContact = errors.New("No Contact header")
ErrDialogInvalidCseq = errors.New("Invalid CSEQ number")
// ErrDialogCanceled matches invite transaction canceled
ErrDialogCanceled = sip.ErrTransactionCanceled
)
type ErrDialogResponse struct {
Res *sip.Response
}
func (e ErrDialogResponse) Error() string {
return fmt.Sprintf("Invite failed with response: %s", e.Res.StartLine())
}
type DialogStateFn func(s sip.DialogState)
type Dialog struct {
ID string
// InviteRequest is set when dialog is created. It is not thread safe!
// Use it only as read only and use methods to change headers
InviteRequest *sip.Request
// lastCSeqNo is set for every request within dialog except ACK CANCEL
lastCSeqNo atomic.Uint32
// InviteResponse is last response received or sent. It is not thread safe!
// Use it only as read only and do not change values
InviteResponse *sip.Response
state atomic.Int32
ctx context.Context
cancel context.CancelCauseFunc
onStatePointer atomic.Pointer[DialogStateFn]
}
// Init setups dialog state
func (d *Dialog) Init() {
d.ctx, d.cancel = context.WithCancelCause(context.Background())
d.state = atomic.Int32{}
d.lastCSeqNo = atomic.Uint32{}
// We may have sequence number initialized
if cseq := d.InviteRequest.CSeq(); cseq != nil {
d.lastCSeqNo.Store(cseq.SeqNo)
}
d.onStatePointer = atomic.Pointer[DialogStateFn]{}
}
func (d *Dialog) OnState(f DialogStateFn) {
for current := d.onStatePointer.Load(); current != nil; current = d.onStatePointer.Load() {
cb := *current
newCb := func(s sip.DialogState) {
f(s)
cb(s)
}
newCBState := DialogStateFn(newCb)
if d.onStatePointer.CompareAndSwap(current, &newCBState) {
return
}
}
d.onStatePointer.Store(&f)
}
func (d *Dialog) InitWithState(s sip.DialogState) {
d.Init()
d.state.Store(int32(s))
}
func (d *Dialog) setState(s sip.DialogState) {
old := d.state.Swap(int32(s))
if old == int32(s) {
// Safety
return
}
if s == sip.DialogStateEnded {
d.cancel(nil)
}
if f := d.onStatePointer.Load(); f != nil {
cb := *f
cb(s)
}
}
// endWithCause sets dialog state ended and place context cause error
// Experimental
func (d *Dialog) endWithCause(err error) {
s := sip.DialogStateEnded
old := d.state.Swap(int32(s))
if old == int32(s) {
// Safety
return
}
d.cancel(err)
if f := d.onStatePointer.Load(); f != nil {
cb := *f
cb(s)
}
}
func (d *Dialog) LoadState() sip.DialogState {
return sip.DialogState(d.state.Load())
}
func (d *Dialog) StateRead() <-chan sip.DialogState {
ch := make(chan sip.DialogState, 5)
d.OnState(func(s sip.DialogState) {
select {
case ch <- s:
default:
}
})
return ch
}
func (d *Dialog) CSEQ() uint32 {
return d.lastCSeqNo.Load()
}
func (d *Dialog) Context() context.Context {
return d.ctx
}