Files
wgortc/endpoint/helpers.go
2023-04-24 20:10:08 +08:00

48 lines
870 B
Go

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
}