@ -509,7 +509,15 @@ class PeerConnectionClient: NSObject, RTCPeerConnectionDelegate, RTCDataChannelD
// MARK: - D a t a C h a n n e l
public func sendDataChannelMessage ( data : Data , description : String , isCritical : Bool = false ) {
// s h o u l d o n l y b e a c c e s s e d o n P e e r C o n n e c t i o n C l i e n t . s i g n a l i n g Q u e u e
var pendingDataChannelMessages : [ PendingDataChannelMessage ] = [ ]
struct PendingDataChannelMessage {
let data : Data
let description : String
let isCritical : Bool
}
public func sendDataChannelMessage ( data : Data , description : String , isCritical : Bool ) {
AssertIsOnMainThread ( )
PeerConnectionClient . signalingQueue . async {
@ -519,9 +527,11 @@ class PeerConnectionClient: NSObject, RTCPeerConnectionDelegate, RTCDataChannelD
}
guard let dataChannel = self . dataChannel else {
Logger . error ( " \( self . TAG ) in \( #function ) ignoring sending \( data ) for nil dataChannel: \( description ) " )
if isCritical {
owsFail ( " \( self . TAG ) in \( #function ) ignoring sending \( data ) for nil dataChannel: \( description ) " )
Logger . info ( " \( self . TAG ) in \( #function ) enqueuing critical data channel message for after we have a dataChannel: \( description ) " )
self . pendingDataChannelMessages . append ( PendingDataChannelMessage ( data : data , description : description , isCritical : isCritical ) )
} else {
Logger . error ( " \( self . TAG ) in \( #function ) ignoring sending \( data ) for nil dataChannel: \( description ) " )
}
return
}
@ -708,6 +718,14 @@ class PeerConnectionClient: NSObject, RTCPeerConnectionDelegate, RTCDataChannelD
assert ( self . dataChannel = = nil )
self . dataChannel = dataChannel
dataChannel . delegate = self
let pendingMessages = self . pendingDataChannelMessages
self . pendingDataChannelMessages = [ ]
DispatchQueue . main . async {
pendingMessages . forEach { message in
self . sendDataChannelMessage ( data : message . data , description : message . description , isCritical : message . isCritical )
}
}
}
}