refactor. Remove var from methods and pattern matching
This commit is contained in:
parent
fff2f29c55
commit
d94a1901ca
@ -147,13 +147,12 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func checkIfMessageIsBase64Binary(var message: String) -> Bool {
|
private func checkIfMessageIsBase64Binary(message: String) -> Bool {
|
||||||
if message.hasPrefix("b4") {
|
if message.hasPrefix("b4") {
|
||||||
// binary in base64 string
|
// binary in base64 string
|
||||||
message.removeRange(Range(start: message.startIndex,
|
let noPrefix = message[message.startIndex.advancedBy(2)..<message.endIndex]
|
||||||
end: message.startIndex.advancedBy(2)))
|
|
||||||
|
|
||||||
if let data = NSData(base64EncodedString: message,
|
if let data = NSData(base64EncodedString: noPrefix,
|
||||||
options: .IgnoreUnknownCharacters) {
|
options: .IgnoreUnknownCharacters) {
|
||||||
client?.parseBinaryData(data)
|
client?.parseBinaryData(data)
|
||||||
}
|
}
|
||||||
@ -282,17 +281,15 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
|
|||||||
private func flushProbeWait() {
|
private func flushProbeWait() {
|
||||||
DefaultSocketLogger.Logger.log("Flushing probe wait", type: logType)
|
DefaultSocketLogger.Logger.log("Flushing probe wait", type: logType)
|
||||||
|
|
||||||
dispatch_async(emitQueue) {[weak self] in
|
dispatch_async(emitQueue) {
|
||||||
if let this = self {
|
for waiter in self.probeWait {
|
||||||
for waiter in this.probeWait {
|
self.write(waiter.msg, withType: waiter.type, withData: waiter.data)
|
||||||
this.write(waiter.msg, withType: waiter.type, withData: waiter.data)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
this.probeWait.removeAll(keepCapacity: false)
|
self.probeWait.removeAll(keepCapacity: false)
|
||||||
|
|
||||||
if this.postWait.count != 0 {
|
if self.postWait.count != 0 {
|
||||||
this.flushWaitingForPostToWebSocket()
|
self.flushWaitingForPostToWebSocket()
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -420,10 +417,11 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
|
|||||||
client?.parseBinaryData(data.subdataWithRange(NSMakeRange(1, data.length - 1)))
|
client?.parseBinaryData(data.subdataWithRange(NSMakeRange(1, data.length - 1)))
|
||||||
}
|
}
|
||||||
|
|
||||||
private func parseEngineMessage(var message: String, fromPolling: Bool) {
|
private func parseEngineMessage(message: String, fromPolling: Bool) {
|
||||||
DefaultSocketLogger.Logger.log("Got message: %@", type: logType, args: message)
|
DefaultSocketLogger.Logger.log("Got message: %@", type: logType, args: message)
|
||||||
|
|
||||||
let reader = SocketStringReader(message: message)
|
let reader = SocketStringReader(message: message)
|
||||||
|
let fixedString: String
|
||||||
|
|
||||||
guard let type = SocketEnginePacketType(rawValue: Int(reader.currentCharacter) ?? -1) else {
|
guard let type = SocketEnginePacketType(rawValue: Int(reader.currentCharacter) ?? -1) else {
|
||||||
if !checkIfMessageIsBase64Binary(message) {
|
if !checkIfMessageIsBase64Binary(message) {
|
||||||
@ -434,22 +432,22 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if fromPolling && type != .Noop {
|
if fromPolling && type != .Noop {
|
||||||
fixDoubleUTF8(&message)
|
fixedString = fixDoubleUTF8(message)
|
||||||
|
} else {
|
||||||
|
fixedString = message
|
||||||
}
|
}
|
||||||
|
|
||||||
switch type {
|
switch type {
|
||||||
case .Message:
|
case .Message:
|
||||||
message.removeAtIndex(message.startIndex)
|
handleMessage(fixedString[fixedString.startIndex.successor()..<fixedString.endIndex])
|
||||||
handleMessage(message)
|
|
||||||
case .Noop:
|
case .Noop:
|
||||||
handleNOOP()
|
handleNOOP()
|
||||||
case .Pong:
|
case .Pong:
|
||||||
handlePong(message)
|
handlePong(fixedString)
|
||||||
case .Open:
|
case .Open:
|
||||||
message.removeAtIndex(message.startIndex)
|
handleOpen(fixedString[fixedString.startIndex.successor()..<fixedString.endIndex])
|
||||||
handleOpen(message)
|
|
||||||
case .Close:
|
case .Close:
|
||||||
handleClose(message)
|
handleClose(fixedString)
|
||||||
default:
|
default:
|
||||||
DefaultSocketLogger.Logger.log("Got unknown packet type", type: logType)
|
DefaultSocketLogger.Logger.log("Got unknown packet type", type: logType)
|
||||||
}
|
}
|
||||||
@ -567,7 +565,6 @@ extension SocketEngine {
|
|||||||
let req = NSMutableURLRequest(URL: NSURL(string: urlPolling + "&sid=\(sid)&b64=1")!)
|
let req = NSMutableURLRequest(URL: NSURL(string: urlPolling + "&sid=\(sid)&b64=1")!)
|
||||||
|
|
||||||
addHeaders(req)
|
addHeaders(req)
|
||||||
|
|
||||||
doLongPoll(req)
|
doLongPoll(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -600,9 +597,9 @@ extension SocketEngine {
|
|||||||
|
|
||||||
DefaultSocketLogger.Logger.log("Got polling response", type: this.logType)
|
DefaultSocketLogger.Logger.log("Got polling response", type: this.logType)
|
||||||
|
|
||||||
if let str = NSString(data: data!, encoding: NSUTF8StringEncoding) as? String {
|
if let str = String(data: data!, encoding: NSUTF8StringEncoding) {
|
||||||
dispatch_async(this.parseQueue) {[weak this] in
|
dispatch_async(this.parseQueue) {
|
||||||
this?.parsePollingMessage(str)
|
this.parsePollingMessage(str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -666,10 +663,10 @@ extension SocketEngine {
|
|||||||
|
|
||||||
this.waitingForPost = false
|
this.waitingForPost = false
|
||||||
|
|
||||||
dispatch_async(this.emitQueue) {[weak this] in
|
dispatch_async(this.emitQueue) {
|
||||||
if !(this?.fastUpgrade ?? true) {
|
if !this.fastUpgrade {
|
||||||
this?.flushWaitingForPost()
|
this.flushWaitingForPost()
|
||||||
this?.doPoll()
|
this.doPoll()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -712,12 +709,11 @@ extension SocketEngine {
|
|||||||
|
|
||||||
/// Send polling message.
|
/// Send polling message.
|
||||||
/// Only call on emitQueue
|
/// Only call on emitQueue
|
||||||
private func sendPollMessage(var msg: String, withType type: SocketEnginePacketType,
|
private func sendPollMessage(message: String, withType type: SocketEnginePacketType,
|
||||||
datas:[NSData]? = nil) {
|
datas:[NSData]? = nil) {
|
||||||
DefaultSocketLogger.Logger.log("Sending poll: %@ as type: %@", type: logType, args: msg, type.rawValue)
|
DefaultSocketLogger.Logger.log("Sending poll: %@ as type: %@", type: logType, args: message, type.rawValue)
|
||||||
|
let fixedMessage = doubleEncodeUTF8(message)
|
||||||
doubleEncodeUTF8(&msg)
|
let strMsg = "\(type.rawValue)\(fixedMessage)"
|
||||||
let strMsg = "\(type.rawValue)\(msg)"
|
|
||||||
|
|
||||||
postWait.append(strMsg)
|
postWait.append(strMsg)
|
||||||
|
|
||||||
@ -749,7 +745,7 @@ extension SocketEngine {
|
|||||||
ws?.writeString("\(type.rawValue)\(str)")
|
ws?.writeString("\(type.rawValue)\(str)")
|
||||||
|
|
||||||
for data in datas ?? [] {
|
for data in datas ?? [] {
|
||||||
if case let Either.Left(bin) = createBinaryDataForSend(data) {
|
if case let .Left(bin) = createBinaryDataForSend(data) {
|
||||||
ws?.writeData(bin)
|
ws?.writeData(bin)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,16 +25,20 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
func fixDoubleUTF8(inout name: String) {
|
func fixDoubleUTF8(string: String) -> String {
|
||||||
if let utf8 = name.dataUsingEncoding(NSISOLatin1StringEncoding),
|
if let utf8 = string.dataUsingEncoding(NSISOLatin1StringEncoding),
|
||||||
latin1 = NSString(data: utf8, encoding: NSUTF8StringEncoding) {
|
latin1 = NSString(data: utf8, encoding: NSUTF8StringEncoding) {
|
||||||
name = latin1 as String
|
return latin1 as String
|
||||||
|
} else {
|
||||||
|
return string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func doubleEncodeUTF8(inout str: String) {
|
func doubleEncodeUTF8(string: String) -> String {
|
||||||
if let latin1 = str.dataUsingEncoding(NSUTF8StringEncoding),
|
if let latin1 = string.dataUsingEncoding(NSUTF8StringEncoding),
|
||||||
utf8 = NSString(data: latin1, encoding: NSISOLatin1StringEncoding) {
|
utf8 = NSString(data: latin1, encoding: NSISOLatin1StringEncoding) {
|
||||||
str = utf8 as String
|
return utf8 as String
|
||||||
|
} else {
|
||||||
|
return string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -95,7 +95,9 @@ struct SocketPacket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func completeMessage(var message: String, ack: Bool) -> String {
|
private func completeMessage(message: String, ack: Bool) -> String {
|
||||||
|
var restOfMessage = ""
|
||||||
|
|
||||||
if data.count == 0 {
|
if data.count == 0 {
|
||||||
return message + "]"
|
return message + "]"
|
||||||
}
|
}
|
||||||
@ -107,28 +109,25 @@ struct SocketPacket {
|
|||||||
options: NSJSONWritingOptions(rawValue: 0))
|
options: NSJSONWritingOptions(rawValue: 0))
|
||||||
let jsonString = NSString(data: jsonSend, encoding: NSUTF8StringEncoding)
|
let jsonString = NSString(data: jsonSend, encoding: NSUTF8StringEncoding)
|
||||||
|
|
||||||
message += jsonString! as String + ","
|
restOfMessage += jsonString! as String + ","
|
||||||
} catch {
|
} catch {
|
||||||
DefaultSocketLogger.Logger.error("Error creating JSON object in SocketPacket.completeMessage",
|
DefaultSocketLogger.Logger.error("Error creating JSON object in SocketPacket.completeMessage",
|
||||||
type: SocketPacket.logType)
|
type: SocketPacket.logType)
|
||||||
}
|
}
|
||||||
} else if var str = arg as? String {
|
} else if let str = arg as? String {
|
||||||
str = str["\n"] ~= "\\\\n"
|
restOfMessage += "\"\((str["\n"] ~= "\\\\n")["\r"] ~= "\\\\r")\","
|
||||||
str = str["\r"] ~= "\\\\r"
|
|
||||||
|
|
||||||
message += "\"\(str)\","
|
|
||||||
} else if arg is NSNull {
|
} else if arg is NSNull {
|
||||||
message += "null,"
|
restOfMessage += "null,"
|
||||||
} else {
|
} else {
|
||||||
message += "\(arg),"
|
restOfMessage += "\(arg),"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if message != "" {
|
if restOfMessage != "" {
|
||||||
message.removeAtIndex(message.endIndex.predecessor())
|
restOfMessage.removeAtIndex(restOfMessage.endIndex.predecessor())
|
||||||
}
|
}
|
||||||
|
|
||||||
return message + "]"
|
return message + restOfMessage + "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
private func createAck() -> String {
|
private func createAck() -> String {
|
||||||
@ -191,7 +190,7 @@ struct SocketPacket {
|
|||||||
private func createPacketString() -> String {
|
private func createPacketString() -> String {
|
||||||
let str: String
|
let str: String
|
||||||
|
|
||||||
if type == PacketType.Event || type == PacketType.BinaryEvent {
|
if type == .Event || type == .BinaryEvent {
|
||||||
str = createMessageForEvent()
|
str = createMessageForEvent()
|
||||||
} else {
|
} else {
|
||||||
str = createAck()
|
str = createAck()
|
||||||
@ -226,14 +225,8 @@ struct SocketPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return newDict
|
return newDict
|
||||||
} else if let arr = data as? NSArray {
|
} else if let arr = data as? [AnyObject] {
|
||||||
let newArr = NSMutableArray(array: arr)
|
return arr.map({_fillInPlaceholders($0)})
|
||||||
|
|
||||||
for i in 0..<arr.count {
|
|
||||||
newArr[i] = _fillInPlaceholders(arr[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
return newArr
|
|
||||||
} else {
|
} else {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
@ -268,17 +261,13 @@ extension SocketPacket {
|
|||||||
private extension SocketPacket {
|
private extension SocketPacket {
|
||||||
static func shred(data: AnyObject, inout binary: [NSData]) -> AnyObject {
|
static func shred(data: AnyObject, inout binary: [NSData]) -> AnyObject {
|
||||||
if let bin = data as? NSData {
|
if let bin = data as? NSData {
|
||||||
let placeholder = ["_placeholder" :true, "num": binary.count]
|
let placeholder = ["_placeholder": true, "num": binary.count]
|
||||||
|
|
||||||
binary.append(bin)
|
binary.append(bin)
|
||||||
|
|
||||||
return placeholder
|
return placeholder
|
||||||
} else if var arr = data as? [AnyObject] {
|
} else if let arr = data as? [AnyObject] {
|
||||||
for i in 0..<arr.count {
|
return arr.map({shred($0, binary: &binary)})
|
||||||
arr[i] = shred(arr[i], binary: &binary)
|
|
||||||
}
|
|
||||||
|
|
||||||
return arr
|
|
||||||
} else if let dict = data as? NSDictionary {
|
} else if let dict = data as? NSDictionary {
|
||||||
let mutDict = NSMutableDictionary(dictionary: dict)
|
let mutDict = NSMutableDictionary(dictionary: dict)
|
||||||
|
|
||||||
@ -292,13 +281,9 @@ private extension SocketPacket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static func deconstructData(var data: [AnyObject]) -> ([AnyObject], [NSData]) {
|
static func deconstructData(data: [AnyObject]) -> ([AnyObject], [NSData]) {
|
||||||
var binary = [NSData]()
|
var binary = [NSData]()
|
||||||
|
|
||||||
for i in 0..<data.count {
|
return (data.map({shred($0, binary: &binary)}), binary)
|
||||||
data[i] = shred(data[i], binary: &binary)
|
|
||||||
}
|
|
||||||
|
|
||||||
return (data, binary)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user