Added support for asynchronous reading in GCDWebServerBodyReader

This commit is contained in:
Pierre-Olivier Latour
2014-10-14 12:40:19 -07:00
parent a933b2126e
commit c4bf7b11e2
4 changed files with 71 additions and 45 deletions
+4 -2
View File
@@ -280,8 +280,8 @@ static inline NSUInteger _ScanHexNumber(const void* bytes, NSUInteger size) {
- (void)_writeBodyWithCompletionBlock:(WriteBodyCompletionBlock)block {
GWS_DCHECK([_response hasBody]);
NSError* error = nil;
NSData* data = [_response performReadData:&error];
[_response performReadDataWithCompletion:^(NSData* data, NSError* error) {
if (data) {
if (data.length) {
if (_response.usesChunkedTransferEncoding) {
@@ -328,6 +328,8 @@ static inline NSUInteger _ScanHexNumber(const void* bytes, NSUInteger size) {
GWS_LOG_ERROR(@"Failed reading response body for socket %i: %@", _socket, error);
block(NO);
}
}];
}
@end
+1 -1
View File
@@ -249,6 +249,6 @@ extern NSString* GCDWebServerStringFromSockAddr(const struct sockaddr* addr, BOO
@property(nonatomic, readonly) BOOL usesChunkedTransferEncoding;
- (void)prepareForReading;
- (BOOL)performOpen:(NSError**)error;
- (NSData*)performReadData:(NSError**)error;
- (void)performReadDataWithCompletion:(GCDWebServerBodyReaderCompletionBlock)block;
- (void)performClose;
@end
+19
View File
@@ -27,6 +27,12 @@
#import <Foundation/Foundation.h>
/**
* The GCDWebServerBodyReaderCompletionBlock is passed by GCDWebServer to the
* GCDWebServerBodyReader object when reading data from it asynchronously.
*/
typedef void (^GCDWebServerBodyReaderCompletionBlock)(NSData* data, NSError* error);
/**
* This protocol is used by the GCDWebServerConnection to communicate with
* the GCDWebServerResponse and read the HTTP body data to send.
@@ -39,6 +45,8 @@
*/
@protocol GCDWebServerBodyReader <NSObject>
@required
/**
* This method is called before any body data is sent.
*
@@ -61,6 +69,17 @@
*/
- (void)close;
@optional
/**
* If this method is implemented, it will be preferred over -readData:.
*
* It must call the passed block when data is available, passing a non-empty
* NSData if there is body data available, or an empty NSData there is no more
* body data, or nil on error and pass an NSError along.
*/
- (void)asyncReadDataWithCompletion:(GCDWebServerBodyReaderCompletionBlock)block;
@end
/**
+8 -3
View File
@@ -244,9 +244,14 @@
return [_reader open:error];
}
- (NSData*)performReadData:(NSError**)error {
GWS_DCHECK(_opened);
return [_reader readData:error];
- (void)performReadDataWithCompletion:(GCDWebServerBodyReaderCompletionBlock)block {
if ([_reader respondsToSelector:@selector(asyncReadDataWithCompletion:)]) {
[_reader asyncReadDataWithCompletion:block];
} else {
NSError* error = nil;
NSData* data = [_reader readData:&error];
block(data, error);
}
}
- (void)performClose {