@@ -512,7 +512,7 @@ private class Deflater {
512
512
/// WebSocket objects are bidirectional network streams that communicate over HTTP. RFC 6455.
513
513
private class InnerWebSocket : Hashable {
514
514
var id : Int
515
- var mutex = pthread_mutex_t ( )
515
+ var mutex = UnsafeMutablePointer < pthread_mutex_t> . allocate ( capacity : 1 )
516
516
let request : URLRequest !
517
517
let subProtocols : [ String ] !
518
518
var frames : [ Frame ] = [ ]
@@ -611,9 +611,10 @@ private class InnerWebSocket: Hashable {
611
611
func hash( into hasher: inout Hasher ) {
612
612
hasher. combine ( id)
613
613
}
614
-
614
+
615
615
init ( request: URLRequest , subProtocols : [ String ] = [ ] , stub : Bool = false ) {
616
- pthread_mutex_init ( & mutex, nil )
616
+ mutex. initialize ( to: pthread_mutex_t ( ) )
617
+ pthread_mutex_init ( mutex, nil )
617
618
self . id = manager. nextId ( )
618
619
self . request = request
619
620
self . subProtocols = subProtocols
@@ -639,13 +640,14 @@ private class InnerWebSocket: Hashable {
639
640
if inputBytes != nil {
640
641
free ( inputBytes)
641
642
}
642
- pthread_mutex_init ( & mutex, nil )
643
+ pthread_mutex_init ( mutex, nil )
644
+ mutex. deallocate ( )
643
645
}
644
646
@inline ( __always) fileprivate func lock( ) {
645
- pthread_mutex_lock ( & mutex)
647
+ pthread_mutex_lock ( mutex)
646
648
}
647
649
@inline ( __always) fileprivate func unlock( ) {
648
- pthread_mutex_unlock ( & mutex)
650
+ pthread_mutex_unlock ( mutex)
649
651
}
650
652
651
653
fileprivate var dirty : Bool {
@@ -1035,7 +1037,7 @@ private class InnerWebSocket: Hashable {
1035
1037
security = . none
1036
1038
}
1037
1039
1038
- var path = CFURLCopyPath ( req. url! as CFURL ? ) as String
1040
+ var path = CFURLCopyPath ( req. url! as CFURL ) as String
1039
1041
if path == " " {
1040
1042
path = " / "
1041
1043
}
@@ -1069,7 +1071,7 @@ private class InnerWebSocket: Hashable {
1069
1071
var ( rdo, wro) : ( InputStream ? , OutputStream ? )
1070
1072
var readStream : Unmanaged < CFReadStream > ?
1071
1073
var writeStream : Unmanaged < CFWriteStream > ?
1072
- CFStreamCreatePairWithSocketToHost ( nil , addr [ 0 ] as CFString ? , UInt32 ( Int ( addr [ 1 ] ) !) , & readStream, & writeStream) ;
1074
+ CFStreamCreatePairWithSocketToHost ( nil , addr [ 0 ] as CFString , UInt32 ( Int ( addr [ 1 ] ) !) , & readStream, & writeStream) ;
1073
1075
rdo = readStream!. takeRetainedValue ( )
1074
1076
wro = writeStream!. takeRetainedValue ( )
1075
1077
( rd, wr) = ( rdo!, wro!)
@@ -1155,8 +1157,8 @@ private class InnerWebSocket: Hashable {
1155
1157
} else {
1156
1158
key = " "
1157
1159
if let r = line. range ( of: " : " ) {
1158
- key = trim ( String ( line. prefix ( upTo : r. lowerBound) ) )
1159
- value = trim ( String ( line. suffix ( from : r. upperBound) ) )
1160
+ key = trim ( String ( line [ ..< r. lowerBound] ) )
1161
+ value = trim ( String ( line [ r. upperBound... ] ) )
1160
1162
}
1161
1163
}
1162
1164
@@ -1613,38 +1615,43 @@ private enum TCPConnSecurity {
1613
1615
private class Manager {
1614
1616
var queue = DispatchQueue ( label: " SwiftWebSocketInstance " , attributes: [ ] )
1615
1617
var once = Int ( )
1616
- var mutex = pthread_mutex_t ( )
1618
+ var mutex = UnsafeMutablePointer < pthread_mutex_t> . allocate ( capacity : 1 )
1617
1619
var cond = pthread_cond_t ( )
1618
1620
var websockets = Set < InnerWebSocket > ( )
1619
1621
var _nextId = 0
1620
1622
init ( ) {
1621
- pthread_mutex_init ( & mutex, nil )
1623
+ mutex. initialize ( to: pthread_mutex_t ( ) )
1624
+ pthread_mutex_init ( mutex, nil )
1622
1625
pthread_cond_init ( & cond, nil )
1623
1626
DispatchQueue ( label: " SwiftWebSocket " , attributes: [ ] ) . async {
1624
1627
var wss : [ InnerWebSocket ] = [ ]
1625
1628
while true {
1626
1629
var wait = true
1627
1630
wss. removeAll ( )
1628
- pthread_mutex_lock ( & self . mutex)
1631
+ pthread_mutex_lock ( self . mutex)
1629
1632
for ws in self . websockets {
1630
1633
wss. append ( ws)
1631
1634
}
1632
1635
for ws in wss {
1633
1636
self . checkForConnectionTimeout ( ws)
1634
1637
if ws. dirty {
1635
- pthread_mutex_unlock ( & self . mutex)
1638
+ pthread_mutex_unlock ( self . mutex)
1636
1639
ws. step ( )
1637
- pthread_mutex_lock ( & self . mutex)
1640
+ pthread_mutex_lock ( self . mutex)
1638
1641
wait = false
1639
1642
}
1640
1643
}
1641
1644
if wait {
1642
1645
_ = self . wait ( 250 )
1643
1646
}
1644
- pthread_mutex_unlock ( & self . mutex)
1647
+ pthread_mutex_unlock ( self . mutex)
1645
1648
}
1646
1649
}
1647
1650
}
1651
+ deinit {
1652
+ pthread_mutex_init ( mutex, nil )
1653
+ mutex. deallocate ( )
1654
+ }
1648
1655
func checkForConnectionTimeout( _ ws : InnerWebSocket ) {
1649
1656
if ws. rd != nil && ws. wr != nil && ( ws. rd. streamStatus == . opening || ws. wr. streamStatus == . opening) {
1650
1657
let age = CFAbsoluteTimeGetCurrent ( ) - ws. createdAt
@@ -1663,28 +1670,28 @@ private class Manager {
1663
1670
ts. tv_nsec = v1 + v2;
1664
1671
ts. tv_sec += ts. tv_nsec / ( 1000 * 1000 * 1000 ) ;
1665
1672
ts. tv_nsec %= ( 1000 * 1000 * 1000 ) ;
1666
- return pthread_cond_timedwait ( & self . cond, & self . mutex, & ts)
1673
+ return pthread_cond_timedwait ( & self . cond, self . mutex, & ts)
1667
1674
}
1668
1675
func signal( ) {
1669
- pthread_mutex_lock ( & mutex)
1676
+ pthread_mutex_lock ( mutex)
1670
1677
pthread_cond_signal ( & cond)
1671
- pthread_mutex_unlock ( & mutex)
1678
+ pthread_mutex_unlock ( mutex)
1672
1679
}
1673
1680
func add( _ websocket: InnerWebSocket ) {
1674
- pthread_mutex_lock ( & mutex)
1681
+ pthread_mutex_lock ( mutex)
1675
1682
websockets. insert ( websocket)
1676
1683
pthread_cond_signal ( & cond)
1677
- pthread_mutex_unlock ( & mutex)
1684
+ pthread_mutex_unlock ( mutex)
1678
1685
}
1679
1686
func remove( _ websocket: InnerWebSocket ) {
1680
- pthread_mutex_lock ( & mutex)
1687
+ pthread_mutex_lock ( mutex)
1681
1688
websockets. remove ( websocket)
1682
1689
pthread_cond_signal ( & cond)
1683
- pthread_mutex_unlock ( & mutex)
1690
+ pthread_mutex_unlock ( mutex)
1684
1691
}
1685
1692
func nextId( ) -> Int {
1686
- pthread_mutex_lock ( & mutex)
1687
- defer { pthread_mutex_unlock ( & mutex) }
1693
+ pthread_mutex_lock ( mutex)
1694
+ defer { pthread_mutex_unlock ( mutex) }
1688
1695
_nextId += 1
1689
1696
return _nextId
1690
1697
}
@@ -1693,11 +1700,17 @@ private class Manager {
1693
1700
private let manager = Manager ( )
1694
1701
1695
1702
/// WebSocket objects are bidirectional network streams that communicate over HTTP. RFC 6455.
1703
+ @objcMembers
1696
1704
open class WebSocket : NSObject {
1697
1705
fileprivate var ws : InnerWebSocket
1698
1706
fileprivate var id = manager. nextId ( )
1699
1707
fileprivate var opened : Bool
1708
+
1700
1709
open override var hash : Int { return id }
1710
+ open override func isEqual( _ other: Any ? ) -> Bool {
1711
+ guard let other = other as? WebSocket else { return false }
1712
+ return self . id == other. id
1713
+ }
1701
1714
1702
1715
/// Create a WebSocket connection to a URL; this should be the URL to which the WebSocket server will respond.
1703
1716
public convenience init ( _ url: String ) {
@@ -1861,6 +1874,7 @@ public func ==(lhs: WebSocket, rhs: WebSocket) -> Bool {
1861
1874
1862
1875
extension WebSocket {
1863
1876
/// The events of the WebSocket using a delegate.
1877
+ @objc
1864
1878
public var delegate : WebSocketDelegate ? {
1865
1879
get { return ws. eventDelegate }
1866
1880
set { ws. eventDelegate = newValue }
0 commit comments