Adding OnDial handler for datachannels

Adds an `OnDial` handler to be fired once the dialing side has sent
the DCEP OPEN message over the data channel.
This commit is contained in:
treyhakanson
2023-01-29 16:24:51 -08:00
committed by David Zhao
parent 3c802f7169
commit 657dab7b97
2 changed files with 105 additions and 0 deletions

View File

@@ -49,6 +49,8 @@ type DataChannel struct {
onMessageHandler func(DataChannelMessage)
openHandlerOnce sync.Once
onOpenHandler func()
dialHandlerOnce sync.Once
onDialHandler func()
onCloseHandler func()
onBufferedAmountLow func()
onErrorHandler func(error)
@@ -175,6 +177,7 @@ func (d *DataChannel) open(sctpTransport *SCTPTransport) error {
dc.OnBufferedAmountLow(d.onBufferedAmountLow)
d.mu.Unlock()
d.onDial()
d.handleOpen(dc, false, d.negotiated)
return nil
}
@@ -228,6 +231,30 @@ func (d *DataChannel) onOpen() {
}
}
// OnDial sets an event handler which is invoked when the
// peer has been dialed, but before said peer has responsed
func (d *DataChannel) OnDial(f func()) {
d.mu.Lock()
d.dialHandlerOnce = sync.Once{}
d.onDialHandler = f
d.mu.Unlock()
if d.ReadyState() == DataChannelStateOpen {
// If the data channel is already open, call the handler immediately.
go d.dialHandlerOnce.Do(f)
}
}
func (d *DataChannel) onDial() {
d.mu.RLock()
handler := d.onDialHandler
d.mu.RUnlock()
if handler != nil {
go d.dialHandlerOnce.Do(handler)
}
}
// OnClose sets an event handler which is invoked when
// the underlying data transport has been closed.
func (d *DataChannel) OnClose(f func()) {