Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race on connected #355

Merged
merged 1 commit into from
Jul 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions Source/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ open class WebSocket : NSObject, StreamDelegate {
public var origin: String?
public var timeout = 5
public var isConnected: Bool {
return connected
connectedMutex.lock()
let isConnected = connected
connectedMutex.unlock()
return isConnected
}

public var currentURL: URL { return url }
Expand All @@ -200,6 +203,7 @@ open class WebSocket : NSObject, StreamDelegate {
private var outputStream: OutputStream?
private var connected = false
private var isConnecting = false
private let connectedMutex = NSLock()
private var compressionState = CompressionState()
private var writeQueue = OperationQueue()
private var readStack = [WSResponse]()
Expand All @@ -209,12 +213,12 @@ open class WebSocket : NSObject, StreamDelegate {
private var didDisconnect = false
private var readyToWrite = false
private var headerSecKey = ""
private let mutex = NSLock()
private let readyToWriteMutex = NSLock()
private let notificationCenter = NotificationCenter.default
private var canDispatch: Bool {
mutex.lock()
readyToWriteMutex.lock()
let canWork = readyToWrite
mutex.unlock()
readyToWriteMutex.unlock()
return canWork
}
/// The shared processing queue used for all WebSocket.
Expand Down Expand Up @@ -428,9 +432,9 @@ open class WebSocket : NSObject, StreamDelegate {
inStream.open()
outStream.open()

self.mutex.lock()
self.readyToWriteMutex.lock()
self.readyToWrite = true
self.mutex.unlock()
self.readyToWriteMutex.unlock()

let bytes = UnsafeRawPointer((data as NSData).bytes).assumingMemoryBound(to: UInt8.self)
var out = timeout * 1_000_000 // wait 5 seconds before giving up
Expand Down Expand Up @@ -500,7 +504,9 @@ open class WebSocket : NSObject, StreamDelegate {
writeQueue.cancelAllOperations()
}
cleanupStream()
connectedMutex.lock()
connected = false
connectedMutex.unlock()
if runDelegate {
doDisconnect(error)
}
Expand Down Expand Up @@ -609,7 +615,9 @@ open class WebSocket : NSObject, StreamDelegate {
return code
}
isConnecting = false
connectedMutex.lock()
connected = true
connectedMutex.unlock()
didDisconnect = false
if canDispatch {
callbackQueue.async { [weak self] in
Expand Down Expand Up @@ -1068,7 +1076,9 @@ open class WebSocket : NSObject, StreamDelegate {
guard !didDisconnect else { return }
didDisconnect = true
isConnecting = false
connectedMutex.lock()
connected = false
connectedMutex.unlock()
guard canDispatch else {return}
callbackQueue.async { [weak self] in
guard let s = self else { return }
Expand All @@ -1083,9 +1093,9 @@ open class WebSocket : NSObject, StreamDelegate {
// MARK: - Deinit

deinit {
mutex.lock()
readyToWriteMutex.lock()
readyToWrite = false
mutex.unlock()
readyToWriteMutex.unlock()
cleanupStream()
writeQueue.cancelAllOperations()
}
Expand Down