developer
2023-05-20 e12c7b4c22df631ebdcd16b2f98fbef8f738f92f
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
//
//  NWURLConnectionOperation.h
//  ALINetworkSDK
//
//  Created by Roger.Mu on 5/11/15.
//  Copyright (c) 2015 Taobao.com. All rights reserved.
//
 
typedef NS_ENUM(NSInteger, AFOperationState) {
    TBSDKOperationPausedState      = -1,
    TBSDKOperationReadyState       = 1,
    TBSDKOperationExecutingState   = 2,
    TBSDKOperationFinishedState    = 3,
};
 
typedef NSURLRequest *(^TBSDKURLConnectionOperationRedirectResponseBlock)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse);
 
@interface TBSDKURLConnectionOperation : NSOperation <NSURLConnectionDelegate, NSURLConnectionDataDelegate, NSCopying>
 
// properties
@property (readonly, nonatomic, strong) NSURLRequest *request;
@property (readonly, nonatomic, strong) NSURLResponse *response;
@property (readonly, nonatomic, strong) NSData *responseData;
@property (nonatomic, copy) NSString *responseString;
@property (nonatomic, strong) NSError *error;
@property (nonatomic, assign) NSStringEncoding responseStringEncoding;
@property (readwrite, nonatomic, assign) long long totalBytesRead;
@property (nonatomic, strong) NSURLConnection *connection;
@property (nonatomic, strong) NSRecursiveLock *lock;
@property (nonatomic, strong) NSOutputStream *outputStream;
 
@property (nonatomic, strong) NSSet *runLoopModes;
@property (nonatomic, assign) AFOperationState state;
 
// block methods poperty
@property (readwrite, nonatomic, copy) TBSDKURLConnectionOperationRedirectResponseBlock redirectResponse;
 
// upload and download process
@property (readwrite, nonatomic, copy) void (^uploadProgress)(NSUInteger bytes, long long totalBytes, long long totalBytesExpected);
@property (readwrite, nonatomic, copy) void (^downloadProgress)(NSUInteger bytes, long long totalBytes, long long totalBytesExpected);
 
// methods
-(instancetype) initWithRequest: (NSURLRequest *) urlRequest;
 
// block methods
- (void)setUploadProgressBlock:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))block;
- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block;
- (void)setRedirectResponseBlock:(NSURLRequest * (^)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse))block;
 
//由于NSURLProtocol有限制,didReceiveResponse方法在内部第二次重试的时候不会收到,因此这里手动通知
- (void)connection:(NSURLConnection __unused *)connection didReceiveResponseWhenEncounterError:(NSURLResponse *)response;
 
@end
 
@interface TBSDKRequestDelegate : NSObject
 
@property (nonatomic, weak) TBSDKURLConnectionOperation *weakConnection;
 
@end