|
| 1 | +// |
| 2 | +// QCloudQuicDataTask.m |
| 3 | +// QCloudCore |
| 4 | +// |
| 5 | +// Created by karisli(李雪) on 2019/3/22. |
| 6 | +// |
| 7 | + |
| 8 | +#import "QCloudQuicDataTask.h" |
| 9 | +#import "QCloudQuicSession.h" |
| 10 | +#import "Tquicnet.h" |
| 11 | + |
| 12 | +@implementation QCloudQuicDataTask |
| 13 | +@synthesize response = _response; |
| 14 | +@synthesize originalRequest = _originalRequest; |
| 15 | + |
| 16 | +- (instancetype)initWithHTTPRequest:(NSMutableURLRequest *)httpRequest |
| 17 | + quicHost:(NSString *)quicHost |
| 18 | + quicIp:(NSString *)quicIp |
| 19 | + body:(id)body |
| 20 | + quicSession:(QCloudQuicSession *)quicSession { |
| 21 | + if (self = [super init]) { |
| 22 | + id bodyData = nil; |
| 23 | + if (body && !(body == [NSNull null])) { |
| 24 | + bodyData = body; |
| 25 | + } else if (httpRequest.HTTPBody) { |
| 26 | + bodyData = httpRequest.HTTPBody; |
| 27 | + } |
| 28 | + TquicRequest *req = [[TquicRequest alloc] initWithURL:httpRequest.URL |
| 29 | + host:quicHost |
| 30 | + httpMethod:httpRequest.HTTPMethod |
| 31 | + ip:quicIp |
| 32 | + body:bodyData |
| 33 | + headerFileds:httpRequest.allHTTPHeaderFields]; |
| 34 | + _manager = [TquicConnection new]; |
| 35 | + __weak typeof(self) weakSelf = self; |
| 36 | + [_manager tquicConnectWithQuicRequest:req |
| 37 | + didReceiveResponse:^(TquicResponse *_Nonnull response) { |
| 38 | + __strong typeof(weakSelf) strngSelf = weakSelf; |
| 39 | + strngSelf.response = [[NSHTTPURLResponse alloc] initWithURL:httpRequest.URL |
| 40 | + statusCode:response.statusCode |
| 41 | + HTTPVersion:response.httpVersion |
| 42 | + headerFields:[response.allHeaderFields copy]]; |
| 43 | + if ([strngSelf.delegate respondsToSelector:@selector(URLSession:dataTask:didReceiveResponse:completionHandler:)]) { |
| 44 | + [strngSelf.delegate URLSession:quicSession dataTask:strngSelf didReceiveResponse:strngSelf.response completionHandler:nil]; |
| 45 | + } |
| 46 | + } |
| 47 | + didReceiveData:^(NSData *_Nonnull data) { |
| 48 | + __strong typeof(weakSelf) strngSelf = weakSelf; |
| 49 | + if ([strngSelf.delegate respondsToSelector:@selector(URLSession:dataTask:didReceiveData:)]) { |
| 50 | + [strngSelf.delegate URLSession:quicSession dataTask:strngSelf didReceiveData:data]; |
| 51 | + } |
| 52 | + } |
| 53 | + didSendBodyData:^(int64_t bytesSent, int64_t totolSentBytes, int64_t totalBytesExpectedToSend) { |
| 54 | + __strong typeof(weakSelf) strngSelf = weakSelf; |
| 55 | + if ([strngSelf.delegate respondsToSelector:@selector(URLSession:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)]) { |
| 56 | + [strngSelf.delegate URLSession:quicSession |
| 57 | + task:strngSelf |
| 58 | + didSendBodyData:bytesSent |
| 59 | + totalBytesSent:totolSentBytes |
| 60 | + totalBytesExpectedToSend:totalBytesExpectedToSend]; |
| 61 | + } |
| 62 | + } |
| 63 | + RequestDidCompleteWithError:^(NSError *_Nonnull error) { |
| 64 | + __strong typeof(weakSelf) strngSelf = weakSelf; |
| 65 | + strngSelf.originalRequest = [[NSURLRequest alloc] initWithURL:httpRequest.URL]; |
| 66 | + if ([strngSelf.delegate respondsToSelector:@selector(URLSession:task:didCompleteWithError:)]) { |
| 67 | + [strngSelf.delegate URLSession:quicSession task:strngSelf didCompleteWithError:error]; |
| 68 | + } |
| 69 | + }]; |
| 70 | + } |
| 71 | + return self; |
| 72 | +} |
| 73 | +- (void)setResponse:(NSHTTPURLResponse *)response { |
| 74 | + _response = response; |
| 75 | +} |
| 76 | +- (NSHTTPURLResponse *)response { |
| 77 | + return _response; |
| 78 | +} |
| 79 | + |
| 80 | +- (NSURLRequest *)currentRequest { |
| 81 | + return _originalRequest; |
| 82 | +} |
| 83 | + |
| 84 | +- (void)setOriginalRequest:(NSURLRequest *)originalRequest { |
| 85 | + _originalRequest = originalRequest; |
| 86 | +} |
| 87 | +- (NSURLRequest *)originalRequest { |
| 88 | + return _originalRequest; |
| 89 | +} |
| 90 | +- (void)cancel { |
| 91 | + [_manager cancleRequest]; |
| 92 | +} |
| 93 | +- (void)start { |
| 94 | + [_manager startRequest]; |
| 95 | +} |
| 96 | +- (void)dealloc { |
| 97 | + // _manager = nil; |
| 98 | + NSLog(@"QCloudQuicDataTask dealloc"); |
| 99 | +} |
| 100 | + |
| 101 | +@end |
0 commit comments