-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMAAsyncReader.h
More file actions
58 lines (42 loc) · 1.58 KB
/
MAAsyncReader.h
File metadata and controls
58 lines (42 loc) · 1.58 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
//
// MAAsyncReader.h
// MAAsyncIO
//
// Created by Michael Ash on 12/1/10.
// Copyright 2010 Michael Ash. All rights reserved.
//
#import <Cocoa/Cocoa.h>
typedef void (^MAReadCallback)(NSData *data, BOOL prematureEOF); // prematureOF = EOF hit before condition met
@class MAFDSource;
@interface MAAsyncReader : NSObject
{
MAFDSource *_fdSource;
int _fd;
BOOL _reading;
BOOL _isEOF;
NSMutableData *_buffer;
void (^_errorHandler)(int);
NSRange (^_condition)(NSData *);
MAReadCallback _readCallback;
}
// initialization
- (id)initWithFileDescriptor: (int)fd; // sets fd nonblocking, uses MAFDRetain/MAFDRelease to manage it
// setup
- (void)setErrorHandler: (void (^)(int err))handlerBlock;
- (void)setTargetQueue: (dispatch_queue_t)queue; // default is normal global queue
// reading
// condition returns range of delimeter
// everything up to range.location is passed to the callback
// everything within the range is deleted from the buffer
// return NSNotFound in range.location to signal "keep reading"
// or use the MAKeepReading constant
- (void)readUntilCondition: (NSRange (^)(NSData *buffer))condBlock
callback: (MAReadCallback)callbackBlock;
- (void)readBytes: (NSUInteger)bytes callback: (MAReadCallback)callbackBlock;
- (void)readUntilData: (NSData *)data callback: (MAReadCallback)callbackBlock;
- (void)readUntilCString: (const char *)cstr callback: (MAReadCallback)callbackBlock;
- (void)readUntilEOFCallback: (MAReadCallback)callbackBlock;
// stopping
- (void)invalidate;
@end
extern const NSRange MAKeepReading;