-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
47 lines (37 loc) · 870 Bytes
/
helpers.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
package endpoint
import (
"context"
"errors"
"time"
"github.com/pion/webrtc/v3"
)
func refVal[T any](v T) *T { return &v }
var ErrDataChannelClosed = errors.New("DataChannel state is closed")
func WaitDC(dc *webrtc.DataChannel, timeout time.Duration) (err error) {
switch dc.ReadyState() {
case webrtc.DataChannelStateOpen:
return
case webrtc.DataChannelStateClosing:
fallthrough
case webrtc.DataChannelStateClosed:
return ErrDataChannelClosed
case webrtc.DataChannelStateConnecting:
break
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
ctx, cancelWith := context.WithCancelCause(ctx)
dc.OnOpen(func() {
cancelWith(nil)
})
dc.OnClose(func() {
cancelWith(ErrDataChannelClosed)
})
<-ctx.Done()
switch err = context.Cause(ctx); err {
case context.Canceled:
return nil
}
return
}