Re-implementing throttling in stream provider architecture

This commit is contained in:
Mattt Thompson 2013-03-24 01:30:46 -04:00
Родитель 327cc7cea3
Коммит 48d05e215f
1 изменённых файлов: 14 добавлений и 9 удалений

Просмотреть файл

@ -778,7 +778,7 @@ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2;
@end @end
@interface AFMultipartBodyStreamProvider : NSObject @interface AFMultipartBodyStreamProvider : NSObject
@property (nonatomic, assign) NSUInteger numberOfBytesInPacket; @property (nonatomic, assign) NSUInteger bufferLength;
@property (nonatomic, assign) NSTimeInterval delay; @property (nonatomic, assign) NSTimeInterval delay;
@property (nonatomic, readonly) unsigned long long contentLength; @property (nonatomic, readonly) unsigned long long contentLength;
@property (nonatomic, readonly, getter = isEmpty) BOOL empty; @property (nonatomic, readonly, getter = isEmpty) BOOL empty;
@ -919,7 +919,7 @@ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2;
- (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes - (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes
delay:(NSTimeInterval)delay delay:(NSTimeInterval)delay
{ {
self.bodyStream.numberOfBytesInPacket = numberOfBytes; self.bodyStream.bufferLength = numberOfBytes;
self.bodyStream.delay = delay; self.bodyStream.delay = delay;
} }
@ -952,7 +952,7 @@ NSTimeInterval const kAFUploadStream3GSuggestedDelay = 0.2;
@property (nonatomic, strong) NSMutableData *buffer; @property (nonatomic, strong) NSMutableData *buffer;
@end @end
static const NSUInteger AFMultipartBodyStreamProviderBufferSize = 4096; static const NSUInteger AFMultipartBodyStreamProviderDefaultBufferLength = 4096;
@implementation AFMultipartBodyStreamProvider { @implementation AFMultipartBodyStreamProvider {
@private @private
@ -966,7 +966,7 @@ static const NSUInteger AFMultipartBodyStreamProviderBufferSize = 4096;
@synthesize inputStream = _inputStream; @synthesize inputStream = _inputStream;
@synthesize outputStream = _outputStream; @synthesize outputStream = _outputStream;
@synthesize buffer = _buffer; @synthesize buffer = _buffer;
@synthesize numberOfBytesInPacket = _numberOfBytesInPacket; @synthesize bufferLength = _numberOfBytesInPacket;
@synthesize delay = _delay; @synthesize delay = _delay;
- (id)initWithStringEncoding:(NSStringEncoding)encoding { - (id)initWithStringEncoding:(NSStringEncoding)encoding {
@ -977,9 +977,10 @@ static const NSUInteger AFMultipartBodyStreamProviderBufferSize = 4096;
self.stringEncoding = encoding; self.stringEncoding = encoding;
self.HTTPBodyParts = [NSMutableArray array]; self.HTTPBodyParts = [NSMutableArray array];
self.numberOfBytesInPacket = NSIntegerMax; self.bufferLength = NSIntegerMax;
self.buffer = [[NSMutableData alloc] init]; self.buffer = [[NSMutableData alloc] init];
self.bufferLength = AFMultipartBodyStreamProviderDefaultBufferLength;
return self; return self;
} }
@ -1008,7 +1009,7 @@ static const NSUInteger AFMultipartBodyStreamProviderBufferSize = 4096;
if (_inputStream == nil) { if (_inputStream == nil) {
CFReadStreamRef readStream; CFReadStreamRef readStream;
CFWriteStreamRef writeStream; CFWriteStreamRef writeStream;
CFStreamCreateBoundPair(NULL, &readStream, &writeStream, AFMultipartBodyStreamProviderBufferSize); CFStreamCreateBoundPair(NULL, &readStream, &writeStream, self.bufferLength);
_inputStream = CFBridgingRelease(readStream); _inputStream = CFBridgingRelease(readStream);
_outputStream = CFBridgingRelease(writeStream); _outputStream = CFBridgingRelease(writeStream);
@ -1065,7 +1066,7 @@ static const NSUInteger AFMultipartBodyStreamProviderBufferSize = 4096;
return; return;
} }
[_buffer setLength:AFMultipartBodyStreamProviderBufferSize]; [_buffer setLength:self.bufferLength];
NSInteger numberOfBytesRead = [self.currentHTTPBodyPart read:[_buffer mutableBytes] maxLength:[_buffer length]]; NSInteger numberOfBytesRead = [self.currentHTTPBodyPart read:[_buffer mutableBytes] maxLength:[_buffer length]];
if (numberOfBytesRead < 0) { if (numberOfBytesRead < 0) {
@ -1078,6 +1079,10 @@ static const NSUInteger AFMultipartBodyStreamProviderBufferSize = 4096;
if(numberOfBytesRead == 0) { if(numberOfBytesRead == 0) {
self.currentHTTPBodyPart = nil; self.currentHTTPBodyPart = nil;
} }
if (self.delay > 0.0f) {
[NSThread sleepForTimeInterval:self.delay];
}
} }
} }
} }