forked from mikeash/MAAsyncIO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMAAsyncReader.m
More file actions
178 lines (141 loc) · 4.51 KB
/
MAAsyncReader.m
File metadata and controls
178 lines (141 loc) · 4.51 KB
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
//
// MAAsyncReader.m
// MAAsyncIO
//
// Created by Michael Ash on 12/1/10.
// Copyright 2010 Michael Ash. All rights reserved.
//
#import "MAAsyncReader.h"
#import "MAFDSource.h"
const NSRange MAKeepReading = { NSNotFound, 0 };
@interface MAAsyncReader ()
- (void)_read;
- (void)_checkCondition;
@end
@implementation MAAsyncReader
- (id)initWithFileDescriptor: (int)fd
{
if((self = [self init]))
{
_fdSource = [[MAFDSource alloc] initWithFileDescriptor: fd type: DISPATCH_SOURCE_TYPE_READ];
_fd = fd;
__block MAAsyncReader *weakSelf = self;
[_fdSource setEventCallback: ^{ [weakSelf _read]; }];
_buffer = [[NSMutableData alloc] init];
}
return self;
}
- (void)dealloc
{
[_fdSource invalidate];
[_fdSource release];
[_buffer release];
[_errorHandler release];
[_condition release];
[_readCallback release];
[super dealloc];
}
- (void)setErrorHandler: (void (^)(int err))handlerBlock
{
handlerBlock = [handlerBlock copy];
[_errorHandler release];
_errorHandler = handlerBlock;
}
- (void)setTargetQueue: (dispatch_queue_t)queue
{
[_fdSource setTargetQueue: queue];
}
- (void)readUntilCondition: (NSRange (^)(NSData *buffer))condBlock
callback: (MAReadCallback)callbackBlock
{
NSAssert(!_reading, @"Can't start a MAAsyncReader read while a read is already pending");
_reading = YES;
_condition = [condBlock copy];
_readCallback = [callbackBlock copy];
[self retain]; // make sure we stick around until the read is done
dispatch_async([_fdSource queue], ^{
[_fdSource resume];
[self _checkCondition];
});
}
- (void)readBytes: (NSUInteger)bytes callback: (MAReadCallback)callbackBlock
{
[self readUntilCondition: ^(NSData *buffer) { return [buffer length] >= bytes ? NSMakeRange(bytes, 0) : MAKeepReading; }
callback: callbackBlock];
}
- (void)readUntilData: (NSData *)data callback: (MAReadCallback)callbackBlock
{
[self readUntilCondition: ^(NSData *buffer) {
return [buffer rangeOfData: data options: 0 range: NSMakeRange(0, [buffer length])];
}
callback: callbackBlock];
}
- (void)readUntilCString: (const char *)cstr callback: (MAReadCallback)callbackBlock
{
[self readUntilData: [NSData dataWithBytes: cstr length: strlen(cstr)] callback: callbackBlock];
}
- (void)readUntilEOFCallback: (MAReadCallback)callbackBlock
{
[self readUntilCondition: ^(NSData *buffer) { return MAKeepReading; } callback: callbackBlock];
}
- (void)invalidate
{
[_fdSource invalidate];
}
- (void)_read
{
NSUInteger howmuch = [_fdSource bytesAvailable];
howmuch = MAX(howmuch, 128U); // read no less than 128 bytes
howmuch = MIN(howmuch, 8192U); // read no more than 8kB
NSUInteger oldLength = [_buffer length];
[_buffer setLength: oldLength + howmuch];
ssize_t result = read(_fd, (char *)[_buffer mutableBytes] + oldLength, howmuch);
NSUInteger didRead = MAX(result, 0); // if -1 (got an error), that means we read 0 bytes
[_buffer setLength: oldLength + didRead];
if(result < 0)
{
if(errno != EAGAIN && errno != EINTR)
if(_errorHandler)
_errorHandler(errno);
}
else
{
if(result == 0)
_isEOF = YES;
[self _checkCondition];
}
}
- (void)_fireReadCallback: (NSData *)data prematureEOF: (BOOL)flag
{
// a fancy dance so that the callback can set up a new callback without breaking everything
MAReadCallback localReadCallback = _readCallback;
_readCallback = nil;
[_condition release];
_condition = nil;
localReadCallback(data, flag);
[localReadCallback release];
[self release]; // balance the retain from readUntilCondition:
}
- (void)_checkCondition
{
if(_condition)
{
NSRange r = _condition(_buffer);
if(r.location != NSNotFound)
{
_reading = NO;
[_fdSource suspend];
NSRange chunkRange = NSMakeRange(0, r.location);
NSData *chunk = [_buffer subdataWithRange: chunkRange];
NSRange deleteRange = NSMakeRange(0, NSMaxRange(r));
[_buffer replaceBytesInRange: deleteRange withBytes: NULL length: 0];
[self _fireReadCallback: chunk prematureEOF: NO];
}
else if(_isEOF)
{
[_fdSource suspend];
[self _fireReadCallback: _buffer prematureEOF: YES];
}
}
}
@end