-
Notifications
You must be signed in to change notification settings - Fork 0
/
HFConnectionOpertation.m
146 lines (117 loc) · 4.29 KB
/
HFConnectionOpertation.m
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
//
// MyDownloader.m
// MultDownloader
//
// Created by LiQiu Yu on 15-9-28.
// Copyright (c) 2015年 LiQiu Yu. All rights reserved.
//
#import "HFConnectionOpertation.h"
#import "HFRequest.h"
#import "HFResponse.h"
@interface HFConnectionOpertation ()
@end
@implementation HFConnectionOpertation
@synthesize request;
@synthesize response;
@synthesize delegate;
- (void)main {
connection = [[NSURLConnection alloc] initWithRequest:request.request delegate:self startImmediately:YES];
NSLog(@"request is %@",request);
isFinished = NO;
response.responseTarget = request.requestTarget;
if (connection) {
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!isFinished);
}
}
- (id)initWithRequest:(HFRequest *)request_ {
if (self = [super init]) {
self.request = request_;
response = [[HFResponse alloc] init];
self.delegate = self.request.delegate;
if (request.persistentType == SAVE_TO_FILE){
fileHandle = [[HFFileHandle alloc] initWithRequest:request];
}else{
fileHandle= nil;
}
startOffset = [fileHandle getStartOffSetWithPath];
[self.request setFileOffSet:startOffset];
}
return self;
}
- (void)dealloc{
#if !__has_feature(objc_arc)
[connection release];
[fileHandle release];
[request release];
[response release];
[super dealloc];
#endif
}
#pragma mark -- NS URL Connetion Delegate Protocal
- (void)connection:(NSURLConnection *)connection_ didReceiveResponse:(NSURLResponse *)response_ {
if ([[((NSHTTPURLResponse *)response_) allHeaderFields] objectForKey:@"Content-Length"]) {
response.expectedSize = [[[((NSHTTPURLResponse *)response_) allHeaderFields] objectForKey:@"Content-Length"] integerValue];
}
response.response = response_;
[fileHandle createFile];
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection_ didReceiveData:(NSData *)data_{
[responseData appendData:data_];
NSLog(@"%@:download complete %f / 100",fileHandle.writingFilePath,([responseData length]+startOffset)/(float)(response.expectedSize+startOffset) *100.0);
response.downloadRate = ([responseData length]+startOffset)/(float)(response.expectedSize+startOffset);
if (request.persistentType == SAVE_TO_FILE) {
[fileHandle writeData:data_];
}
if (request.callBackType == DELEGATE_TYPE) {
if (delegate && [delegate respondsToSelector:@selector(downloadInprogress:)]) {
[delegate performSelectorOnMainThread:@selector(downloadInprogress:) withObject:response waitUntilDone:NO];
}
}
else if(request.callBackType == BLOCK_TYPE) {
if (request.inprogressBlock) {
dispatch_async(dispatch_get_main_queue(),^(void){request.inprogressBlock(response);});
}
}
else{
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection_{
response.responseData = responseData;
isFinished = YES;
if (request.callBackType == DELEGATE_TYPE) {
if (delegate && [delegate respondsToSelector:@selector(downloadFinished:)]) {
[delegate performSelectorOnMainThread:@selector(downloadFinished:) withObject:response waitUntilDone:NO];
}
}
else if(request.callBackType == BLOCK_TYPE) {
if (request.finishBlock) {
dispatch_async(dispatch_get_main_queue(),^(void){request.finishBlock(response);});
}
}
else{
}
//connection = nil;
[fileHandle closeFile];
}
-(void)connection:(NSURLConnection *)connection_ didFailWithError:(NSError *)error{
//TDD wap error here
[response setError:error];
//connection_ = nil;
if (request.callBackType == DELEGATE_TYPE) {
if (delegate && [delegate respondsToSelector:@selector(downloadFinished:)]) {
[delegate performSelectorOnMainThread:@selector(downloadFinished:) withObject:response waitUntilDone:NO];
}
}
else if(request.callBackType == BLOCK_TYPE) {
if (request.finishBlock) {
dispatch_async(dispatch_get_main_queue(),^(void){request.finishBlock(response);});
}
}
else{
}
isFinished = YES;
}
@end