-
Notifications
You must be signed in to change notification settings - Fork 2
/
SISocketIOParser.m
239 lines (194 loc) · 7.46 KB
/
SISocketIOParser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// SISocketIOParser.m
// Socket.IO-iOS
//
// Created by masafumi yoshida on 2014/07/01.
// Copyright (c) 2014年 masafumi yoshida. All rights reserved.
//
#import "SISocketIOParser.h"
@implementation SISocketIOParser
- (NSArray*)encodeData:(SISocketIOPacket *)packet{
switch (packet.type) {
case SISocketIOPacketTypeBinaryEvent:
return [self encodeBinaryPacket:packet];
break;
case SISocketIOPacketTypeBinaryAck:
return [self encodeBinaryPacket:packet];
break;
default:
return @[ [self encodeStringData:packet]];
break;
}
}
-(NSArray*)encodeBinaryPacket:(SISocketIOPacket*)packet{
NSDictionary *deconstruct = [self deconstructBinaryPacket:packet];
id pack = [self encodeStringData:[deconstruct objectForKey:@"packet" ]];
NSMutableArray *buffers = [deconstruct objectForKey:@"buffers"];
[buffers insertObject:pack atIndex:0];
return buffers;
}
- (NSDictionary*)deconstructBinaryPacket:(SISocketIOPacket *)packet{
NSMutableArray *buffers = [NSMutableArray array];
packet.data = [self deconstructBinaryData:packet.data buffers:buffers];
packet.attachments = buffers.count;
return @{@"packet":packet,@"buffers":buffers};
}
- (id)deconstructBinaryData:(id)data buffers:(NSMutableArray*)buffers{
if([data isKindOfClass:[NSData class]]){
NSDictionary *placeholder = @{@"_placeholder": @YES, @"num": [NSNumber numberWithInt: buffers.count]};
[buffers addObject:data];
return placeholder;
}
else if([data isKindOfClass:[NSArray class]]){
NSMutableArray *dataArray = [NSMutableArray array];
for(id row in data){
[dataArray addObject:[self deconstructBinaryData:row buffers:buffers]];
}
return dataArray;
}
else if([data isKindOfClass:[NSDictionary class]]){
NSDictionary *dataDictionary = (NSDictionary*)data;
NSMutableDictionary *newDataDictionary = [NSMutableDictionary dictionary];
for(NSString *key in dataDictionary.allKeys){
[ newDataDictionary setValue:[self deconstructBinaryData:[dataDictionary objectForKey:key] buffers:buffers] forKey:key];
}
}
return nil;
}
- (NSString*)encodeStringData:(SISocketIOPacket *)packet{
NSMutableString *encodeString = [NSMutableString string];
[encodeString appendString:[NSString stringWithFormat:@"%d",packet.type]];
if(packet.type == SISocketIOPacketTypeBinaryEvent || packet.type == SISocketIOPacketTypeBinaryAck){
[encodeString appendString:[NSString stringWithFormat:@"%d-",packet.attachments]];
}
BOOL nsp = NO;
if(packet.nsp && packet.nsp.length > 0 && ![packet.nsp isEqualToString:@"/"]){
nsp = YES;
[encodeString appendString:packet.nsp];
}
if(packet.id){
if(nsp){
[encodeString appendString:@","];
nsp = NO;
}
[encodeString appendString:packet.id.stringValue];
}
if(packet.data){
if(nsp){
[encodeString appendString:@","];
}
NSError *error;
NSData *data= [NSJSONSerialization dataWithJSONObject:packet.data options:nil error:&error];
[encodeString appendString:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding ]];
}
return encodeString;
}
-(SISocketIOPacket*)decodeData:(id)data{
int type;
if([data isKindOfClass:[NSString class]]){
type = [NSNumber numberWithChar:[data characterAtIndex:0]].integerValue;
}
else if([data isKindOfClass:[NSData class]]){
NSData *binaryData = (NSData*)data;
[binaryData getBytes:&type length:1];
}
switch (type) {
case SISocketIOPacketTypeBinaryEvent:
return [self decodeBinaryData:data];
break;
case SISocketIOPacketTypeBinaryAck:
return [self decodeBinaryData:data];
break;
default:
return [self decodeStringData:data];
break;
}
}
- (id)reconstructBinaryData:(id)data buffers:(NSMutableArray*)buffers{
if([data isKindOfClass:[NSData class]]){
NSDictionary *placeholder = @{@"_placeholder": @YES, @"num": [NSNumber numberWithInt: buffers.count]};
[buffers addObject:data];
return placeholder;
}
else if([data isKindOfClass:[NSArray class]]){
NSMutableArray *dataArray = [NSMutableArray array];
for(id row in data){
[dataArray addObject:[self deconstructBinaryData:row buffers:buffers]];
}
return dataArray;
}
else if([data isKindOfClass:[NSDictionary class]]){
NSDictionary *dataDictionary = (NSDictionary*)data;
NSMutableDictionary *newDataDictionary = [NSMutableDictionary dictionary];
for(NSString *key in dataDictionary.allKeys){
[ newDataDictionary setValue:[self deconstructBinaryData:[dataDictionary objectForKey:key] buffers:buffers] forKey:key];
}
}
return nil;
}
-(SISocketIOPacket *)decodeBinaryData:(id)data{
if([data isKindOfClass:[NSString class]]){
SISocketIOPacket *packet = [self decodeStringData:data];
}
else if([data isKindOfClass:[NSData class]]){
}
return nil;
}
-(SISocketIOPacket *)decodeStringData:(id)data{
int i = 0;
NSString *string = (NSString*)data;
SISocketIOPacket *packet = [[SISocketIOPacket alloc] init];
packet.type = [string substringWithRange:NSMakeRange(0,1)].integerValue;
if(packet.type == SISocketIOPacketTypeBinaryEvent || packet.type == SISocketIOPacketTypeBinaryAck){
NSMutableString *attachments = [NSMutableString string];
while(YES){
i++;
NSString *c = [string substringWithRange:NSMakeRange(i,1)] ;
if([c isEqualToString:@"-"]){
packet.attachments = attachments.integerValue;
break;
}
[attachments appendString:c];
}
}
if([[string substringWithRange:NSMakeRange(1,1)] isEqualToString:@"/"]){
NSMutableString *nsp = [NSMutableString string];
while(YES){
i++;
NSString *c = [string substringWithRange:NSMakeRange(i,1)] ;
if([c isEqualToString:@","]){
packet.nsp = nsp;
break;
}
[nsp appendString:c];
if((i + 1) == string.length){
packet.nsp = nsp;
return packet;
}
}
}
char next = [string characterAtIndex:i+1] ;
if(isdigit(next)){
NSMutableString *idString = [NSMutableString string];
while(YES){
i++;
char c = [string characterAtIndex:i];
if(!isdigit(c) ){
packet.id = [NSNumber numberWithInteger:idString.integerValue] ;
i--;
break;
}
[idString appendString:[NSString stringWithFormat:@"%c",c]];
if((i + 1) == string.length){
packet.id = [NSNumber numberWithInteger:idString.integerValue];
return packet;
break;
}
}
}
NSString *stringData = [string substringFromIndex:i+1];
NSError *error;
packet.data = [NSJSONSerialization JSONObjectWithData:[stringData dataUsingEncoding:NSUTF8StringEncoding] options:nil error:&error];
return packet;
}
@end