revert to unbroken websocket version
This commit is contained in:
parent
4d07d2911f
commit
e45003210f
@ -115,6 +115,7 @@ public final class SocketEngine: NSObject, SocketEnginePollable, SocketEngineWeb
|
|||||||
deinit {
|
deinit {
|
||||||
DefaultSocketLogger.Logger.log("Engine is being deinit", type: logType)
|
DefaultSocketLogger.Logger.log("Engine is being deinit", type: logType)
|
||||||
closed = true
|
closed = true
|
||||||
|
ws?.disconnect()
|
||||||
stopPolling()
|
stopPolling()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -61,7 +61,7 @@ import Foundation
|
|||||||
|
|
||||||
extension SocketEngineSpec {
|
extension SocketEngineSpec {
|
||||||
func createBinaryDataForSend(data: NSData) -> Either<NSData, String> {
|
func createBinaryDataForSend(data: NSData) -> Either<NSData, String> {
|
||||||
if websocket {
|
if websocket {
|
||||||
var byteArray = [UInt8](count: 1, repeatedValue: 0x4)
|
var byteArray = [UInt8](count: 1, repeatedValue: 0x4)
|
||||||
let mutData = NSMutableData(bytes: &byteArray, length: 1)
|
let mutData = NSMutableData(bytes: &byteArray, length: 1)
|
||||||
|
|
||||||
|
|||||||
@ -111,14 +111,13 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
public var selfSignedSSL = false
|
public var selfSignedSSL = false
|
||||||
private var security: SSLSecurity?
|
private var security: SSLSecurity?
|
||||||
public var enabledSSLCipherSuites: [SSLCipherSuite]?
|
public var enabledSSLCipherSuites: [SSLCipherSuite]?
|
||||||
public var origin: String?
|
|
||||||
public var isConnected :Bool {
|
public var isConnected :Bool {
|
||||||
return connected
|
return connected
|
||||||
}
|
}
|
||||||
public var currentURL: NSURL {return url}
|
|
||||||
private var url: NSURL
|
private var url: NSURL
|
||||||
private var inputStream: NSInputStream?
|
private var inputStream: NSInputStream?
|
||||||
private var outputStream: NSOutputStream?
|
private var outputStream: NSOutputStream?
|
||||||
|
private var isRunLoop = false
|
||||||
private var connected = false
|
private var connected = false
|
||||||
private var isCreated = false
|
private var isCreated = false
|
||||||
private var writeQueue = NSOperationQueue()
|
private var writeQueue = NSOperationQueue()
|
||||||
@ -127,13 +126,10 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
private var fragBuffer: NSData?
|
private var fragBuffer: NSData?
|
||||||
private var certValidated = false
|
private var certValidated = false
|
||||||
private var didDisconnect = false
|
private var didDisconnect = false
|
||||||
//the shared processing queue used for all websocket
|
|
||||||
private static let sharedWorkQueue = dispatch_queue_create("com.vluxe.starscream.websocket", DISPATCH_QUEUE_SERIAL)
|
|
||||||
|
|
||||||
//used for setting protocols.
|
//used for setting protocols.
|
||||||
public init(url: NSURL, protocols: [String]? = nil) {
|
public init(url: NSURL, protocols: [String]? = nil) {
|
||||||
self.url = url
|
self.url = url
|
||||||
self.origin = url.absoluteString
|
|
||||||
writeQueue.maxConcurrentOperationCount = 1
|
writeQueue.maxConcurrentOperationCount = 1
|
||||||
optionalProtocols = protocols
|
optionalProtocols = protocols
|
||||||
}
|
}
|
||||||
@ -141,10 +137,15 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
///Connect to the websocket server on a background thread
|
///Connect to the websocket server on a background thread
|
||||||
public func connect() {
|
public func connect() {
|
||||||
guard !isCreated else { return }
|
guard !isCreated else { return }
|
||||||
didDisconnect = false
|
|
||||||
isCreated = true
|
dispatch_async(queue) { [weak self] in
|
||||||
createHTTPRequest()
|
self?.didDisconnect = false
|
||||||
isCreated = false
|
}
|
||||||
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)) { [weak self] in
|
||||||
|
self?.isCreated = true
|
||||||
|
self?.createHTTPRequest()
|
||||||
|
self?.isCreated = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -174,22 +175,20 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
|
|
||||||
///write a string to the websocket. This sends it as a text frame.
|
///write a string to the websocket. This sends it as a text frame.
|
||||||
public func writeString(str: String) {
|
public func writeString(str: String) {
|
||||||
guard isConnected else { return }
|
|
||||||
dequeueWrite(str.dataUsingEncoding(NSUTF8StringEncoding)!, code: .TextFrame)
|
dequeueWrite(str.dataUsingEncoding(NSUTF8StringEncoding)!, code: .TextFrame)
|
||||||
}
|
}
|
||||||
|
|
||||||
///write binary data to the websocket. This sends it as a binary frame.
|
///write binary data to the websocket. This sends it as a binary frame.
|
||||||
public func writeData(data: NSData) {
|
public func writeData(data: NSData) {
|
||||||
guard isConnected else { return }
|
|
||||||
dequeueWrite(data, code: .BinaryFrame)
|
dequeueWrite(data, code: .BinaryFrame)
|
||||||
}
|
}
|
||||||
|
|
||||||
//write a ping to the websocket. This sends it as a control frame.
|
//write a ping to the websocket. This sends it as a control frame.
|
||||||
//yodel a sound to the planet. This sends it as an astroid. http://youtu.be/Eu5ZJELRiJ8?t=42s
|
//yodel a sound to the planet. This sends it as an astroid. http://youtu.be/Eu5ZJELRiJ8?t=42s
|
||||||
public func writePing(data: NSData) {
|
public func writePing(data: NSData) {
|
||||||
guard isConnected else { return }
|
|
||||||
dequeueWrite(data, code: .Ping)
|
dequeueWrite(data, code: .Ping)
|
||||||
}
|
}
|
||||||
|
//private methods below!
|
||||||
|
|
||||||
//private method that starts the connection
|
//private method that starts the connection
|
||||||
private func createHTTPRequest() {
|
private func createHTTPRequest() {
|
||||||
@ -212,9 +211,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
}
|
}
|
||||||
addHeader(urlRequest, key: headerWSVersionName, val: headerWSVersionValue)
|
addHeader(urlRequest, key: headerWSVersionName, val: headerWSVersionValue)
|
||||||
addHeader(urlRequest, key: headerWSKeyName, val: generateWebSocketKey())
|
addHeader(urlRequest, key: headerWSKeyName, val: generateWebSocketKey())
|
||||||
if let origin = origin {
|
addHeader(urlRequest, key: headerOriginName, val: url.absoluteString)
|
||||||
addHeader(urlRequest, key: headerOriginName, val: origin)
|
|
||||||
}
|
|
||||||
addHeader(urlRequest, key: headerWSHostName, val: "\(url.host!):\(port!)")
|
addHeader(urlRequest, key: headerWSHostName, val: "\(url.host!):\(port!)")
|
||||||
for (key,value) in headers {
|
for (key,value) in headers {
|
||||||
addHeader(urlRequest, key: key, val: value)
|
addHeader(urlRequest, key: key, val: value)
|
||||||
@ -224,12 +221,10 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
initStreamsWithData(serializedRequest, Int(port!))
|
initStreamsWithData(serializedRequest, Int(port!))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Add a header to the CFHTTPMessage by using the NSString bridges to CFString
|
//Add a header to the CFHTTPMessage by using the NSString bridges to CFString
|
||||||
private func addHeader(urlRequest: CFHTTPMessage, key: NSString, val: NSString) {
|
private func addHeader(urlRequest: CFHTTPMessage, key: NSString, val: NSString) {
|
||||||
CFHTTPMessageSetHeaderFieldValue(urlRequest, key, val)
|
CFHTTPMessageSetHeaderFieldValue(urlRequest, key, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
//generate a websocket key as needed in rfc
|
//generate a websocket key as needed in rfc
|
||||||
private func generateWebSocketKey() -> String {
|
private func generateWebSocketKey() -> String {
|
||||||
var key = ""
|
var key = ""
|
||||||
@ -242,7 +237,6 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
let baseKey = data?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
|
let baseKey = data?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
|
||||||
return baseKey!
|
return baseKey!
|
||||||
}
|
}
|
||||||
|
|
||||||
//Start the stream connection and write the data to the output stream
|
//Start the stream connection and write the data to the output stream
|
||||||
private func initStreamsWithData(data: NSData, _ port: Int) {
|
private func initStreamsWithData(data: NSData, _ port: Int) {
|
||||||
//higher level API we will cut over to at some point
|
//higher level API we will cut over to at some point
|
||||||
@ -289,16 +283,15 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CFReadStreamSetDispatchQueue(inStream, WebSocket.sharedWorkQueue)
|
isRunLoop = true
|
||||||
CFWriteStreamSetDispatchQueue(outStream, WebSocket.sharedWorkQueue)
|
inStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
|
||||||
|
outStream.scheduleInRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
|
||||||
inStream.open()
|
inStream.open()
|
||||||
outStream.open()
|
outStream.open()
|
||||||
let bytes = UnsafePointer<UInt8>(data.bytes)
|
let bytes = UnsafePointer<UInt8>(data.bytes)
|
||||||
writeQueue.addOperationWithBlock {
|
outStream.write(bytes, maxLength: data.length)
|
||||||
while !outStream.hasSpaceAvailable {
|
while(isRunLoop) {
|
||||||
usleep(100) //wait until the socket is ready
|
NSRunLoop.currentRunLoop().runMode(NSDefaultRunLoopMode, beforeDate: NSDate.distantFuture() as NSDate)
|
||||||
}
|
|
||||||
outStream.write(bytes, maxLength: data.length)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//delegate for the stream methods. Processes incoming bytes
|
//delegate for the stream methods. Processes incoming bytes
|
||||||
@ -331,16 +324,18 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
private func disconnectStream(error: NSError?) {
|
private func disconnectStream(error: NSError?) {
|
||||||
writeQueue.waitUntilAllOperationsAreFinished()
|
writeQueue.waitUntilAllOperationsAreFinished()
|
||||||
if let stream = inputStream {
|
if let stream = inputStream {
|
||||||
CFReadStreamSetDispatchQueue(stream, nil)
|
stream.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
|
||||||
stream.close()
|
stream.close()
|
||||||
}
|
}
|
||||||
if let stream = outputStream {
|
if let stream = outputStream {
|
||||||
CFWriteStreamSetDispatchQueue(stream, nil)
|
stream.removeFromRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
|
||||||
stream.close()
|
stream.close()
|
||||||
}
|
}
|
||||||
outputStream = nil
|
outputStream = nil
|
||||||
|
isRunLoop = false
|
||||||
certValidated = false
|
certValidated = false
|
||||||
doDisconnect(error)
|
doDisconnect(error)
|
||||||
|
connected = false
|
||||||
}
|
}
|
||||||
|
|
||||||
///handles the incoming bytes and sending them to the proper processing method
|
///handles the incoming bytes and sending them to the proper processing method
|
||||||
@ -350,13 +345,24 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
let length = inputStream!.read(buffer, maxLength: BUFFER_MAX)
|
let length = inputStream!.read(buffer, maxLength: BUFFER_MAX)
|
||||||
|
|
||||||
guard length > 0 else { return }
|
guard length > 0 else { return }
|
||||||
var process = false
|
|
||||||
if inputQueue.count == 0 {
|
if !connected {
|
||||||
process = true
|
connected = processHTTP(buffer, bufferLen: length)
|
||||||
}
|
if !connected {
|
||||||
inputQueue.append(NSData(bytes: buffer, length: length))
|
let response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, false).takeRetainedValue()
|
||||||
if process {
|
CFHTTPMessageAppendBytes(response, buffer, length)
|
||||||
dequeueInput()
|
let code = CFHTTPMessageGetResponseStatusCode(response)
|
||||||
|
doDisconnect(errorWithDetail("Invalid HTTP upgrade", code: UInt16(code)))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var process = false
|
||||||
|
if inputQueue.count == 0 {
|
||||||
|
process = true
|
||||||
|
}
|
||||||
|
inputQueue.append(NSData(bytes: buffer, length: length))
|
||||||
|
if process {
|
||||||
|
dequeueInput()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
///dequeue the incoming input so it is processed in order
|
///dequeue the incoming input so it is processed in order
|
||||||
@ -372,36 +378,13 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
self.fragBuffer = nil
|
self.fragBuffer = nil
|
||||||
}
|
}
|
||||||
let buffer = UnsafePointer<UInt8>(work.bytes)
|
let buffer = UnsafePointer<UInt8>(work.bytes)
|
||||||
let length = work.length
|
processRawMessage(buffer, bufferLen: work.length)
|
||||||
if !connected {
|
|
||||||
processTCPHandshake(buffer, bufferLen: length)
|
|
||||||
} else {
|
|
||||||
processRawMessage(buffer, bufferLen: length)
|
|
||||||
}
|
|
||||||
inputQueue = inputQueue.filter{$0 != data}
|
inputQueue = inputQueue.filter{$0 != data}
|
||||||
dequeueInput()
|
dequeueInput()
|
||||||
}
|
}
|
||||||
|
|
||||||
//handle checking the inital connection status
|
|
||||||
private func processTCPHandshake(buffer: UnsafePointer<UInt8>, bufferLen: Int) {
|
|
||||||
let code = processHTTP(buffer, bufferLen: bufferLen)
|
|
||||||
switch code {
|
|
||||||
case 0:
|
|
||||||
connected = true
|
|
||||||
dispatch_async(queue) { [weak self] in
|
|
||||||
guard let s = self else { return }
|
|
||||||
s.onConnect?()
|
|
||||||
s.delegate?.websocketDidConnect(s)
|
|
||||||
}
|
|
||||||
case -1:
|
|
||||||
fragBuffer = NSData(bytes: buffer, length: bufferLen)
|
|
||||||
break //do nothing, we are going to collect more data
|
|
||||||
default:
|
|
||||||
doDisconnect(errorWithDetail("Invalid HTTP upgrade", code: UInt16(code)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
///Finds the HTTP Packet in the TCP stream, by looking for the CRLF.
|
///Finds the HTTP Packet in the TCP stream, by looking for the CRLF.
|
||||||
private func processHTTP(buffer: UnsafePointer<UInt8>, bufferLen: Int) -> Int {
|
private func processHTTP(buffer: UnsafePointer<UInt8>, bufferLen: Int) -> Bool {
|
||||||
let CRLFBytes = [UInt8(ascii: "\r"), UInt8(ascii: "\n"), UInt8(ascii: "\r"), UInt8(ascii: "\n")]
|
let CRLFBytes = [UInt8(ascii: "\r"), UInt8(ascii: "\n"), UInt8(ascii: "\r"), UInt8(ascii: "\n")]
|
||||||
var k = 0
|
var k = 0
|
||||||
var totalSize = 0
|
var totalSize = 0
|
||||||
@ -417,37 +400,39 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if totalSize > 0 {
|
if totalSize > 0 {
|
||||||
let code = validateResponse(buffer, bufferLen: totalSize)
|
if validateResponse(buffer, bufferLen: totalSize) {
|
||||||
if code != 0 {
|
dispatch_async(queue) { [weak self] in
|
||||||
return code
|
guard let s = self else { return }
|
||||||
|
s.onConnect?()
|
||||||
|
s.delegate?.websocketDidConnect(s)
|
||||||
|
}
|
||||||
|
totalSize += 1 //skip the last \n
|
||||||
|
let restSize = bufferLen - totalSize
|
||||||
|
if restSize > 0 {
|
||||||
|
processRawMessage((buffer+totalSize),bufferLen: restSize)
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
totalSize += 1 //skip the last \n
|
|
||||||
let restSize = bufferLen - totalSize
|
|
||||||
if restSize > 0 {
|
|
||||||
processRawMessage((buffer+totalSize),bufferLen: restSize)
|
|
||||||
}
|
|
||||||
return 0 //success
|
|
||||||
}
|
}
|
||||||
return -1 //was unable to find the full TCP header
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
///validates the HTTP is a 101 as per the RFC spec
|
///validates the HTTP is a 101 as per the RFC spec
|
||||||
private func validateResponse(buffer: UnsafePointer<UInt8>, bufferLen: Int) -> Int {
|
private func validateResponse(buffer: UnsafePointer<UInt8>, bufferLen: Int) -> Bool {
|
||||||
let response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, false).takeRetainedValue()
|
let response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, false).takeRetainedValue()
|
||||||
CFHTTPMessageAppendBytes(response, buffer, bufferLen)
|
CFHTTPMessageAppendBytes(response, buffer, bufferLen)
|
||||||
let code = CFHTTPMessageGetResponseStatusCode(response)
|
if CFHTTPMessageGetResponseStatusCode(response) != 101 {
|
||||||
if code != 101 {
|
return false
|
||||||
return code
|
|
||||||
}
|
}
|
||||||
if let cfHeaders = CFHTTPMessageCopyAllHeaderFields(response) {
|
if let cfHeaders = CFHTTPMessageCopyAllHeaderFields(response) {
|
||||||
let headers = cfHeaders.takeRetainedValue() as NSDictionary
|
let headers = cfHeaders.takeRetainedValue() as NSDictionary
|
||||||
if let acceptKey = headers[headerWSAcceptName] as? NSString {
|
if let acceptKey = headers[headerWSAcceptName] as? NSString {
|
||||||
if acceptKey.length > 0 {
|
if acceptKey.length > 0 {
|
||||||
return 0
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
///read a 16 bit big endian value from a buffer
|
///read a 16 bit big endian value from a buffer
|
||||||
@ -703,6 +688,8 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
}
|
}
|
||||||
///used to write things to the stream
|
///used to write things to the stream
|
||||||
private func dequeueWrite(data: NSData, code: OpCode) {
|
private func dequeueWrite(data: NSData, code: OpCode) {
|
||||||
|
guard isConnected else { return }
|
||||||
|
|
||||||
writeQueue.addOperationWithBlock { [weak self] in
|
writeQueue.addOperationWithBlock { [weak self] in
|
||||||
//stream isn't ready, let's wait
|
//stream isn't ready, let's wait
|
||||||
guard let s = self else { return }
|
guard let s = self else { return }
|
||||||
@ -734,6 +721,9 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
}
|
}
|
||||||
var total = 0
|
var total = 0
|
||||||
while true {
|
while true {
|
||||||
|
if !s.isConnected {
|
||||||
|
break
|
||||||
|
}
|
||||||
guard let outStream = s.outputStream else { break }
|
guard let outStream = s.outputStream else { break }
|
||||||
let writeBuffer = UnsafePointer<UInt8>(frame!.bytes+total)
|
let writeBuffer = UnsafePointer<UInt8>(frame!.bytes+total)
|
||||||
let len = outStream.write(writeBuffer, maxLength: offset-total)
|
let len = outStream.write(writeBuffer, maxLength: offset-total)
|
||||||
@ -761,16 +751,17 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
///used to preform the disconnect delegate
|
///used to preform the disconnect delegate
|
||||||
private func doDisconnect(error: NSError?) {
|
private func doDisconnect(error: NSError?) {
|
||||||
guard !didDisconnect else { return }
|
guard !didDisconnect else { return }
|
||||||
didDisconnect = true
|
|
||||||
connected = false
|
|
||||||
dispatch_async(queue) { [weak self] in
|
dispatch_async(queue) { [weak self] in
|
||||||
guard let s = self else { return }
|
guard let s = self else { return }
|
||||||
|
s.didDisconnect = true
|
||||||
s.onDisconnect?(error)
|
s.onDisconnect?(error)
|
||||||
s.delegate?.websocketDidDisconnect(s, error: error)
|
s.delegate?.websocketDidDisconnect(s, error: error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class SSLCert {
|
private class SSLCert {
|
||||||
var certData: NSData?
|
var certData: NSData?
|
||||||
var key: SecKeyRef?
|
var key: SecKeyRef?
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user