Skip to content

Commit 3545425

Browse files
committed
Merge pull request #45 from longbai/fast_cancel
fast_cancel
2 parents 9ba5ae7 + 0e47ec1 commit 3545425

File tree

9 files changed

+44
-26
lines changed

9 files changed

+44
-26
lines changed

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@
203203
DFA9B65619E0B53700A15FD1 /* QNFileRecorder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNFileRecorder.h; sourceTree = "<group>"; };
204204
DFA9B65719E0B53700A15FD1 /* QNFileRecorder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNFileRecorder.m; sourceTree = "<group>"; };
205205
DFA9B65B19E0B58900A15FD1 /* QNRecorderDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNRecorderDelegate.h; sourceTree = "<group>"; };
206-
DFA9B65D19E0EDF100A15FD1 /* QNUploadOption+Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "QNUploadOption+Private.h"; sourceTree = "<group>"; };
207206
DFA9B65E19E391A100A15FD1 /* QNTestConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNTestConfig.h; sourceTree = "<group>"; };
208207
DFBC622419DE459800458C4B /* QNHttpTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNHttpTest.m; sourceTree = "<group>"; };
209208
DFF5252F1A6235D100D02BA1 /* QNSessionManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = QNSessionManager.h; sourceTree = "<group>"; };
@@ -360,7 +359,6 @@
360359
DF293CAA19DC0E5300799011 /* QNResumeUpload.m */,
361360
DFA9B64719E0018800A15FD1 /* QNUploadOption.h */,
362361
DFA9B64819E0018800A15FD1 /* QNUploadOption.m */,
363-
DFA9B65D19E0EDF100A15FD1 /* QNUploadOption+Private.h */,
364362
DF609A031A58E39D00AC7297 /* QNFormUpload.h */,
365363
DF609A041A58E39D00AC7297 /* QNFormUpload.m */,
366364
DF482FD61B0DA8A2000DAD98 /* QNConfiguration.h */,

QiniuSDK/Http/QNDns.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
const char *p = inet_ntop(AF_INET, &(remoteAddr->sin_addr), buf, 32);
3535
NSString *ip = [NSString stringWithUTF8String:p];
3636
[ret addObject:ip];
37-
NSLog(@"Resolved %u->%@", i, ip);
37+
// NSLog(@"Resolved %u->%@", i, ip);
3838
}
3939
}
4040
return ret;

QiniuSDK/Http/QNHttpManager.m

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ + (QNResponseInfo *)buildResponseInfo:(AFHTTPRequestOperation *)operation
7474

7575
- (void) sendRequest:(NSMutableURLRequest *)request
7676
withCompleteBlock:(QNCompleteBlock)completeBlock
77-
withProgressBlock:(QNInternalProgressBlock)progressBlock {
77+
withProgressBlock:(QNInternalProgressBlock)progressBlock
78+
withCancelBlock:(QNCancelBlock)cancelBlock{
7879
NSString *u = request.URL.absoluteString;
7980
NSURL *url = request.URL;
8081
__block NSString *ip = nil;
@@ -117,9 +118,21 @@ - (void) sendRequest:(NSMutableURLRequest *)request
117118
}
118119
];
119120

120-
if (progressBlock) {
121+
if (progressBlock || cancelBlock) {
122+
__block AFHTTPRequestOperation *op = nil;
123+
if (cancelBlock) {
124+
op = operation;
125+
}
121126
[operation setUploadProgressBlock: ^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
122-
progressBlock(totalBytesWritten, totalBytesExpectedToWrite);
127+
if (progressBlock) {
128+
progressBlock(totalBytesWritten, totalBytesExpectedToWrite);
129+
}
130+
if (cancelBlock) {
131+
if (cancelBlock()) {
132+
[op cancel];
133+
}
134+
op = nil;
135+
}
123136
}];
124137
}
125138
[request setTimeoutInterval:_timeout];
@@ -148,7 +161,8 @@ - (void)multipartPost:(NSString *)url
148161
error:nil];
149162
[self sendRequest:request
150163
withCompleteBlock:completeBlock
151-
withProgressBlock:progressBlock];
164+
withProgressBlock:progressBlock
165+
withCancelBlock:cancelBlock];
152166
}
153167

154168
- (void) post:(NSString *)url
@@ -171,7 +185,8 @@ - (void) post:(NSString *)url
171185
[request setHTTPBody:data];
172186
[self sendRequest:request
173187
withCompleteBlock:completeBlock
174-
withProgressBlock:progressBlock];
188+
withProgressBlock:progressBlock
189+
withCancelBlock:cancelBlock];
175190
}
176191

177192
@end

QiniuSDK/Http/QNResponseInfo.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ - (NSString *)description {
169169
}
170170

171171
- (BOOL)isCancelled {
172-
return _statusCode == kQNRequestCancelled;
172+
return _statusCode == kQNRequestCancelled || _statusCode == -999;
173173
}
174174

175175
- (BOOL)isOK {

QiniuSDK/Http/QNSessionManager.m

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ @interface QNProgessDelegate : NSObject
2121
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context;
2222
@property (nonatomic, strong) QNInternalProgressBlock progressBlock;
2323
@property (nonatomic, strong) NSProgress *progress;
24+
@property (nonatomic, strong) NSURLSessionUploadTask *task;
25+
@property (nonatomic, strong) QNCancelBlock cancelBlock;
2426
- (instancetype)initWithProgress:(QNInternalProgressBlock)progressBlock;
2527
@end
2628

@@ -51,6 +53,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
5153
void *p = (__bridge void *)(self);
5254
if (p == context) {
5355
_progressBlock(progress.completedUnitCount, progress.totalUnitCount);
56+
if (_cancelBlock && _cancelBlock()) {
57+
[_task cancel];
58+
}
5459
}
5560
else {
5661
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
@@ -126,7 +131,8 @@ + (QNResponseInfo *)buildResponseInfo:(NSHTTPURLResponse *)response
126131

127132
- (void) sendRequest:(NSMutableURLRequest *)request
128133
withCompleteBlock:(QNCompleteBlock)completeBlock
129-
withProgressBlock:(QNInternalProgressBlock)progressBlock {
134+
withProgressBlock:(QNInternalProgressBlock)progressBlock
135+
withCancelBlock:(QNCancelBlock)cancelBlock{
130136
__block NSDate *startTime = [NSDate date];
131137
NSProgress *progress = nil;
132138
__block NSString *host = request.URL.host;
@@ -188,6 +194,8 @@ - (void) sendRequest:(NSMutableURLRequest *)request
188194
if (progress != nil) {
189195
[progress addObserver:delegate forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:(__bridge void *)delegate];
190196
delegate.progress = progress;
197+
delegate.task = uploadTask;
198+
delegate.cancelBlock = cancelBlock;
191199
}
192200

193201
[request setTimeoutInterval:_timeout];
@@ -216,7 +224,8 @@ - (void)multipartPost:(NSString *)url
216224
error:nil];
217225
[self sendRequest:request
218226
withCompleteBlock:completeBlock
219-
withProgressBlock:progressBlock];
227+
withProgressBlock:progressBlock
228+
withCancelBlock:cancelBlock];
220229
}
221230

222231
- (void) post:(NSString *)url
@@ -240,7 +249,8 @@ - (void) post:(NSString *)url
240249
QNAsyncRun( ^{
241250
[self sendRequest:request
242251
withCompleteBlock:completeBlock
243-
withProgressBlock:progressBlock];
252+
withProgressBlock:progressBlock
253+
withCancelBlock:cancelBlock];
244254
});
245255
}
246256

QiniuSDK/Storage/QNFormUpload.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ - (void)put {
8686
_complete(info, _key, resp);
8787
return;
8888
}
89+
if (_option.cancellationSignal()) {
90+
_complete([QNResponseInfo cancel], _key, nil);
91+
return;
92+
}
8993
NSString *nextHost = _config.upHost;
9094
if (info.isConnectionBroken || info.needSwitchServer) {
9195
nextHost = _config.upHostBackup;
@@ -105,7 +109,7 @@ - (void)put {
105109
withMimeType:_option.mimeType
106110
withCompleteBlock:retriedComplete
107111
withProgressBlock:p
108-
withCancelBlock:nil];
112+
withCancelBlock:_option.cancellationSignal];
109113
};
110114

111115
[_httpManager multipartPost:[NSString stringWithFormat:@"http://%@:%u/", _config.upHost, (unsigned int)_config.upPort]
@@ -115,7 +119,7 @@ - (void)put {
115119
withMimeType:_option.mimeType
116120
withCompleteBlock:complete
117121
withProgressBlock:p
118-
withCancelBlock:nil];
122+
withCancelBlock:_option.cancellationSignal];
119123
}
120124

121125
@end

QiniuSDK/Storage/QNResumeUpload.m

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ @interface QNResumeUpload ()
3131
@property (nonatomic, strong) QNUpToken *token;
3232
@property (nonatomic, strong) QNUpCompletionHandler complete;
3333
@property (nonatomic, strong) NSMutableArray *contexts;
34-
@property (nonatomic, readonly, getter = isCancelled) BOOL cancelled;
3534

3635
@property int64_t modifyTime;
3736
@property (nonatomic, strong) id <QNRecorderDelegate> recorder;
@@ -172,7 +171,7 @@ - (UInt32)recoveryFromRecord {
172171
}
173172

174173
- (void)nextTask:(UInt32)offset retriedTimes:(int)retried host:(NSString *)host {
175-
if (self.isCancelled) {
174+
if (self.option.cancellationSignal()) {
176175
self.complete([QNResponseInfo cancel], self.key, nil);
177176
return;
178177
}
@@ -281,10 +280,6 @@ - (void)putChunk:(NSString *)uphost
281280
[self post:url withData:data withCompleteBlock:complete withProgressBlock:progressBlock];
282281
}
283282

284-
- (BOOL)isCancelled {
285-
return self.option.priv_isCancelled;
286-
}
287-
288283
- (void)makeFile:(NSString *)uphost
289284
complete:(QNCompleteBlock)complete {
290285
NSString *mime = [[NSString alloc] initWithFormat:@"/mimeType/%@", [QNUrlSafeBase64 encodeString:self.option.mimeType]];
@@ -311,7 +306,7 @@ - (void) post:(NSString *)url
311306
withData:(NSData *)data
312307
withCompleteBlock:(QNCompleteBlock)completeBlock
313308
withProgressBlock:(QNInternalProgressBlock)progressBlock {
314-
[_httpManager post:url withData:data withParams:nil withHeaders:_headers withCompleteBlock:completeBlock withProgressBlock:progressBlock withCancelBlock:nil];
309+
[_httpManager post:url withData:data withParams:nil withHeaders:_headers withCompleteBlock:completeBlock withProgressBlock:progressBlock withCancelBlock:_option.cancellationSignal];
315310
}
316311

317312
- (void)run {

QiniuSDK/Storage/QNUploadOption.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ - (instancetype)initWithMime:(NSString *)mimeType
5656
return self;
5757
}
5858

59-
- (BOOL)priv_isCancelled {
60-
return _cancellationSignal && _cancellationSignal();
61-
}
62-
6359
+ (instancetype)defaultOptions {
6460
return [[QNUploadOption alloc] initWithMime:nil progressHandler:nil params:nil checkCrc:NO cancellationSignal:nil];
6561
}

QiniuSDKTests/QNFileRecorderTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ - (void)template:(int)size pos:(float)pos {
6868
} option:opt];
6969
AGWW_WAIT_WHILE(key == nil, 60 * 30);
7070
NSLog(@"info %@", info);
71-
XCTAssert(info.statusCode == kQNRequestCancelled, @"Pass");
71+
XCTAssert(info.isCancelled, @"Pass");
7272
XCTAssert([keyUp isEqualToString:key], @"Pass");
7373

7474
// continue

0 commit comments

Comments
 (0)