forked from peter-iakovlev/MtProtoKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTNetworkUsageManager.m
259 lines (224 loc) · 8.44 KB
/
MTNetworkUsageManager.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#import "MTNetworkUsageManager.h"
#include <sys/mman.h>
#import <libkern/OSAtomic.h>
#if defined(MtProtoKitDynamicFramework)
# import <MTProtoKitDynamic/MTNetworkUsageCalculationInfo.h>
# import <MTProtoKitDynamic/MTSignal.h>
# import <MTProtoKitDynamic/MTTimer.h>
# import <MTProtoKitDynamic/MTQueue.h>
# import <MTProtoKitDynamic/MTAtomic.h>
#elif defined(MtProtoKitMacFramework)
# import <MTProtoKitMac/MTNetworkUsageCalculationInfo.h>
# import <MTProtoKitMac/MTSignal.h>
# import <MTProtoKitMac/MTTimer.h>
# import <MTProtoKitMac/MTQueue.h>
# import <MTProtoKitMac/MTAtomic.h>
#else
# import <MTProtoKit/MTNetworkUsageCalculationInfo.h>
# import <MTProtoKit/MTSignal.h>
# import <MTProtoKit/MTTimer.h>
# import <MTProtoKit/MTQueue.h>
# import <MTProtoKit/MTAtomic.h>
#endif
static int offsetForInterface(MTNetworkUsageCalculationInfo *info, MTNetworkUsageManagerInterface interface, bool incoming) {
switch (interface) {
case MTNetworkUsageManagerInterfaceWWAN:
if (incoming) {
return info.incomingWWANKey * 8;
} else {
return info.outgoingWWANKey * 8;
}
case MTNetworkUsageManagerInterfaceOther:
if (incoming) {
return info.incomingOtherKey * 8;
} else {
return info.outgoingOtherKey * 8;
}
}
}
@interface MTNetworkUsageManagerImpl : NSObject {
MTQueue *_queue;
MTNetworkUsageCalculationInfo *_info;
NSMutableDictionary<NSNumber *, NSNumber *> *_pendingIncomingBytes;
NSMutableDictionary<NSNumber *, NSNumber *> *_pendingOutgoingBytes;
MTTimer *_timer;
}
@end
@implementation MTNetworkUsageManagerImpl
- (instancetype)initWithQueue:(MTQueue *)queue info:(MTNetworkUsageCalculationInfo *)info {
self = [super init];
if (self != nil) {
_queue = queue;
_info = info;
_pendingIncomingBytes = [[NSMutableDictionary alloc] init];
_pendingOutgoingBytes = [[NSMutableDictionary alloc] init];
}
return self;
}
- (void)dealloc {
[self sync];
}
- (void)scheduleSync {
if (_timer == nil) {
__weak MTNetworkUsageManagerImpl *weakSelf = self;
_timer = [[MTTimer alloc] initWithTimeout:1.0 repeat:false completion:^{
__strong MTNetworkUsageManagerImpl *strongSelf = weakSelf;
[strongSelf sync];
} queue:_queue.nativeQueue];
[_timer start];
}
}
- (void)sync {
[_timer invalidate];
_timer = nil;
int32_t fd = open([_info.filePath UTF8String], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd >= 0) {
[_pendingIncomingBytes enumerateKeysAndObjectsUsingBlock:^(NSNumber * nInterface, NSNumber *nValue, __unused BOOL *stop) {
off_t offset = offsetForInterface(_info, (MTNetworkUsageManagerInterface)[nInterface intValue], true);
lseek(fd, offset, SEEK_SET);
int64_t currentValue = 0;
read(fd, ¤tValue, 8);
currentValue += (int64_t)[nValue intValue];
lseek(fd, offset, SEEK_SET);
write(fd, ¤tValue, 8);
}];
[_pendingOutgoingBytes enumerateKeysAndObjectsUsingBlock:^(NSNumber * nInterface, NSNumber *nValue, __unused BOOL *stop) {
off_t offset = offsetForInterface(_info, (MTNetworkUsageManagerInterface)[nInterface intValue], false);
lseek(fd, offset, SEEK_SET);
int64_t currentValue = 0;
read(fd, ¤tValue, 8);
currentValue += (int64_t)[nValue intValue];
lseek(fd, offset, SEEK_SET);
write(fd, ¤tValue, 8);
}];
close(fd);
}
[_pendingIncomingBytes removeAllObjects];
[_pendingOutgoingBytes removeAllObjects];
}
- (void)addIncomingBytes:(NSUInteger)incomingBytes interface:(MTNetworkUsageManagerInterface)interface {
_pendingIncomingBytes[@(interface)] = @([_pendingIncomingBytes[@(interface)] unsignedIntegerValue] + incomingBytes);
[self scheduleSync];
}
- (void)addOutgoingBytes:(NSUInteger)outgoingBytes interface:(MTNetworkUsageManagerInterface)interface {
_pendingOutgoingBytes[@(interface)] = @([_pendingOutgoingBytes[@(interface)] unsignedIntegerValue] + outgoingBytes);
[self scheduleSync];
}
- (void)resetKeys:(NSArray<NSNumber *> *)keys setKeys:(NSDictionary<NSNumber *, NSNumber *> *)setKeys completion:(void (^)())completion {
[self sync];
int32_t fd = open([_info.filePath UTF8String], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd >= 0) {
for (NSNumber *nKey in keys) {
lseek(fd, [nKey intValue] * 8, SEEK_SET);
int64_t currentValue = 0;
write(fd, ¤tValue, 8);
}
[setKeys enumerateKeysAndObjectsUsingBlock:^(NSNumber *nKey, NSNumber *nValue, __unused BOOL *stop) {
lseek(fd, [nKey intValue] * 8, SEEK_SET);
int64_t currentValue = [nValue longLongValue];
write(fd, ¤tValue, 8);
}];
close(fd);
}
if (completion) {
completion();
}
}
- (NSDictionary *)currentStatsForKeys:(NSArray<NSNumber *> *)keys {
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
[self sync];
int32_t fd = open([_info.filePath UTF8String], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd >= 0) {
for (NSNumber *nKey in keys) {
lseek(fd, [nKey intValue] * 8, SEEK_SET);
int64_t currentValue = 0;
read(fd, ¤tValue, 8);
result[nKey] = @(currentValue);
}
int64_t currentValue = 0;
read(fd, ¤tValue, 8);
close(fd);
}
return result;
}
@end
@interface MTNetworkUsageManagerImplHolder: NSObject
@property (nonatomic) void *impl;
@property (nonatomic) bool deallocated;
@end
@implementation MTNetworkUsageManagerImplHolder
@end
@interface MTNetworkUsageManager () {
MTQueue *_queue;
MTAtomic *_holder;
}
@end
@implementation MTNetworkUsageManager
- (instancetype)initWithInfo:(MTNetworkUsageCalculationInfo *)info {
self = [super init];
if (self != nil) {
_queue = [[MTQueue alloc] init];
_holder = [[MTAtomic alloc] initWithValue:[[MTNetworkUsageManagerImplHolder alloc] init]];
[_queue dispatchOnQueue:^{
[_holder with:^id (MTNetworkUsageManagerImplHolder *holder) {
if (!holder.deallocated) {
holder.impl = (void *)CFBridgingRetain([[MTNetworkUsageManagerImpl alloc] initWithQueue:_queue info:info]);
}
return nil;
}];
}];
}
return self;
}
- (void)dealloc {
MTAtomic *holder = _holder;
[holder with:^id (MTNetworkUsageManagerImplHolder *holder) {
holder.deallocated = true;
return nil;
}];
[_queue dispatchOnQueue:^{
__block void *impl = nil;
[holder with:^id (MTNetworkUsageManagerImplHolder *holder) {
impl = holder.impl;
holder.impl = nil;
return nil;
}];
CFBridgingRelease(impl);
}];
}
- (void)with:(void (^)(MTNetworkUsageManagerImpl *))f {
[_queue dispatchOnQueue:^{
__block __strong MTNetworkUsageManagerImpl *impl = nil;
[_holder with:^id (MTNetworkUsageManagerImplHolder *holder) {
impl = (__bridge MTNetworkUsageManagerImpl *)holder.impl;
return nil;
}];
f(impl);
}];
}
- (void)addIncomingBytes:(NSUInteger)incomingBytes interface:(MTNetworkUsageManagerInterface)interface {
[self with:^(MTNetworkUsageManagerImpl *impl) {
[impl addIncomingBytes:incomingBytes interface:interface];
}];
}
- (void)addOutgoingBytes:(NSUInteger)outgoingBytes interface:(MTNetworkUsageManagerInterface)interface {
[self with:^(MTNetworkUsageManagerImpl *impl) {
[impl addOutgoingBytes:outgoingBytes interface:interface];
}];
}
- (void)resetKeys:(NSArray<NSNumber *> *)keys setKeys:(NSDictionary<NSNumber *, NSNumber *> *)setKeys completion:(void (^)())completion {
[self with:^(MTNetworkUsageManagerImpl *impl) {
[impl resetKeys:keys setKeys:setKeys completion:completion];
}];
}
- (MTSignal *)currentStatsForKeys:(NSArray<NSNumber *> *)keys {
return [[MTSignal alloc] initWithGenerator:^id<MTDisposable>(MTSubscriber *subscriber) {
[self with:^(MTNetworkUsageManagerImpl *impl) {
NSDictionary *result = [impl currentStatsForKeys:keys];
[subscriber putNext:result];
[subscriber putCompletion];
}];
return nil;
}];
}
@end