-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQCommandConnection.m
529 lines (429 loc) · 18.8 KB
/
QCommandConnection.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
File: QCommandConnection.m
Contains: Manages a single TCP connection for sending and receiving commands.
Written by: DTS
Copyright: Copyright (c) 2011 Apple Inc. All Rights Reserved.
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or
redistribution of this Apple software constitutes acceptance of
these terms. If you do not agree with these terms, please do
not use, install, modify or redistribute this Apple software.
In consideration of your agreement to abide by the following
terms, and subject to these terms, Apple grants you a personal,
non-exclusive license, under Apple's copyrights in this
original Apple software (the "Apple Software"), to use,
reproduce, modify and redistribute the Apple Software, with or
without modifications, in source and/or binary forms; provided
that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the
following text and disclaimers in all such redistributions of
the Apple Software. Neither the name, trademarks, service marks
or logos of Apple Inc. may be used to endorse or promote
products derived from the Apple Software without specific prior
written permission from Apple. Except as expressly stated in
this notice, no other rights or licenses, express or implied,
are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or
by other works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis.
APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING
THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY
OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR
OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#import "QCommandConnection.h"
@interface QCommandConnection () <NSStreamDelegate>
// read/write versions of public properties
@property (nonatomic, retain, readonly ) NSMutableSet * runLoopModesMutable;
@property (nonatomic, assign, readwrite) BOOL isOpen;
// private properties
@property (nonatomic, retain, readwrite) NSMutableData * inputBuffer;
@property (nonatomic, retain, readwrite) NSMutableData * outputBuffer;
@property (nonatomic, assign, readwrite) BOOL hasSpaceAvailable;
@end
@implementation QCommandConnection
@synthesize inputStream = inputStream_;
@synthesize outputStream = outputStream_;
@synthesize inputBufferCapacity = inputBufferCapacity_;
@synthesize outputBufferCapacity = outputBufferCapacity_;
@synthesize runLoopModesMutable = runLoopModesMutable_;
@synthesize delegate = delegate_;
@synthesize name = name_;
@synthesize error = error_;
@synthesize isOpen = isOpen_;
@synthesize inputBuffer = inputBuffer_;
@synthesize outputBuffer = outputBuffer_;
@synthesize hasSpaceAvailable = hasSpaceAvailable_;
- (id)initWithInputStream:(NSInputStream *)inputStream outputStream:(NSOutputStream *)outputStream
// See comment in header.
{
assert(inputStream != nil);
assert(outputStream != nil);
self = [super init];
if (self != nil) {
self->inputStream_ = [inputStream retain];
self->outputStream_ = [outputStream retain];
self->runLoopModesMutable_ = [[NSMutableSet alloc] initWithObjects:NSDefaultRunLoopMode, nil];
assert(self->runLoopModesMutable_ != nil);
}
return self;
}
- (void)dealloc
{
[self->inputStream_ release];
[self->outputStream_ release];
[self->runLoopModesMutable_ release];
[self->name_ release];
[self->error_ release];
[self->inputBuffer_ release];
[self->outputBuffer_ release];
[super dealloc];
}
#pragma mark * Run loop modes
- (void)addRunLoopMode:(NSString *)modeToAdd
{
assert(modeToAdd != nil);
if ( ! self.isOpen ) {
[self.runLoopModesMutable addObject:modeToAdd];
}
}
- (void)removeRunLoopMode:(NSString *)modeToRemove
{
assert(modeToRemove != nil);
if ( ! self.isOpen ) {
[self.runLoopModesMutable removeObject:modeToRemove];
}
}
- (NSSet *)runLoopModes
{
return [[self.runLoopModesMutable copy] autorelease];
}
#pragma mark * Utilities
- (void)logWithFormat:(NSString *)format arguments:(va_list)argList
{
assert(format != nil);
if ([self.delegate respondsToSelector:@selector(connection:logWithFormat:arguments:)]) {
[self.delegate connection:self logWithFormat:format arguments:argList];
}
}
- (void)logWithFormat:(NSString *)format, ...
{
va_list argList;
assert(format != nil);
va_start(argList, format);
[self logWithFormat:format arguments:argList];
va_end(argList);
}
+ (NSError *)errorWithCode:(NSInteger)code
// Creates an error in the kQCommandConnectionErrorDomain domain
// with the specified code and (not really) user-visible string.
{
NSMutableDictionary * userInfo;
NSString * description;
assert(code != 0);
userInfo = nil;
switch (code) {
case kQCommandConnectionOutputBufferFullError: {
description = @"output buffer full";
} break;
case kQCommandConnectionInputBufferFullError: {
description = @"input buffer full";
} break;
case kQCommandConnectionOutputCommandTooLongError: {
description = @"output command too long";
} break;
case kQCommandConnectionInputUnexpectedError: {
description = @"did not expect input on this connection";
} break;
case kQCommandConnectionInputCommandTooLongError: {
description = @"input command too long";
} break;
case kQCommandConnectionInputCommandMalformedError: {
description = @"input command malformed";
} break;
default: {
assert(NO);
description = nil;
} break;
}
if (description != nil) {
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
description, NSLocalizedDescriptionKey,
nil
];
assert(userInfo != nil);
}
return [NSError errorWithDomain:kQCommandConnectionErrorDomain code:code userInfo:userInfo];
}
#pragma mark * Open and close
- (void)open
// See comment in header.
{
assert( ! self.isOpen );
[self logWithFormat:@"open"];
// Set up the input and output buffers.
if (self.inputBufferCapacity == 0) {
self.inputBufferCapacity = 16 * 1024;
}
if (self.outputBufferCapacity == 0) {
self.outputBufferCapacity = 16 * 1024;
}
self.inputBuffer = [NSMutableData dataWithCapacity:self.inputBufferCapacity];
assert(self.inputBuffer != nil);
self.outputBuffer = [NSMutableData dataWithCapacity:self.outputBufferCapacity];
assert(self.outputBuffer != nil);
// Start the streams.
[self.inputStream setDelegate:self];
[self.outputStream setDelegate:self];
for (NSString * mode in self.runLoopModesMutable) {
[self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:mode];
[self.outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:mode];
}
[self.inputStream open];
[self.outputStream open];
self.isOpen = YES;
}
- (void)willCloseWithError:(NSError *)error
// See comment in header.
{
// error may be nil (indicates EOF)
if ([self.delegate respondsToSelector:@selector(connection:willCloseWithError:)]) {
[self.delegate connection:self willCloseWithError:error];
}
}
- (void)closeWithError:(NSError *)error notify:(BOOL)notify
// Closes the stream and, if notify is YES, tells the delegate about it.
// This is the core code for the -close and -closeWithError: public
// methods.
{
// error may be nil (indicates EOF)
if (self.isOpen) {
// Latch the error.
if (self.error == nil) {
self.error = error;
}
// Inform the delegate, if required.
if (notify) {
// The following retain and autorelease is necessary to prevent crashes when,
// after we tell the delegate about the close, the delegate releases its reference
// to us, and that's the last reference, so we end up freed, and hence crash on
// returning back up the stack to this code.
[[self retain] autorelease];
[self willCloseWithError:error];
}
// Tear down the streams.
[self.inputStream setDelegate:nil];
[self.outputStream setDelegate:nil];
for (NSString * mode in self.runLoopModesMutable) {
[self.inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:mode];
[self.outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:mode];
}
[self.inputStream close];
[self.outputStream close];
self.isOpen = NO;
}
}
- (void)closeWithError:(NSError *)error
// See comment in header.
{
if (error == nil) {
[self logWithFormat:@"close without error"];
} else {
[self logWithFormat:@"close with error %@", error];
}
[self closeWithError:error notify:YES];
}
- (void)close
// See comment in header.
{
[self logWithFormat:@"close"];
[self closeWithError:nil notify:NO];
}
#pragma mark * Send and receive
- (NSUInteger)parseCommandData:(NSData *)commandData
// See comment in header.
{
NSUInteger result;
assert(commandData != nil);
assert([commandData length] != 0);
result = 0;
if ( [self.delegate respondsToSelector:@selector(connection:parseCommandData:)] ) {
result = [self.delegate connection:self parseCommandData:commandData];
} else {
[self closeWithError:[[self class] errorWithCode:kQCommandConnectionInputUnexpectedError]];
}
return result;
}
- (void)parseCommandsInBuffer
// Calls the delegate to parse all of the commands that are currently sitting
// in the input buffer.
{
NSUInteger inputBufferLength;
NSData * dataToParse;
NSUInteger offset;
NSUInteger bytesParsed;
inputBufferLength = [self.inputBuffer length];
assert(inputBufferLength != 0);
assert(inputBufferLength <= self.inputBufferCapacity);
// We retain the data here because we're going to release it at the end.
// This allows us to, inside the loop, create a sub-range of data and
// have all the retains and releases work out. This means that the
// delegate gets a retained pointer to our input buffer for the first call
// and an immutable copy of a sub-range of our data for subsequent calls.
// But hey, the delegate is supposed to copy it if it wants to keep it.
// And parse all the commands it can on each call.
dataToParse = [self.inputBuffer retain];
assert(dataToParse != nil);
offset = 0;
do {
// Call the delegate to parse the commands in the buffer.
bytesParsed = [self parseCommandData:dataToParse];
assert(bytesParsed <= [dataToParse length]); // you can't parse more data than we gave you
// If the stream is now magically closed, the delegate closed it out from under
// us and we need to leave.
if ( ! self.isOpen ) {
break;
}
// If the delegate couldn't parse any bytes, then leave the loop and wait for the
// remaining bytes in the command to arrive. However, if we already passed a maximum
// size command to the delegate and it still wasn't enough, that means the
// client sent us a command that's too long to be parsed and the connection dies.
if (bytesParsed == 0) {
if ([dataToParse length] == self.outputBufferCapacity) {
[self closeWithError:[[self class] errorWithCode:kQCommandConnectionInputCommandTooLongError]];
}
break;
}
// Consume the bytes that the delegate parsed and continue parsing. If we've consumed
// the entire input buffer, it's time to leave. Otherwise, create a subrange of
// our input buffer and pass it back to the delegate.
offset += bytesParsed;
[self logWithFormat:@"parsed %zu bytes of commands", (size_t) bytesParsed];
if (offset == inputBufferLength) {
break;
}
[dataToParse release];
dataToParse = [[self.inputBuffer subdataWithRange:NSMakeRange(offset, inputBufferLength - offset)] retain];
assert(dataToParse != nil);
} while (YES);
[dataToParse release];
// If we consumed any bytes, remove them from the front of the input buffer.
if (offset != 0) {
[self.inputBuffer replaceBytesInRange:NSMakeRange(0, offset) withBytes:NULL length:0];
}
}
- (void)processInput
// Called in response to a NSStreamEventHasBytesAvailable event to read the data
// from the input stream and process any commands in the data.
{
NSInteger bytesRead;
NSUInteger bufferLength;
bufferLength = [self.inputBuffer length];
if (bufferLength == self.inputBufferCapacity) {
[self closeWithError:[[self class] errorWithCode:kQCommandConnectionInputBufferFullError]];
} else {
// Temporarily increase the size of the buffer up to its capacity
// so as to give us a space to read data.
[self.inputBuffer setLength:self.inputBufferCapacity];
// Read the actual data and respond to the three types of return values.
bytesRead = [self.inputStream read:((uint8_t *) [self.inputBuffer mutableBytes]) + bufferLength maxLength:self.inputBufferCapacity - bufferLength];
if (bytesRead == 0) {
[self logWithFormat:@"read EOF"];
[self closeWithError:nil];
} else if (bytesRead < 0) {
assert([self.inputStream streamError] != nil);
[self logWithFormat:@"read error %@", [self.inputStream streamError]];
[self closeWithError:[self.inputStream streamError]];
} else {
[self logWithFormat:@"read %zu bytes", (size_t) bytesRead];
// Reset the buffer length based on the bytes we actually read and
// then parse any received commands.
[self.inputBuffer setLength:bufferLength + bytesRead];
[self parseCommandsInBuffer];
}
}
}
- (void)processOutput
// Called in response to a NSStreamEventHasSpaceAvailable event (or if such
// an event was deferred) to start sending data to the output stream.
{
NSInteger bytesWritten;
if (self.hasSpaceAvailable) {
if ( [self.outputBuffer length] != 0 ) {
// Write the data and process the two types of return values.
bytesWritten = [self.outputStream write:[self.outputBuffer bytes] maxLength:[self.outputBuffer length]];
if (bytesWritten <= 0) {
assert([self.outputStream streamError] != nil);
[self logWithFormat:@"write error %@", [self.outputStream streamError]];
[self closeWithError:[self.outputStream streamError]];
} else {
[self logWithFormat:@"wrote %zu bytes", (size_t) bytesWritten];
[self.outputBuffer replaceBytesInRange:NSMakeRange(0, bytesWritten) withBytes:NULL length:0];
self.hasSpaceAvailable = NO;
}
}
}
}
- (void)sendCommand:(NSData *)command
// See comment in header.
{
NSUInteger commandLength;
assert(command != nil);
commandLength = [command length];
assert(commandLength != 0); // that's just silly
if (commandLength > self.outputBufferCapacity) {
[self closeWithError:[[self class] errorWithCode:kQCommandConnectionOutputCommandTooLongError]];
} else if ( ([self.outputBuffer length] + commandLength) > self.outputBufferCapacity ) {
[self closeWithError:[[self class] errorWithCode:kQCommandConnectionOutputBufferFullError]];
} else {
[self logWithFormat:@"enqueue %zu byte command", (size_t) commandLength];
[self.outputBuffer appendData:command];
[self processOutput];
}
}
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
// The input and output stream delegate callback method.
{
assert( (aStream == self.inputStream) || (aStream == self.outputStream) );
switch (eventCode) {
case NSStreamEventOpenCompleted: {
[self logWithFormat:@"open %@", (aStream == self.inputStream) ? @"input" : @"output"];
} break;
case NSStreamEventHasBytesAvailable: {
assert(aStream == self.inputStream);
[self logWithFormat:@"has bytes available"];
NSLog(@"Had Bytes available");
[self processInput];
} break;
case NSStreamEventHasSpaceAvailable: {
assert(aStream == self.outputStream);
[self logWithFormat:@"has space available"];
self.hasSpaceAvailable = YES;
[self processOutput];
} break;
case NSStreamEventEndEncountered: {
[self logWithFormat:@"EOF %@", (aStream == self.inputStream) ? @"input" : @"output"];
[self closeWithError:nil];
} break;
default:
assert(NO);
// fall through
case NSStreamEventErrorOccurred: {
[self logWithFormat:@"error %@ %@", (aStream == self.inputStream) ? @"input" : @"output", [aStream streamError]];
[self closeWithError:[aStream streamError]];
} break;
}
}
@end
NSString * kQCommandConnectionErrorDomain = @"com.apple.dts.kQCommandConnectionErrorDomain";