diff --git a/Source/WebSocket.swift b/Source/WebSocket.swift index ca0ebe27..a436d786 100644 --- a/Source/WebSocket.swift +++ b/Source/WebSocket.swift @@ -372,9 +372,12 @@ open class WebSocket : NSObject, StreamDelegate { let bytes = UnsafeRawPointer((data as NSData).bytes).assumingMemoryBound(to: UInt8.self) var out = timeout * 1_000_000 // wait 5 seconds before giving up - writeQueue.addOperation { [weak self] in - while !outStream.hasSpaceAvailable { + let operation = BlockOperation() + operation.addExecutionBlock { [weak self, weak operation] in + guard let sOperation = operation else { return } + while !outStream.hasSpaceAvailable && !sOperation.isCancelled { usleep(100) // wait until the socket is ready + guard !sOperation.isCancelled else { return } out -= 100 if out < 0 { self?.cleanupStream() @@ -384,8 +387,10 @@ open class WebSocket : NSObject, StreamDelegate { return // disconnectStream will be called. } } + guard !sOperation.isCancelled else { return } outStream.write(bytes, maxLength: data.count) } + writeQueue.addOperation(operation) } /** @@ -840,9 +845,11 @@ open class WebSocket : NSObject, StreamDelegate { Used to write things to the stream */ private func dequeueWrite(_ data: Data, code: OpCode, writeCompletion: (() -> ())? = nil) { - writeQueue.addOperation { [weak self] in + let operation = BlockOperation() + operation.addExecutionBlock { [weak self, weak operation] in //stream isn't ready, let's wait guard let s = self else { return } + guard let sOperation = operation else { return } var offset = 2 let dataLength = data.count let frame = NSMutableData(capacity: dataLength + s.MaxFrameSize) @@ -869,7 +876,7 @@ open class WebSocket : NSObject, StreamDelegate { offset += 1 } var total = 0 - while true { + while !sOperation.isCancelled { guard let outStream = s.outputStream else { break } let writeBuffer = UnsafeRawPointer(frame!.bytes+total).assumingMemoryBound(to: UInt8.self) let len = outStream.write(writeBuffer, maxLength: offset-total) @@ -896,8 +903,8 @@ open class WebSocket : NSObject, StreamDelegate { break } } - } + writeQueue.addOperation(operation) } /**