Skip to content

Commit b3fd420

Browse files
committed
Merge branch 'development'
* development: SocketAckManager thread-safe acks removal thread-safe ack generation bump websocket Fix building on xcode8.3 double encoding should be getting fixed, make the default false bump websocket
2 parents e38bda7 + c462381 commit b3fd420

File tree

6 files changed

+148
-112
lines changed

6 files changed

+148
-112
lines changed

SocketIO-MacTests/SocketEngineTest.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ class SocketEngineTest: XCTestCase {
8282
XCTAssertEqual(data[0] as? String, "lïne one\nlīne \rtwo", "Failed string test")
8383
expect.fulfill()
8484
}
85-
85+
86+
engine = SocketEngine(client: client, url: URL(string: "http://localhost")!, config: [.doubleEncodeUTF8(true)])
8687
engine.parsePollingMessage("41:42[\"stringTest\",\"lïne one\\nlīne \\rtwo\"]")
88+
8789
waitForExpectations(timeout: 3, handler: nil)
8890
}
8991

Source/SocketAckManager.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,29 @@ private func ==(lhs: SocketAck, rhs: SocketAck) -> Bool {
5151

5252
struct SocketAckManager {
5353
private var acks = Set<SocketAck>(minimumCapacity: 1)
54+
private let ackSemaphore = DispatchSemaphore(value: 1)
5455

5556
mutating func addAck(_ ack: Int, callback: @escaping AckCallback) {
5657
acks.insert(SocketAck(ack: ack, callback: callback))
5758
}
5859

5960
/// Should be called on handle queue
6061
mutating func executeAck(_ ack: Int, with items: [Any], onQueue: DispatchQueue) {
62+
ackSemaphore.wait()
63+
defer { ackSemaphore.signal() }
6164
let ack = acks.remove(SocketAck(ack: ack))
6265

6366
onQueue.async() { ack?.callback(items) }
6467
}
6568

6669
/// Should be called on handle queue
6770
mutating func timeoutAck(_ ack: Int, onQueue: DispatchQueue) {
71+
ackSemaphore.wait()
72+
defer { ackSemaphore.signal() }
6873
let ack = acks.remove(SocketAck(ack: ack))
6974

70-
onQueue.async() { ack?.callback?(["NO ACK"]) }
75+
onQueue.async() {
76+
ack?.callback?(["NO ACK"])
77+
}
7178
}
7279
}

Source/SocketEngine.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
4242
public private(set) var closed = false
4343
public private(set) var connected = false
4444
public private(set) var cookies: [HTTPCookie]?
45-
public private(set) var doubleEncodeUTF8 = true
45+
public private(set) var doubleEncodeUTF8 = false
4646
public private(set) var extraHeaders: [String: String]?
4747
public private(set) var fastUpgrade = false
4848
public private(set) var forcePolling = false

Source/SocketIOClient.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
5454
private var handlers = [SocketEventHandler]()
5555
private var reconnecting = false
5656

57+
private let ackSemaphore = DispatchSemaphore(value: 1)
5758
private(set) var currentAck = -1
5859
private(set) var handleQueue = DispatchQueue.main
5960
private(set) var reconnectAttempts = -1
@@ -161,10 +162,15 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable
161162
}
162163
}
163164

164-
private func createOnAck(_ items: [Any]) -> OnAckCallback {
165+
private func nextAck() -> Int {
166+
ackSemaphore.wait()
167+
defer { ackSemaphore.signal() }
165168
currentAck += 1
166-
167-
return OnAckCallback(ackNumber: currentAck, items: items, socket: self)
169+
return currentAck
170+
}
171+
172+
private func createOnAck(_ items: [Any]) -> OnAckCallback {
173+
return OnAckCallback(ackNumber: nextAck(), items: items, socket: self)
168174
}
169175

170176
func didConnect() {

Source/SocketIOClientConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
public struct SocketIOClientConfiguration : ExpressibleByArrayLiteral, Collection, MutableCollection {
2626
public typealias Element = SocketIOClientOption
2727
public typealias Index = Array<SocketIOClientOption>.Index
28-
public typealias Generator = Array<SocketIOClientOption>.Generator
28+
public typealias Generator = Array<SocketIOClientOption>.Iterator
2929
public typealias SubSequence = Array<SocketIOClientOption>.SubSequence
3030

3131
private var backingArray = [SocketIOClientOption]()

0 commit comments

Comments
 (0)