@@ -190,12 +190,19 @@ struct SocketPacket {
190
190
return str
191
191
}
192
192
193
+ // Called when we have all the binary data for a packet
194
+ // calls _fillInPlaceholders, which replaces placeholders with the
195
+ // corresponding binary
193
196
private mutating func fillInPlaceholders( ) {
194
197
data = data. map ( { _fillInPlaceholders ( $0) } )
195
198
}
196
199
197
- private mutating func _fillInPlaceholders( data: AnyObject ) -> AnyObject {
198
- switch data {
200
+ // Helper method that looks for placeholder strings
201
+ // If object is a collection it will recurse
202
+ // Returns the object if it is not a placeholder string or the corresponding
203
+ // binary data
204
+ private func _fillInPlaceholders( object: AnyObject ) -> AnyObject {
205
+ switch object {
199
206
case let string as String where string [ " ~~( \\ d) " ] . groups ( ) != nil :
200
207
return binary [ Int ( string [ " ~~( \\ d) " ] . groups ( ) ![ 1 ] ) !]
201
208
case let dict as NSDictionary :
@@ -206,8 +213,7 @@ struct SocketPacket {
206
213
case let arr as [ AnyObject ] :
207
214
return arr. map ( { _fillInPlaceholders ( $0) } )
208
215
default :
209
- return data
210
-
216
+ return object
211
217
}
212
218
}
213
219
}
@@ -216,15 +222,15 @@ extension SocketPacket {
216
222
private static func findType( binCount: Int , ack: Bool ) -> PacketType {
217
223
switch binCount {
218
224
case 0 where !ack:
219
- return PacketType . Event
225
+ return . Event
220
226
case 0 where ack:
221
- return PacketType . Ack
227
+ return . Ack
222
228
case _ where !ack:
223
- return PacketType . BinaryEvent
229
+ return . BinaryEvent
224
230
case _ where ack:
225
- return PacketType . BinaryAck
231
+ return . BinaryAck
226
232
default :
227
- return PacketType . Error
233
+ return . Error
228
234
}
229
235
}
230
236
@@ -238,6 +244,7 @@ extension SocketPacket {
238
244
}
239
245
240
246
private extension SocketPacket {
247
+ // Recursive function that looks for NSData in collections
241
248
static func shred( data: AnyObject , inout binary: [ NSData ] ) -> AnyObject {
242
249
let placeholder = [ " _placeholder " : true , " num " : binary. count]
243
250
@@ -257,6 +264,8 @@ private extension SocketPacket {
257
264
}
258
265
}
259
266
267
+ // Removes binary data from emit data
268
+ // Returns a type containing the de-binaryed data and the binary
260
269
static func deconstructData( data: [ AnyObject ] ) -> ( [ AnyObject ] , [ NSData ] ) {
261
270
var binary = [ NSData] ( )
262
271
0 commit comments