-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathKMSConnection.m
383 lines (312 loc) · 10.4 KB
/
KMSConnection.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//
// Created by Sam Deane on 06/11/2012.
// Copyright 2012 Karelia Software. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "KMSConnection.h"
#import "KMSServer.h"
#import "KMSListener.h"
#import "KMSResponder.h"
#import "KMSTranscriptEntry.h"
#import "KMSCommand.h"
#import "KMSCloseCommand.h"
@interface KMSConnection()
@property (strong, nonatomic) NSMutableArray* commands;
@property (strong, nonatomic) NSInputStream* input;
@property (strong, nonatomic) NSOutputStream* output;
@property (strong, nonatomic) NSMutableData* outputData;
@property (strong, nonatomic) KMSResponder* responder;
@property (strong, nonatomic) KMSServer* server;
@property (assign, nonatomic) int socket;
#if DEBUG
@property (strong, nonatomic) NSString* lastDisconnectReason;
#endif
@end
@implementation KMSConnection
@synthesize input = _input;
@synthesize output = _output;
@synthesize outputData = _outputData;
@synthesize responder = _responder;
@synthesize server = _server;
#pragma mark - Object Lifecycle
+ (KMSConnection*)connectionWithSocket:(int)socket responder:(KMSResponder*)responder server:(KMSServer*)server
{
KMSConnection* connection = [[KMSConnection alloc] initWithSocket:socket responder:responder server:server];
return [connection autorelease];
}
- (id)initWithSocket:(int)socket responder:(KMSResponder*)responder server:(KMSServer*)server
{
if ((self = [super init]) != nil)
{
self.server = server;
self.responder = responder;
self.outputData = [NSMutableData data];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocket(NULL, socket, &readStream, &writeStream);
self.socket = socket;
self.input = [self setupStream:(NSStream*)readStream mode:InputRunMode];
self.output = [self setupStream:(NSStream*)writeStream mode:OutputRunMode];
CFRelease(readStream);
CFRelease(writeStream);
}
return self;
}
- (void)dealloc
{
KMSAssert((_input == nil) && (_output == nil));
[_input release];
[_output release];
[_outputData release];
[_server release];
#if DEBUG
[_lastDisconnectReason release];
#endif
[super dealloc];
}
#pragma mark - Public API
- (void)open
{
[self.input open];
[self.output open];
}
- (void)cancel
{
dispatch_async(self.server.queue, ^{
[self disconnectStreams:@"cancelled"];
});
}
- (void)appendOutput:(NSData*)output
{
// this should only be called from within a performOnConnection call on a command
// so we should already be on our serial queue
KMSAssert(self.server.currentQueueTargetsServerQueue);
[self.outputData appendData:output];
[self processOutput];
}
#pragma mark - Data Processing
- (void)processInput
{
KMSAssert(self.server.currentQueueTargetsServerQueue);
uint8_t buffer[32768];
NSInteger bytesRead = [self.input read:buffer maxLength:sizeof(buffer)];
if (bytesRead == -1)
{
[self disconnectStreams:[NSString stringWithFormat:@"read error %@", [self.input streamError]]];
}
else if (bytesRead == 0)
{
[self disconnectStreams:@"no more data to read"];
}
else
{
NSDictionary* substitutions = [self.server standardSubstitutions];
NSString* request = [[NSString alloc] initWithBytes:buffer length:bytesRead encoding:NSUTF8StringEncoding];
[self.server.transcript addObject:[KMSTranscriptEntry entryWithType:KMSTranscriptInput value:request]];
KMSLog(@"got request '%@'", request);
NSArray* commands = [self.responder responseForRequest:request substitutions:substitutions];
if (!commands)
{
// if nothing matched, close the connection
// to prevent this, add a key of ".*" as the last response in the array
commands = @[[KMSCloseCommand closeCommand]];
}
[self queueCommands:commands];
[request release];
}
}
- (void)close
{
KMSLogDetail(@"closed connection");
[self.server.transcript addObject:[KMSTranscriptEntry entryWithType:KMSTranscriptCommand value:CloseCommandToken]];
[self disconnectStreams:@"closed as part of response"];
}
- (void)queueCommands:(NSArray*)commands
{
KMSAssert(self.server.currentQueueTargetsServerQueue);
if (!self.commands)
{
self.commands = [NSMutableArray array];
}
BOOL wasEmpty = [self.commands count] == 0;
[self.commands addObjectsFromArray:commands];
if (wasEmpty)
{
[self processNextCommand];
}
}
- (void)processNextCommand
{
KMSAssert(self.server.currentQueueTargetsServerQueue);
NSUInteger count = [self.commands count];
if (count)
{
KMSCommand* command = self.commands[0];
[command retain];
[self.commands removeObjectAtIndex:0];
NSTimeInterval delay = [command performOnConnection:self server:self.server];
if (count > 1)
{
dispatch_time_t nextTimeToProcess = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC));
dispatch_after(nextTimeToProcess, self.server.queue, ^(void){
[self processNextCommand];
});
}
[command release];
}
}
- (void)processOutput
{
KMSAssert(self.server.currentQueueTargetsServerQueue);
NSUInteger bytesToWrite = [self.outputData length];
if (bytesToWrite)
{
NSUInteger written = [self.output write:[self.outputData bytes] maxLength:bytesToWrite];
if (written != -1)
{
[self.outputData replaceBytesInRange:NSMakeRange(0, written) withBytes:nil length:0];
[self.server.transcript addObject:[KMSTranscriptEntry entryWithType:KMSTranscriptOutput value:self.outputData]];
KMSLogDetail(@"wrote %ld bytes", (long)written);
}
else
{
[self disconnectStreams:[NSString stringWithFormat:@"write error %@", [self.output streamError]]];
}
}
}
#pragma mark - Streams
- (id)setupStream:(NSStream*)stream mode:(NSString*)mode
{
KMSAssert(stream);
KMSAssert([NSThread isMainThread]);
[stream setProperty:@(NO) forKey:(NSString*)kCFStreamPropertyShouldCloseNativeSocket];
stream.delegate = self;
[stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:mode];
return stream;
}
- (void)disconnectStreams:(NSString*)reason
{
dispatch_async(self.server.queue, ^{
NSInputStream* input = self.input;
NSOutputStream* output = self.output;
if (input || output)
{
KMSLogDetail(@"disconnecting: %@", reason);
#if DEBUG
self.lastDisconnectReason = reason;
#endif
KMSAssert(input && output);
// do final stream cleanup on the main queue
input.delegate = nil;
output.delegate = nil;
dispatch_async(dispatch_get_main_queue(), ^{
[input close];
[output close];
});
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
close(self.socket);
});
// release the streams here, and tell the server that they're gone
// the block above still holds a reference to them until it's done and they've actually been properly closed
[self.server connectionDidClose:self];
KMSLogDetail(@"disconnected: %@", reason);
self.input = nil;
self.output = nil;
[self.server.transcript addObject:[KMSTranscriptEntry entryWithType:KMSTranscriptEvent value:reason]];
}
else
{
KMSLog(@"attempting to disconnect again: %@\nalready closed because:%@", reason, self.lastDisconnectReason);
}
});
}
- (NSString*)nameForStream:(NSStream*)stream
{
NSString* result;
if (stream == self.input)
{
result = @"input";
}
else if (stream == self.output)
{
result = @"output";
}
else
{
result = @"unknown";
}
return result;
}
- (KMSResponder*)responder
{
KMSResponder* result = _responder;
if (!result)
{
result = self.server.responder;
}
return result;
}
- (void)stream:(NSStream*)stream handleEvent:(NSStreamEvent)eventCode
{
NSInputStream* input = self.input;
NSOutputStream* output = self.output;
if ((input != nil) && (output != nil)) // if events come in after we've been shut down, ignore them...
{
KMSAssert((stream == input) || (stream == output));
switch (eventCode)
{
case NSStreamEventOpenCompleted:
{
KMSLogDetail(@"opened %@ stream", [self nameForStream:stream]);
if (stream == input)
{
dispatch_async(self.server.queue, ^{
[self queueCommands:self.responder.initialResponse];
});
}
break;
}
case NSStreamEventHasBytesAvailable:
{
KMSAssert(stream == input); // should never happen for the output stream
dispatch_async(self.server.queue, ^{
[self processInput];
});
break;
}
case NSStreamEventHasSpaceAvailable:
{
KMSAssert(stream == output); // should never happen for the input stream
dispatch_async(self.server.queue, ^{
[self processOutput];
});
break;
}
case NSStreamEventErrorOccurred:
{
KMSLog(@"got error for %@ stream", [self nameForStream:stream]);
dispatch_async(self.server.queue, ^{
[self disconnectStreams:[NSString stringWithFormat:@"%@ error %@", [self nameForStream:stream], [stream streamError]]];
});
break;
}
case NSStreamEventEndEncountered:
{
KMSLogDetail(@"got eof for %@ stream", [self nameForStream:stream]);
break;
}
default:
{
KMSLog(@"unknown event for %@ stream", [self nameForStream:stream]);
KMSAssert(NO);
break;
}
}
}
else
{
NSLog(@"received an event whilst being disposed");
}
}
@end