1+ // The content below is the concatenation of the files in Starscream (https://github.com/daltoniam/Starscream)
2+ // with trivial warnings fixed, trailing spaces removed, and some access levels changed.
13//
2- // Starscream.swift
3- // PusherSwift
4- //
5- // Created by Hamilton Chapman on 06/04/2016.
6- //
7- // The content below is the concatenation of these files
8- // - https://raw.githubusercontent.com/daltoniam/Starscream/swift-23/Source/SSLSecurity.swift
9- // - https://raw.githubusercontent.com/daltoniam/Starscream/swift-23/Source/WebSocket.swift
10- // (with trivial warnings fixed, trailing spaces removed, and some access levels changed)
11- //
12- // commit SHA ee993322c
4+ // Based on commit SHA 789264eeff101e, but without some docs fixes from ee993322c
5+ // onwards, and without the compression support.
136
147//////////////////////////////////////////////////////////////////////////////////////////////////
158//
@@ -394,7 +387,7 @@ open class WebSocket : NSObject, StreamDelegate {
394387
395388 // MARK: - Block based API.
396389
397- public var onConnect : ( ( Void ) -> Void ) ?
390+ public var onConnect : ( ( ) -> Void ) ?
398391 public var onDisconnect : ( ( NSError ? ) -> Void ) ?
399392 public var onText : ( ( String ) -> Void ) ?
400393 public var onData : ( ( Data ) -> Void ) ?
@@ -408,7 +401,10 @@ open class WebSocket : NSObject, StreamDelegate {
408401 public var origin : String ?
409402 public var timeout = 5
410403 public var isConnected : Bool {
411- return connected
404+ connectedMutex. lock ( )
405+ let isConnected = connected
406+ connectedMutex. unlock ( )
407+ return isConnected
412408 }
413409
414410 public var currentURL : URL { return url }
@@ -420,19 +416,20 @@ open class WebSocket : NSObject, StreamDelegate {
420416 private var outputStream : OutputStream ?
421417 private var connected = false
422418 private var isConnecting = false
419+ private let connectedMutex = NSLock ( )
423420 private var writeQueue = OperationQueue ( )
424421 private var readStack = [ WSResponse] ( )
425422 private var inputQueue = [ Data] ( )
426423 private var fragBuffer : Data ?
427424 private var certValidated = false
428425 private var didDisconnect = false
429426 private var readyToWrite = false
430- private let mutex = NSLock ( )
427+ private let readyToWriteMutex = NSLock ( )
431428 private let notificationCenter = NotificationCenter . default
432429 private var canDispatch : Bool {
433- mutex . lock ( )
430+ readyToWriteMutex . lock ( )
434431 let canWork = readyToWrite
435- mutex . unlock ( )
432+ readyToWriteMutex . unlock ( )
436433 return canWork
437434 }
438435 /// The shared processing queue used for all WebSocket.
@@ -484,12 +481,17 @@ open class WebSocket : NSObject, StreamDelegate {
484481 let milliseconds = Int ( seconds * 1_000 )
485482 callbackQueue. asyncAfter ( deadline: . now( ) + . milliseconds( milliseconds) ) { [ weak self] in
486483 self ? . disconnectStream ( nil )
484+ WebSocket . sharedWorkQueue. async {
485+ self ? . disconnectStream ( nil )
486+ }
487487 }
488488 fallthrough
489489 case . none:
490490 writeError ( closeCode)
491491 default :
492- disconnectStream ( nil )
492+ WebSocket . sharedWorkQueue. async { [ weak self] in
493+ self ? . disconnectStream ( nil )
494+ }
493495 break
494496 }
495497 }
@@ -620,13 +622,17 @@ open class WebSocket : NSObject, StreamDelegate {
620622 let resIn = SSLSetEnabledCiphers ( sslContextIn, cipherSuites, cipherSuites. count)
621623 let resOut = SSLSetEnabledCiphers ( sslContextOut, cipherSuites, cipherSuites. count)
622624 if resIn != errSecSuccess {
623- let error = self . errorWithDetail ( " Error setting ingoing cypher suites " , code: UInt16 ( resIn) )
624- disconnectStream ( error)
625+ WebSocket . sharedWorkQueue. async { [ weak self] in
626+ let error = self ? . errorWithDetail ( " Error setting ingoing cypher suites " , code: UInt16 ( resIn) )
627+ self ? . disconnectStream ( error)
628+ }
625629 return
626630 }
627631 if resOut != errSecSuccess {
628- let error = self . errorWithDetail ( " Error setting outgoing cypher suites " , code: UInt16 ( resOut) )
629- disconnectStream ( error)
632+ WebSocket . sharedWorkQueue. async { [ weak self] in
633+ let error = self ? . errorWithDetail ( " Error setting outgoing cypher suites " , code: UInt16 ( resOut) )
634+ self ? . disconnectStream ( error)
635+ }
630636 return
631637 }
632638 }
@@ -644,9 +650,9 @@ open class WebSocket : NSObject, StreamDelegate {
644650 inStream. open ( )
645651 outStream. open ( )
646652
647- self . mutex . lock ( )
653+ self . readyToWriteMutex . lock ( )
648654 self . readyToWrite = true
649- self . mutex . unlock ( )
655+ self . readyToWriteMutex . unlock ( )
650656
651657 let bytes = UnsafeRawPointer ( ( data as NSData ) . bytes) . assumingMemoryBound ( to: UInt8 . self)
652658 var out = timeout * 1_000_000 // wait 5 seconds before giving up
@@ -670,9 +676,12 @@ open class WebSocket : NSObject, StreamDelegate {
670676 guard !sOperation. isCancelled, let s = self else { return }
671677 // Do the pinning now if needed
672678 if let sec = s. security, !s. certValidated {
673- let trust = outStream. property ( forKey: kCFStreamPropertySSLPeerTrust as Stream . PropertyKey ) as! SecTrust
674- let domain = outStream. property ( forKey: kCFStreamSSLPeerName as Stream . PropertyKey ) as? String
675- s. certValidated = sec. isValid ( trust, domain: domain)
679+ if let possibleTrust = outStream. property ( forKey: kCFStreamPropertySSLPeerTrust as Stream . PropertyKey ) {
680+ let domain = outStream. property ( forKey: kCFStreamSSLPeerName as Stream . PropertyKey ) as? String
681+ s. certValidated = sec. isValid ( possibleTrust as! SecTrust , domain: domain)
682+ } else {
683+ s. certValidated = false
684+ }
676685 if !s. certValidated {
677686 WebSocket . sharedWorkQueue. async {
678687 let error = s. errorWithDetail ( " Invalid SSL certificate " , code: 1 )
@@ -711,7 +720,9 @@ open class WebSocket : NSObject, StreamDelegate {
711720 writeQueue. cancelAllOperations ( )
712721 }
713722 cleanupStream ( )
723+ connectedMutex. lock ( )
714724 connected = false
725+ connectedMutex. unlock ( )
715726 if runDelegate {
716727 doDisconnect ( error)
717728 }
@@ -820,7 +831,9 @@ open class WebSocket : NSObject, StreamDelegate {
820831 return code
821832 }
822833 isConnecting = false
834+ connectedMutex. lock ( )
823835 connected = true
836+ connectedMutex. unlock ( )
824837 didDisconnect = false
825838 if canDispatch {
826839 callbackQueue. async { [ weak self] in
@@ -1202,7 +1215,9 @@ open class WebSocket : NSObject, StreamDelegate {
12021215 guard !didDisconnect else { return }
12031216 didDisconnect = true
12041217 isConnecting = false
1218+ connectedMutex. lock ( )
12051219 connected = false
1220+ connectedMutex. unlock ( )
12061221 guard canDispatch else { return }
12071222 callbackQueue. async { [ weak self] in
12081223 guard let s = self else { return }
@@ -1216,9 +1231,9 @@ open class WebSocket : NSObject, StreamDelegate {
12161231 // MARK: - Deinit
12171232
12181233 deinit {
1219- mutex . lock ( )
1234+ readyToWriteMutex . lock ( )
12201235 readyToWrite = false
1221- mutex . unlock ( )
1236+ readyToWriteMutex . unlock ( )
12221237 cleanupStream ( )
12231238 writeQueue. cancelAllOperations ( )
12241239 }
0 commit comments