-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdispatch_HeartBeat.m
143 lines (117 loc) · 5.23 KB
/
dispatch_HeartBeat.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
//
// dispatch_HeartBeat.m
// NULL
//
// Created by nil on 23/6/2017.
// Copyright © 2017 github.com/foolsparadise All rights reserved.
//
#import "dispatch_HeartBeat.h"
#ifndef __OPTIMIZE__
#define NSLog(FORMAT,...) NSLog(@"%@:%d:%@",[[[NSString stringWithFormat:@"%s",__FILE__] componentsSeparatedByString:@"/"] lastObject], __LINE__,[NSString stringWithFormat:FORMAT, ##__VA_ARGS__])
#else
#define NSLog(...) {}
#endif
//hello -> (OK,next) -> login -> (OK,next) -> heartbeat per 10 seconds
//__HeartBeatSecond : 10 seconds
#define __HeartBeatSecond 10.0 //__HeartBeatSecond 为10秒心跳一次
@implementation dispatch_HeartBeat
static dispatch_HeartBeat *_instance = nil;
+ (instancetype)shareInstance
{
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init] ;
}) ;
return _instance;
}
- (void)openHeartBeat
{
NSLog(@"openHeartBeat");
// hello
dispatch_semaphore_t semaphore_0_hello = dispatch_semaphore_create(0);
dispatch_queue_t queue_0_hello = dispatch_queue_create("semaphore_0_hello", NULL);
dispatch_async(queue_0_hello , ^(void) {
NSString *str = [NSString stringWithFormat:@"https://example.com/tips_heartbeat?cmd=hello"];
NSURL *url = [NSURL URLWithString:str];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
[[session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (error) {
NSLog(@"0 (%@)", error.description); return ;
}
else { // do something like check
NSLog(@"0 (%@)", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
dispatch_semaphore_signal(semaphore_0_hello);
}] resume];
});
dispatch_semaphore_wait(semaphore_0_hello,DISPATCH_TIME_FOREVER);
// login
dispatch_semaphore_t semaphore_1_login = dispatch_semaphore_create(0);
dispatch_queue_t queue_1_login = dispatch_queue_create("semaphore_1_login", NULL);
dispatch_async(queue_1_login , ^(void) {
NSString *str = [NSString stringWithFormat:@"https://example.com/tips_heartbeat?cmd=login"];
NSURL *url = [NSURL URLWithString:str];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
[[session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (error) {
NSLog(@"1 (%@)", error.description); return ;
}
else { // do something like check
NSLog(@"1 (%@)", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
dispatch_semaphore_signal(semaphore_1_login);
}] resume];
});
dispatch_semaphore_wait(semaphore_1_login,DISPATCH_TIME_FOREVER);
// heartbeat
__weak typeof(self)weakSelf = self;
self.heartbeatTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, _timerQueue);
dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
uint64_t interval = (uint64_t)(__HeartBeatSecond * NSEC_PER_SEC); // __HeartBeatSecond 为10秒心跳一次
dispatch_source_set_timer(self.heartbeatTimer, start, interval, 0);
// 设置回调
dispatch_source_set_event_handler(self.heartbeatTimer, ^{
[weakSelf sendHeartBeat];
});
// 启动定时器
dispatch_resume(self.heartbeatTimer);
return;
}
-(void)sendHeartBeat
{
// __HeartBeatSecond 为10秒心跳一次
dispatch_semaphore_t semaphore_2_heartbeat = dispatch_semaphore_create(0);
dispatch_queue_t queue_2_heartbeat = dispatch_queue_create("semaphore_2_heartbeat", NULL);
dispatch_async(queue_2_heartbeat , ^(void) {
NSString *str = [NSString stringWithFormat:@"https://example.com/tips_heartbeat?cmd=heartbeat"];
NSURL *url = [NSURL URLWithString:str];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
[[session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (error) {
NSLog(@"2 (%@)", error.description); return ;
}
else { // do something like check
NSLog(@"2 (%@)", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
dispatch_semaphore_signal(semaphore_2_heartbeat);
}] resume];
});
dispatch_semaphore_wait(semaphore_2_heartbeat,DISPATCH_TIME_FOREVER);
}
- (void)closeHeartBeat
{
NSLog(@"closeHeartBeat");
dispatch_source_cancel(self.heartbeatTimer);
self.heartbeatTimer = nil;
return;
}
@end