use struct for socketpacket
This commit is contained in:
parent
f2e5a1437b
commit
76637d160e
@ -25,7 +25,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
||||||
private typealias Probe = (msg:String, type:PacketType, data:ContiguousArray<NSData>?)
|
private typealias Probe = (msg:String, type:PacketType, data:[NSData]?)
|
||||||
private typealias ProbeWaitQueue = [Probe]
|
private typealias ProbeWaitQueue = [Probe]
|
||||||
|
|
||||||
private let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
|
private let allowedCharacterSet = NSCharacterSet(charactersInString: "!*'();:@&=+$,/?%#[]\" {}").invertedSet
|
||||||
@ -579,7 +579,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Send an engine message (4)
|
/// Send an engine message (4)
|
||||||
public func send(msg:String, withData datas:ContiguousArray<NSData>?) {
|
public func send(msg:String, withData datas:[NSData]?) {
|
||||||
if probing {
|
if probing {
|
||||||
probeWait.append((msg, PacketType.MESSAGE, datas))
|
probeWait.append((msg, PacketType.MESSAGE, datas))
|
||||||
} else {
|
} else {
|
||||||
@ -602,7 +602,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
/// Send polling message.
|
/// Send polling message.
|
||||||
/// Only call on emitQueue
|
/// Only call on emitQueue
|
||||||
private func sendPollMessage(var msg:String, withType type:PacketType,
|
private func sendPollMessage(var msg:String, withType type:PacketType,
|
||||||
datas:ContiguousArray<NSData>? = nil) {
|
datas:[NSData]? = nil) {
|
||||||
SocketLogger.log("Sending poll: %@ as type: %@", client: self, args: msg, type.rawValue)
|
SocketLogger.log("Sending poll: %@ as type: %@", client: self, args: msg, type.rawValue)
|
||||||
|
|
||||||
doubleEncodeUTF8(&msg)
|
doubleEncodeUTF8(&msg)
|
||||||
@ -626,7 +626,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
/// Send message on WebSockets
|
/// Send message on WebSockets
|
||||||
/// Only call on emitQueue
|
/// Only call on emitQueue
|
||||||
private func sendWebSocketMessage(str:String, withType type:PacketType,
|
private func sendWebSocketMessage(str:String, withType type:PacketType,
|
||||||
datas:ContiguousArray<NSData>? = nil) {
|
datas:[NSData]? = nil) {
|
||||||
SocketLogger.log("Sending ws: %@ as type: %@", client: self, args: str, type.rawValue)
|
SocketLogger.log("Sending ws: %@ as type: %@", client: self, args: str, type.rawValue)
|
||||||
|
|
||||||
ws?.writeString("\(type.rawValue)\(str)")
|
ws?.writeString("\(type.rawValue)\(str)")
|
||||||
@ -674,7 +674,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
/**
|
/**
|
||||||
Write a message, independent of transport.
|
Write a message, independent of transport.
|
||||||
*/
|
*/
|
||||||
public func write(msg:String, withType type:PacketType, withData data:ContiguousArray<NSData>?) {
|
public func write(msg:String, withType type:PacketType, withData data:[NSData]?) {
|
||||||
dispatch_async(emitQueue) {[weak self] in
|
dispatch_async(emitQueue) {[weak self] in
|
||||||
if let this = self where this.connected {
|
if let this = self where this.connected {
|
||||||
if this.websocket {
|
if this.websocket {
|
||||||
@ -695,7 +695,7 @@ public final class SocketEngine: NSObject, WebSocketDelegate, SocketLogClient {
|
|||||||
*/
|
*/
|
||||||
public func writeObjc(msg:String, withType type:Int, withData data:NSArray?) {
|
public func writeObjc(msg:String, withType type:Int, withData data:NSArray?) {
|
||||||
if let pType = PacketType(rawValue: type) {
|
if let pType = PacketType(rawValue: type) {
|
||||||
var arr = ContiguousArray<NSData>()
|
var arr = [NSData]()
|
||||||
|
|
||||||
if data != nil {
|
if data != nil {
|
||||||
for d in data! {
|
for d in data! {
|
||||||
|
|||||||
@ -312,11 +312,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let packet = SocketPacket(type: nil, data: args, nsp: nsp, id: ack)
|
let packet = SocketPacket.packetFromEmitWithData(args, id: ack ?? -1, nsp: nsp)
|
||||||
let str:String
|
let str = packet.createMessageForEvent(event)
|
||||||
|
|
||||||
SocketParser.parseForEmit(packet)
|
|
||||||
str = packet.createMessageForEvent(event)
|
|
||||||
|
|
||||||
SocketLogger.log("Emitting: %@", client: self, args: str)
|
SocketLogger.log("Emitting: %@", client: self, args: str)
|
||||||
|
|
||||||
@ -331,11 +328,8 @@ public final class SocketIOClient: NSObject, SocketEngineClient, SocketLogClient
|
|||||||
func emitAck(ack:Int, withData args:[AnyObject]) {
|
func emitAck(ack:Int, withData args:[AnyObject]) {
|
||||||
dispatch_async(emitQueue) {[weak self] in
|
dispatch_async(emitQueue) {[weak self] in
|
||||||
if let this = self where this.connected {
|
if let this = self where this.connected {
|
||||||
let packet = SocketPacket(type: nil, data: args, nsp: this.nsp, id: ack)
|
let packet = SocketPacket.packetFromEmitWithData(args, id: ack ?? -1, nsp: this.nsp)
|
||||||
let str:String
|
let str = packet.createAck()
|
||||||
|
|
||||||
SocketParser.parseForEmit(packet)
|
|
||||||
str = packet.createAck()
|
|
||||||
|
|
||||||
SocketLogger.log("Emitting Ack: %@", client: this, args: str)
|
SocketLogger.log("Emitting Ack: %@", client: this, args: str)
|
||||||
|
|
||||||
|
|||||||
@ -24,27 +24,7 @@
|
|||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
final class SocketPacket: CustomStringConvertible {
|
struct SocketPacket {
|
||||||
var binary = ContiguousArray<NSData>()
|
|
||||||
var currentPlace = 0
|
|
||||||
var data:[AnyObject]?
|
|
||||||
var description:String {
|
|
||||||
var better = "SocketPacket {type: ~~0; data: ~~1; " +
|
|
||||||
"id: ~~2; placeholders: ~~3;}"
|
|
||||||
|
|
||||||
better = better["~~0"] ~= (type != nil ? String(type!.rawValue) : "nil")
|
|
||||||
better = better["~~1"] ~= (data != nil ? "\(data!)" : "nil")
|
|
||||||
better = better["~~2"] ~= (id != nil ? String(id!) : "nil")
|
|
||||||
better = better["~~3"] ~= (placeholders != nil ? String(placeholders!) : "nil")
|
|
||||||
|
|
||||||
return better
|
|
||||||
}
|
|
||||||
var id:Int?
|
|
||||||
var justAck = false
|
|
||||||
var nsp = ""
|
|
||||||
var placeholders:Int?
|
|
||||||
var type:PacketType?
|
|
||||||
|
|
||||||
enum PacketType:Int {
|
enum PacketType:Int {
|
||||||
case CONNECT = 0
|
case CONNECT = 0
|
||||||
case DISCONNECT = 1
|
case DISCONNECT = 1
|
||||||
@ -63,20 +43,69 @@ final class SocketPacket: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
init(type:PacketType?, data:[AnyObject]? = nil, nsp:String = "",
|
var currentPlace = 0
|
||||||
placeholders:Int? = nil, id:Int? = nil) {
|
var binary:[NSData]
|
||||||
self.type = type
|
var data:[AnyObject]
|
||||||
self.data = data
|
var id:Int = -1
|
||||||
self.nsp = nsp
|
var nsp = ""
|
||||||
self.placeholders = placeholders
|
var justAck = false
|
||||||
self.id = id
|
var placeholders:Int
|
||||||
|
var type:PacketType
|
||||||
|
var description:String {
|
||||||
|
var better = "SocketPacket {type: ~~0; data: ~~1; " +
|
||||||
|
"id: ~~2; placeholders: ~~3;}"
|
||||||
|
|
||||||
|
better = better["~~0"] ~= String(type.rawValue)
|
||||||
|
better = better["~~1"] ~= String(data)
|
||||||
|
better = better["~~2"] ~= String(id)
|
||||||
|
better = better["~~3"] ~= String(placeholders)
|
||||||
|
|
||||||
|
return better
|
||||||
|
}
|
||||||
|
|
||||||
|
init(type:SocketPacket.PacketType, data:[AnyObject] = [AnyObject](), id:Int = -1,
|
||||||
|
nsp:String, placeholders:Int = 0, binary:[NSData] = [NSData]()) {
|
||||||
|
self.data = data
|
||||||
|
self.id = id
|
||||||
|
self.nsp = nsp
|
||||||
|
self.type = type
|
||||||
|
self.placeholders = placeholders
|
||||||
|
self.binary = binary
|
||||||
|
}
|
||||||
|
|
||||||
|
static func packetFromEmitWithData(data:[AnyObject], id:Int, nsp:String) -> SocketPacket {
|
||||||
|
func findType(num:Int) -> SocketPacket.PacketType {
|
||||||
|
switch num {
|
||||||
|
case 0:
|
||||||
|
return SocketPacket.PacketType.EVENT
|
||||||
|
default:
|
||||||
|
return SocketPacket.PacketType.BINARY_EVENT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let (parsedData, binary) = deconstructData(data)
|
||||||
|
let packet = SocketPacket(type: findType(binary.count), data: parsedData,
|
||||||
|
id: id, nsp: nsp, placeholders: -1, binary: binary)
|
||||||
|
|
||||||
|
return packet
|
||||||
}
|
}
|
||||||
|
|
||||||
func getEvent() -> String {
|
func getEvent() -> String {
|
||||||
return data?.removeAtIndex(0) as! String
|
return data[0] as! String
|
||||||
}
|
}
|
||||||
|
|
||||||
func addData(data:NSData) -> Bool {
|
func getArgs() -> [AnyObject]? {
|
||||||
|
var arr = data
|
||||||
|
|
||||||
|
if data.count == 0 {
|
||||||
|
return nil
|
||||||
|
} else {
|
||||||
|
arr.removeAtIndex(0)
|
||||||
|
return arr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func addData(data:NSData) -> Bool {
|
||||||
if placeholders == currentPlace {
|
if placeholders == currentPlace {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -92,83 +121,19 @@ final class SocketPacket: CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createMessageForEvent(event:String) -> String {
|
|
||||||
let message:String
|
|
||||||
|
|
||||||
if binary.count == 0 {
|
|
||||||
type = PacketType.EVENT
|
|
||||||
|
|
||||||
if nsp == "/" {
|
|
||||||
if id == nil {
|
|
||||||
message = "2[\"\(event)\""
|
|
||||||
} else {
|
|
||||||
message = "2\(id!)[\"\(event)\""
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if id == nil {
|
|
||||||
message = "2/\(nsp),[\"\(event)\""
|
|
||||||
} else {
|
|
||||||
message = "2/\(nsp),\(id!)[\"\(event)\""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
type = PacketType.BINARY_EVENT
|
|
||||||
|
|
||||||
if nsp == "/" {
|
|
||||||
if id == nil {
|
|
||||||
message = "5\(binary.count)-[\"\(event)\""
|
|
||||||
} else {
|
|
||||||
message = "5\(binary.count)-\(id!)[\"\(event)\""
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if id == nil {
|
|
||||||
message = "5\(binary.count)-/\(nsp),[\"\(event)\""
|
|
||||||
} else {
|
|
||||||
message = "5\(binary.count)-/\(nsp),\(id!)[\"\(event)\""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return completeMessage(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
func createAck() -> String {
|
|
||||||
var msg:String
|
|
||||||
|
|
||||||
if binary.count == 0 {
|
|
||||||
type = PacketType.ACK
|
|
||||||
|
|
||||||
if nsp == "/" {
|
|
||||||
msg = "3\(id!)["
|
|
||||||
} else {
|
|
||||||
msg = "3/\(nsp),\(id!)["
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
type = PacketType.BINARY_ACK
|
|
||||||
|
|
||||||
if nsp == "/" {
|
|
||||||
msg = "6\(binary.count)-\(id!)["
|
|
||||||
} else {
|
|
||||||
msg = "6\(binary.count)-/\(nsp),\(id!)["
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return completeMessage(msg, ack: true)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func completeMessage(var message:String, ack:Bool = false) -> String {
|
private func completeMessage(var message:String, ack:Bool = false) -> String {
|
||||||
if data == nil || data!.count == 0 {
|
if data.count == 0 {
|
||||||
return message + "]"
|
return message + "]"
|
||||||
} else if !ack {
|
} else if !ack {
|
||||||
message += ","
|
message += ","
|
||||||
}
|
}
|
||||||
|
|
||||||
for arg in data! {
|
for arg in data {
|
||||||
if arg is NSDictionary || arg is [AnyObject] {
|
if arg is NSDictionary || arg is [AnyObject] {
|
||||||
let jsonSend: NSData?
|
let jsonSend: NSData?
|
||||||
do {
|
do {
|
||||||
jsonSend = try NSJSONSerialization.dataWithJSONObject(arg,
|
jsonSend = try NSJSONSerialization.dataWithJSONObject(arg,
|
||||||
options: NSJSONWritingOptions(rawValue: 0))
|
options: NSJSONWritingOptions(rawValue: 0))
|
||||||
} catch {
|
} catch {
|
||||||
jsonSend = nil
|
jsonSend = nil
|
||||||
}
|
}
|
||||||
@ -194,21 +159,78 @@ final class SocketPacket: CustomStringConvertible {
|
|||||||
return message + "]"
|
return message + "]"
|
||||||
}
|
}
|
||||||
|
|
||||||
func fillInPlaceholders() {
|
func createAck() -> String {
|
||||||
let newArr = NSMutableArray(array: data!)
|
var msg:String
|
||||||
|
|
||||||
for i in 0..<data!.count {
|
if binary.count == 0 {
|
||||||
if let str = data?[i] as? String, num = str["~~(\\d)"].groups() {
|
if nsp == "/" {
|
||||||
|
msg = "3\(id)["
|
||||||
|
} else {
|
||||||
|
msg = "3/\(nsp),\(id)["
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if nsp == "/" {
|
||||||
|
msg = "6\(binary.count)-\(id)["
|
||||||
|
} else {
|
||||||
|
msg = "6\(binary.count)-/\(nsp),\(id)["
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return completeMessage(msg, ack: true)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func createMessageForEvent(event:String) -> String {
|
||||||
|
let message:String
|
||||||
|
|
||||||
|
if binary.count == 0 {
|
||||||
|
if nsp == "/" {
|
||||||
|
if id == -1 {
|
||||||
|
message = "2[\"\(event)\""
|
||||||
|
} else {
|
||||||
|
message = "2\(id)[\"\(event)\""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if id == -1 {
|
||||||
|
message = "2/\(nsp),[\"\(event)\""
|
||||||
|
} else {
|
||||||
|
message = "2/\(nsp),\(id)[\"\(event)\""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if nsp == "/" {
|
||||||
|
if id == -1 {
|
||||||
|
message = "5\(binary.count)-[\"\(event)\""
|
||||||
|
} else {
|
||||||
|
message = "5\(binary.count)-\(id)[\"\(event)\""
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if id == -1 {
|
||||||
|
message = "5\(binary.count)-/\(nsp),[\"\(event)\""
|
||||||
|
} else {
|
||||||
|
message = "5\(binary.count)-/\(nsp),\(id)[\"\(event)\""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return completeMessage(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
mutating func fillInPlaceholders() {
|
||||||
|
let newArr = NSMutableArray(array: data)
|
||||||
|
|
||||||
|
for i in 0..<data.count {
|
||||||
|
if let str = data[i] as? String, num = str["~~(\\d)"].groups() {
|
||||||
newArr[i] = binary[Int(num[1])!]
|
newArr[i] = binary[Int(num[1])!]
|
||||||
} else if data?[i] is NSDictionary || data?[i] is NSArray {
|
} else if data[i] is NSDictionary || data[i] is NSArray {
|
||||||
newArr[i] = _fillInPlaceholders(data![i])
|
newArr[i] = _fillInPlaceholders(data[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
data = newArr as [AnyObject]
|
data = newArr as [AnyObject]
|
||||||
}
|
}
|
||||||
|
|
||||||
private func _fillInPlaceholders(data:AnyObject) -> AnyObject {
|
private mutating func _fillInPlaceholders(data:AnyObject) -> AnyObject {
|
||||||
if let str = data as? String {
|
if let str = data as? String {
|
||||||
if let num = str["~~(\\d)"].groups() {
|
if let num = str["~~(\\d)"].groups() {
|
||||||
return binary[Int(num[1])!]
|
return binary[Int(num[1])!]
|
||||||
@ -235,4 +257,261 @@ final class SocketPacket: CustomStringConvertible {
|
|||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static func shred(data:AnyObject, inout binary:[NSData]) -> AnyObject {
|
||||||
|
if let bin = data as? NSData {
|
||||||
|
let placeholder = ["_placeholder" :true, "num": binary.count]
|
||||||
|
|
||||||
|
binary.append(bin)
|
||||||
|
|
||||||
|
return placeholder
|
||||||
|
} else if let arr = data as? NSArray {
|
||||||
|
let newArr = NSMutableArray(array: arr)
|
||||||
|
|
||||||
|
for i in 0..<arr.count {
|
||||||
|
newArr[i] = shred(arr[i], binary: &binary)
|
||||||
|
}
|
||||||
|
|
||||||
|
return newArr
|
||||||
|
} else if let dict = data as? NSDictionary {
|
||||||
|
let newDict = NSMutableDictionary(dictionary: dict)
|
||||||
|
|
||||||
|
for (key, value) in newDict {
|
||||||
|
newDict[key as! NSCopying] = shred(value, binary: &binary)
|
||||||
|
}
|
||||||
|
|
||||||
|
return newDict
|
||||||
|
} else {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static func deconstructData(var data:[AnyObject]) -> ([AnyObject], [NSData]) {
|
||||||
|
var binary = [NSData]()
|
||||||
|
|
||||||
|
for i in 0..<data.count {
|
||||||
|
if data[i] is NSArray || data[i] is NSDictionary {
|
||||||
|
data[i] = shred(data[i], binary: &binary)
|
||||||
|
} else if let bin = data[i] as? NSData {
|
||||||
|
data[i] = ["_placeholder" :true, "num": binary.count]
|
||||||
|
binary.append(bin)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (data, binary)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//final class SocketPacket: CustomStringConvertible {
|
||||||
|
// var binary = ContiguousArray<NSData>()
|
||||||
|
// var currentPlace = 0
|
||||||
|
// var data:[AnyObject]?
|
||||||
|
// var description:String {
|
||||||
|
// var better = "SocketPacket {type: ~~0; data: ~~1; " +
|
||||||
|
// "id: ~~2; placeholders: ~~3;}"
|
||||||
|
//
|
||||||
|
// better = better["~~0"] ~= (type != nil ? String(type!.rawValue) : "nil")
|
||||||
|
// better = better["~~1"] ~= (data != nil ? "\(data!)" : "nil")
|
||||||
|
// better = better["~~2"] ~= (id != nil ? String(id!) : "nil")
|
||||||
|
// better = better["~~3"] ~= (placeholders != nil ? String(placeholders!) : "nil")
|
||||||
|
//
|
||||||
|
// return better
|
||||||
|
// }
|
||||||
|
// var id:Int?
|
||||||
|
// var justAck = false
|
||||||
|
// var nsp = ""
|
||||||
|
// var placeholders:Int?
|
||||||
|
// var type:PacketType?
|
||||||
|
//
|
||||||
|
// enum PacketType:Int {
|
||||||
|
// case CONNECT = 0
|
||||||
|
// case DISCONNECT = 1
|
||||||
|
// case EVENT = 2
|
||||||
|
// case ACK = 3
|
||||||
|
// case ERROR = 4
|
||||||
|
// case BINARY_EVENT = 5
|
||||||
|
// case BINARY_ACK = 6
|
||||||
|
//
|
||||||
|
// init?(str:String) {
|
||||||
|
// if let int = Int(str), raw = PacketType(rawValue: int) {
|
||||||
|
// self = raw
|
||||||
|
// } else {
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// init(type:PacketType?, data:[AnyObject]? = nil, nsp:String = "",
|
||||||
|
// placeholders:Int? = nil, id:Int? = nil) {
|
||||||
|
// self.type = type
|
||||||
|
// self.data = data
|
||||||
|
// self.nsp = nsp
|
||||||
|
// self.placeholders = placeholders
|
||||||
|
// self.id = id
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func getEvent() -> String {
|
||||||
|
// return data?.removeAtIndex(0) as! String
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func addData(data:NSData) -> Bool {
|
||||||
|
// if placeholders == currentPlace {
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// binary.append(data)
|
||||||
|
// currentPlace++
|
||||||
|
//
|
||||||
|
// if placeholders == currentPlace {
|
||||||
|
// currentPlace = 0
|
||||||
|
// return true
|
||||||
|
// } else {
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func createMessageForEvent(event:String) -> String {
|
||||||
|
// let message:String
|
||||||
|
//
|
||||||
|
// if binary.count == 0 {
|
||||||
|
// type = PacketType.EVENT
|
||||||
|
//
|
||||||
|
// if nsp == "/" {
|
||||||
|
// if id == nil {
|
||||||
|
// message = "2[\"\(event)\""
|
||||||
|
// } else {
|
||||||
|
// message = "2\(id!)[\"\(event)\""
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// if id == nil {
|
||||||
|
// message = "2/\(nsp),[\"\(event)\""
|
||||||
|
// } else {
|
||||||
|
// message = "2/\(nsp),\(id!)[\"\(event)\""
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// type = PacketType.BINARY_EVENT
|
||||||
|
//
|
||||||
|
// if nsp == "/" {
|
||||||
|
// if id == nil {
|
||||||
|
// message = "5\(binary.count)-[\"\(event)\""
|
||||||
|
// } else {
|
||||||
|
// message = "5\(binary.count)-\(id!)[\"\(event)\""
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// if id == nil {
|
||||||
|
// message = "5\(binary.count)-/\(nsp),[\"\(event)\""
|
||||||
|
// } else {
|
||||||
|
// message = "5\(binary.count)-/\(nsp),\(id!)[\"\(event)\""
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return completeMessage(message)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func createAck() -> String {
|
||||||
|
// var msg:String
|
||||||
|
//
|
||||||
|
// if binary.count == 0 {
|
||||||
|
// type = PacketType.ACK
|
||||||
|
//
|
||||||
|
// if nsp == "/" {
|
||||||
|
// msg = "3\(id!)["
|
||||||
|
// } else {
|
||||||
|
// msg = "3/\(nsp),\(id!)["
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// type = PacketType.BINARY_ACK
|
||||||
|
//
|
||||||
|
// if nsp == "/" {
|
||||||
|
// msg = "6\(binary.count)-\(id!)["
|
||||||
|
// } else {
|
||||||
|
// msg = "6\(binary.count)-/\(nsp),\(id!)["
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return completeMessage(msg, ack: true)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// private func completeMessage(var message:String, ack:Bool = false) -> String {
|
||||||
|
// if data == nil || data!.count == 0 {
|
||||||
|
// return message + "]"
|
||||||
|
// } else if !ack {
|
||||||
|
// message += ","
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// for arg in data! {
|
||||||
|
// if arg is NSDictionary || arg is [AnyObject] {
|
||||||
|
// let jsonSend: NSData?
|
||||||
|
// do {
|
||||||
|
// jsonSend = try NSJSONSerialization.dataWithJSONObject(arg,
|
||||||
|
// options: NSJSONWritingOptions(rawValue: 0))
|
||||||
|
// } catch {
|
||||||
|
// jsonSend = nil
|
||||||
|
// }
|
||||||
|
// let jsonString = NSString(data: jsonSend!, encoding: NSUTF8StringEncoding)
|
||||||
|
//
|
||||||
|
// message += jsonString! as String + ","
|
||||||
|
// } else if var str = arg as? String {
|
||||||
|
// str = str["\n"] ~= "\\\\n"
|
||||||
|
// str = str["\r"] ~= "\\\\r"
|
||||||
|
//
|
||||||
|
// message += "\"\(str)\","
|
||||||
|
// } else if arg is NSNull {
|
||||||
|
// message += "null,"
|
||||||
|
// } else {
|
||||||
|
// message += "\(arg),"
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if message != "" {
|
||||||
|
// message.removeAtIndex(message.endIndex.predecessor())
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return message + "]"
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// func fillInPlaceholders() {
|
||||||
|
// let newArr = NSMutableArray(array: data!)
|
||||||
|
//
|
||||||
|
// for i in 0..<data!.count {
|
||||||
|
// if let str = data?[i] as? String, num = str["~~(\\d)"].groups() {
|
||||||
|
// newArr[i] = binary[Int(num[1])!]
|
||||||
|
// } else if data?[i] is NSDictionary || data?[i] is NSArray {
|
||||||
|
// newArr[i] = _fillInPlaceholders(data![i])
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// data = newArr as [AnyObject]
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private func _fillInPlaceholders(data:AnyObject) -> AnyObject {
|
||||||
|
// if let str = data as? String {
|
||||||
|
// if let num = str["~~(\\d)"].groups() {
|
||||||
|
// return binary[Int(num[1])!]
|
||||||
|
// } else {
|
||||||
|
// return str
|
||||||
|
// }
|
||||||
|
// } else if let dict = data as? NSDictionary {
|
||||||
|
// let newDict = NSMutableDictionary(dictionary: dict)
|
||||||
|
//
|
||||||
|
// for (key, value) in dict {
|
||||||
|
// newDict[key as! NSCopying] = _fillInPlaceholders(value)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return newDict
|
||||||
|
// } else if let arr = data as? NSArray {
|
||||||
|
// let newArr = NSMutableArray(array: arr)
|
||||||
|
//
|
||||||
|
// for i in 0..<arr.count {
|
||||||
|
// newArr[i] = _fillInPlaceholders(arr[i])
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return newArr
|
||||||
|
// } else {
|
||||||
|
// return data
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|||||||
@ -23,62 +23,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class SocketParser {
|
class SocketParser {
|
||||||
private static let shredder = SocketParser.PacketShredder()
|
|
||||||
|
|
||||||
// Translation of socket.io-parser#deconstructPacket
|
|
||||||
private final class PacketShredder {
|
|
||||||
var buf = ContiguousArray<NSData>()
|
|
||||||
|
|
||||||
func shred(data:AnyObject) -> AnyObject {
|
|
||||||
if let bin = data as? NSData {
|
|
||||||
let placeholder = ["_placeholder" :true, "num": buf.count]
|
|
||||||
|
|
||||||
buf.append(bin)
|
|
||||||
|
|
||||||
return placeholder
|
|
||||||
} else if let arr = data as? NSArray {
|
|
||||||
let newArr = NSMutableArray(array: arr)
|
|
||||||
|
|
||||||
for i in 0..<arr.count {
|
|
||||||
newArr[i] = shred(arr[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
return newArr
|
|
||||||
} else if let dict = data as? NSDictionary {
|
|
||||||
let newDict = NSMutableDictionary(dictionary: dict)
|
|
||||||
|
|
||||||
for (key, value) in newDict {
|
|
||||||
newDict[key as! NSCopying] = shred(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
return newDict
|
|
||||||
} else {
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func deconstructPacket(packet:SocketPacket) {
|
|
||||||
if packet.data == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var data = packet.data!
|
|
||||||
|
|
||||||
for i in 0..<data.count {
|
|
||||||
if data[i] is NSArray || data[i] is NSDictionary {
|
|
||||||
data[i] = shred(data[i])
|
|
||||||
} else if let bin = data[i] as? NSData {
|
|
||||||
data[i] = ["_placeholder" :true, "num": buf.count]
|
|
||||||
buf.append(bin)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
packet.data = data
|
|
||||||
packet.binary = buf
|
|
||||||
buf.removeAll(keepCapacity: true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static func checkNSP(nsp:String, _ socket:SocketIOClient) -> Bool {
|
private static func checkNSP(nsp:String, _ socket:SocketIOClient) -> Bool {
|
||||||
return nsp == "" && socket.nsp != "/"
|
return nsp == "" && socket.nsp != "/"
|
||||||
}
|
}
|
||||||
@ -88,7 +32,7 @@ class SocketParser {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.handleAck(p.id!, data: p.data)
|
socket.handleAck(p.id, data: p.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
private static func handleBinaryAck(p:SocketPacket, socket:SocketIOClient) {
|
private static func handleBinaryAck(p:SocketPacket, socket:SocketIOClient) {
|
||||||
@ -96,7 +40,6 @@ class SocketParser {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p.justAck = true
|
|
||||||
socket.waitingData.append(p)
|
socket.waitingData.append(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,7 +66,7 @@ class SocketParser {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.handleEvent(p.getEvent(), data: p.data,
|
socket.handleEvent(p.getEvent(), data: p.getArgs(),
|
||||||
isInternalMessage: false, wantsAck: p.id)
|
isInternalMessage: false, wantsAck: p.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,7 +76,7 @@ class SocketParser {
|
|||||||
let type = String(arr[0])
|
let type = String(arr[0])
|
||||||
|
|
||||||
if arr.count == 1 {
|
if arr.count == 1 {
|
||||||
return SocketPacket(type: SocketPacket.PacketType(str: type))
|
return SocketPacket(type: SocketPacket.PacketType(str: type)!, nsp: "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
var id = nil as Int?
|
var id = nil as Int?
|
||||||
@ -172,8 +115,8 @@ class SocketParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if i + 1 >= arr.count {
|
if i + 1 >= arr.count {
|
||||||
return SocketPacket(type: SocketPacket.PacketType(str: type),
|
return SocketPacket(type: SocketPacket.PacketType(str: type)!, id: id ?? -1,
|
||||||
nsp: nsp, placeholders: placeholders, id: id)
|
nsp: nsp, placeholders: placeholders)
|
||||||
}
|
}
|
||||||
|
|
||||||
let next = String(arr[i + 1])
|
let next = String(arr[i + 1])
|
||||||
@ -197,8 +140,8 @@ class SocketParser {
|
|||||||
let noPlaceholders = d["(\\{\"_placeholder\":true,\"num\":(\\d*)\\})"] ~= "\"~~$2\""
|
let noPlaceholders = d["(\\{\"_placeholder\":true,\"num\":(\\d*)\\})"] ~= "\"~~$2\""
|
||||||
let data = SocketParser.parseData(noPlaceholders) as? [AnyObject] ?? [noPlaceholders]
|
let data = SocketParser.parseData(noPlaceholders) as? [AnyObject] ?? [noPlaceholders]
|
||||||
|
|
||||||
return SocketPacket(type: SocketPacket.PacketType(str: type), data: data,
|
return SocketPacket(type: SocketPacket.PacketType(str: type)!, data: data, id: id ?? -1,
|
||||||
nsp: nsp, placeholders: placeholders, id: id)
|
nsp: nsp, placeholders: placeholders)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -225,10 +168,6 @@ class SocketParser {
|
|||||||
return parsed
|
return parsed
|
||||||
}
|
}
|
||||||
|
|
||||||
static func parseForEmit(packet:SocketPacket) {
|
|
||||||
shredder.deconstructPacket(packet)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parses messages recieved
|
// Parses messages recieved
|
||||||
static func parseSocketMessage(stringMessage:String, socket:SocketIOClient) {
|
static func parseSocketMessage(stringMessage:String, socket:SocketIOClient) {
|
||||||
if stringMessage == "" {
|
if stringMessage == "" {
|
||||||
@ -246,25 +185,23 @@ class SocketParser {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketLogger.log("Decoded packet as: %@", client: socket, altType: "SocketParser", args: p)
|
SocketLogger.log("Decoded packet as: %@", client: socket, altType: "SocketParser", args: p.description)
|
||||||
|
|
||||||
switch p.type {
|
switch p.type {
|
||||||
case SocketPacket.PacketType.EVENT?:
|
case SocketPacket.PacketType.EVENT:
|
||||||
handleEvent(p, socket: socket)
|
handleEvent(p, socket: socket)
|
||||||
case SocketPacket.PacketType.ACK?:
|
case SocketPacket.PacketType.ACK:
|
||||||
handleAck(p, socket: socket)
|
handleAck(p, socket: socket)
|
||||||
case SocketPacket.PacketType.BINARY_EVENT?:
|
case SocketPacket.PacketType.BINARY_EVENT:
|
||||||
handleBinaryEvent(p, socket: socket)
|
handleBinaryEvent(p, socket: socket)
|
||||||
case SocketPacket.PacketType.BINARY_ACK?:
|
case SocketPacket.PacketType.BINARY_ACK:
|
||||||
handleBinaryAck(p, socket: socket)
|
handleBinaryAck(p, socket: socket)
|
||||||
case SocketPacket.PacketType.CONNECT?:
|
case SocketPacket.PacketType.CONNECT:
|
||||||
handleConnect(p, socket: socket)
|
handleConnect(p, socket: socket)
|
||||||
case SocketPacket.PacketType.DISCONNECT?:
|
case SocketPacket.PacketType.DISCONNECT:
|
||||||
socket.didDisconnect("Got Disconnect")
|
socket.didDisconnect("Got Disconnect")
|
||||||
case SocketPacket.PacketType.ERROR?:
|
case SocketPacket.PacketType.ERROR:
|
||||||
socket.didError(p.data == nil ? "Error" : p.data!)
|
socket.didError("Error: \(p.data)")
|
||||||
case nil:
|
|
||||||
SocketLogger.err("Got packet with invalid packet type", client: socket)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -280,14 +217,14 @@ class SocketParser {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
let packet = socket.waitingData.removeAtIndex(0)
|
var packet = socket.waitingData.removeAtIndex(0)
|
||||||
packet.fillInPlaceholders()
|
packet.fillInPlaceholders()
|
||||||
|
|
||||||
if !packet.justAck {
|
if !packet.justAck {
|
||||||
socket.handleEvent(packet.getEvent(), data: packet.data,
|
socket.handleEvent(packet.getEvent(), data: packet.getArgs(),
|
||||||
wantsAck: packet.id)
|
wantsAck: packet.id)
|
||||||
} else {
|
} else {
|
||||||
socket.handleAck(packet.id!, data: packet.data)
|
socket.handleAck(packet.id, data: packet.getArgs())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user