From eb5d03c88b21e7dfda3817923aeeb4e0acac5d4e Mon Sep 17 00:00:00 2001 From: Erik Date: Wed, 8 Apr 2015 20:51:44 -0400 Subject: [PATCH] redo types --- SwiftIO/SocketAckMap.swift | 7 ----- SwiftIO/SocketEngine.swift | 26 ++++++++-------- SwiftIO/SocketEventHandler.swift | 3 -- SwiftIO/SocketFixUTF8.swift | 1 + SwiftIO/SocketIOClient.swift | 4 +-- SwiftIO/SocketPacket.swift | 20 +----------- SwiftIO/SocketTypes.swift | 52 ++++++++++++++++++++++++++++++++ 7 files changed, 69 insertions(+), 44 deletions(-) create mode 100644 SwiftIO/SocketTypes.swift diff --git a/SwiftIO/SocketAckMap.swift b/SwiftIO/SocketAckMap.swift index 9b7905b..6e7f932 100644 --- a/SwiftIO/SocketAckMap.swift +++ b/SwiftIO/SocketAckMap.swift @@ -24,13 +24,6 @@ import Foundation -// @objc_block is undocumented, but is used because Swift assumes that all -// Objective-C blocks are copied, but Objective-C assumes that Swift will copy it. -// And the way things are done here, the bridging fails to copy the block in -// SocketAckMap#addAck -public typealias AckCallback = @objc_block (NSArray?) -> Void -public typealias OnAckCallback = (timeout:UInt64, callback:AckCallback) -> Void - struct SocketAckMap { private var acks = [Int: AckCallback]() private var waiting = [Int: Bool]() diff --git a/SwiftIO/SocketEngine.swift b/SwiftIO/SocketEngine.swift index 435b094..6046650 100644 --- a/SwiftIO/SocketEngine.swift +++ b/SwiftIO/SocketEngine.swift @@ -30,20 +30,10 @@ extension String { } } -private typealias Probe = (msg:String, type:PacketType, data:ContiguousArray?) -private typealias ProbeWaitQueue = [Probe] - -public enum PacketType:String { - case OPEN = "0" - case CLOSE = "1" - case PING = "2" - case PONG = "3" - case MESSAGE = "4" - case UPGRADE = "5" - case NOOP = "6" -} - public class SocketEngine: NSObject, WebSocketDelegate { + private typealias Probe = (msg:String, type:PacketType, data:ContiguousArray?) + private typealias ProbeWaitQueue = [Probe] + private let workQueue = NSOperationQueue() private let emitQueue = dispatch_queue_create( "engineEmitQueue".cStringUsingEncoding(NSUTF8StringEncoding), DISPATCH_QUEUE_SERIAL) @@ -83,6 +73,16 @@ public class SocketEngine: NSObject, WebSocketDelegate { return self._websocket } var ws:WebSocket? + + public enum PacketType:String { + case OPEN = "0" + case CLOSE = "1" + case PING = "2" + case PONG = "3" + case MESSAGE = "4" + case UPGRADE = "5" + case NOOP = "6" + } public init(client:SocketEngineClient, forcePolling:Bool, forceWebsockets:Bool, withCookies cookies:[NSHTTPCookie]?) { diff --git a/SwiftIO/SocketEventHandler.swift b/SwiftIO/SocketEventHandler.swift index f68c73d..34c3086 100644 --- a/SwiftIO/SocketEventHandler.swift +++ b/SwiftIO/SocketEventHandler.swift @@ -24,9 +24,6 @@ import Foundation -public typealias NormalCallback = (NSArray?, AckEmitter?) -> Void -public typealias AckEmitter = (AnyObject...) -> Void - private func emitAckCallback(socket:SocketIOClient, num:Int) // Curried (items:AnyObject...) -> Void { diff --git a/SwiftIO/SocketFixUTF8.swift b/SwiftIO/SocketFixUTF8.swift index 0c36b41..5ca3ab2 100644 --- a/SwiftIO/SocketFixUTF8.swift +++ b/SwiftIO/SocketFixUTF8.swift @@ -3,6 +3,7 @@ // Socket.IO-Swift // // Created by Erik Little on 3/16/15. +// // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights diff --git a/SwiftIO/SocketIOClient.swift b/SwiftIO/SocketIOClient.swift index df1b85b..c8bf163 100644 --- a/SwiftIO/SocketIOClient.swift +++ b/SwiftIO/SocketIOClient.swift @@ -306,7 +306,7 @@ public class SocketIOClient: NSObject, SocketEngineClient { } let packet = SocketPacket(type: nil, data: args, nsp: self.nsp, id: ack) - var str:String + let str:String SocketParser.parseForEmit(packet) str = packet.createMessageForEvent(event) @@ -326,7 +326,7 @@ public class SocketIOClient: NSObject, SocketEngineClient { } let packet = SocketPacket(type: nil, data: args, nsp: self!.nsp, id: ack) - var str:String + let str:String SocketParser.parseForEmit(packet) str = packet.createAck() diff --git a/SwiftIO/SocketPacket.swift b/SwiftIO/SocketPacket.swift index 97581c1..9a13b5a 100644 --- a/SwiftIO/SocketPacket.swift +++ b/SwiftIO/SocketPacket.swift @@ -24,24 +24,6 @@ import Foundation -enum SocketPacketType: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 = str.toInt() { - self = SocketPacketType(rawValue: int)! - } else { - self = SocketPacketType(rawValue: 4)! - } - } -} - class SocketPacket { var binary = ContiguousArray() var currentPlace = 0 @@ -90,7 +72,7 @@ class SocketPacket { } func createMessageForEvent(event:String) -> String { - var message:String + let message:String var jsonSendError:NSError? if self.binary.count == 0 { diff --git a/SwiftIO/SocketTypes.swift b/SwiftIO/SocketTypes.swift new file mode 100644 index 0000000..d022a93 --- /dev/null +++ b/SwiftIO/SocketTypes.swift @@ -0,0 +1,52 @@ +// +// SocketTypes.swift +// SocketIO-Swift +// +// Created by Erik Little on 4/8/15. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +import Foundation + +// @objc_block is undocumented, but is used because Swift assumes that all +// Objective-C blocks are copied, but Objective-C assumes that Swift will copy it. +// And the way things are done here, the bridging fails to copy the block in +// SocketAckMap#addAck +public typealias AckCallback = @objc_block (NSArray?) -> Void +public typealias AckEmitter = (AnyObject...) -> Void +public typealias NormalCallback = (NSArray?, AckEmitter?) -> Void +public typealias OnAckCallback = (timeout:UInt64, callback:AckCallback) -> Void + +enum SocketPacketType: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 = str.toInt() { + self = SocketPacketType(rawValue: int)! + } else { + self = SocketPacketType(rawValue: 4)! + } + } +} \ No newline at end of file