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

Fixed issues with not cancelling block operations properly #302

Merged
merged 1 commit into from
Jan 11, 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
17 changes: 12 additions & 5 deletions Source/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}

/**
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -896,8 +903,8 @@ open class WebSocket : NSObject, StreamDelegate {
break
}
}

}
writeQueue.addOperation(operation)
}

/**
Expand Down