-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathrotation.cpp
More file actions
291 lines (270 loc) · 7.18 KB
/
rotation.cpp
File metadata and controls
291 lines (270 loc) · 7.18 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "rotation.h"
#include <time.h>
/// <summary>
/// 初始化轮询结构体
/// </summary>
/// <param name="rotation_opt"></param>
/// <param name="strategyID"></param>
/// <param name="rotate_Strategy_time"></param>
/// <param name="failover_Strategy_time"></param>
/// <param name="failover_Strategy_number"></param>
void init_rotation(rotationstruc* rotation_opt, int strategyID, int rotate_Strategy_time, int failover_Strategy_time, int failover_Strategy_number)
{
rotation_opt->strategyID = strategyID;
rotation_opt->rotate_Strategy_time = rotate_Strategy_time;
rotation_opt->failover_Strategy_time = failover_Strategy_time;
rotation_opt->failover_Strategy_number = failover_Strategy_number;
}
/// <summary>
/// 随机模式下ip存储缓冲区
/// </summary>
char* ServerIP_Buff_random=NULL;
/// <summary>
/// 随机模式下ip数量
/// </summary>
int g_ip_number_random=0;
/// <summary>
/// 随机模式下当前ip索引
/// </summary>
int g_current_ip_index_random=0;
/// <summary>
/// 随机模式下ip指针数组
/// </summary>
char* g_ip_array_random[200];
int random_int(int seed)
{
return rand() % (seed + 1);
}
int random_ipid(int ip_id)
{
return ip_id - ip_id % 2;
}
/// <summary>
/// 随机选择ip
/// </summary>
/// <param name="ServerIP"></param>
/// <returns></returns>
char* random_selection(char* ServerIP)
{
int current_ip_id = 0;
int ip_id = 0 ;
if (ServerIP_Buff_random)
{
current_ip_id = g_current_ip_index_random;
}
else
{
ServerIP_Buff_random = (char*)malloc(strlen(ServerIP) + 1);
strncpy(ServerIP_Buff_random, ServerIP, strlen(ServerIP) + 1);
g_ip_number_random = 0;
for (char* ip = strtok(ServerIP_Buff_random, ","); ip; ip = strtok(0, ","))
{
int ip_id = g_ip_number_random++;
g_ip_array_random[ip_id] = ip;
}
current_ip_id = -1;
g_current_ip_index_random = -1;
}
if (current_ip_id < 0 || current_ip_id >= g_ip_number_random)
{
ip_id = random_int(g_ip_number_random - 1);
ip_id = random_ipid(ip_id);
g_current_ip_index_random = ip_id;
}
else
{
ip_id = current_ip_id + 1;
g_current_ip_index_random =- 1;
}
return g_ip_array_random[ip_id];
}
/// <summary>
/// 轮询模式下ip缓冲区
/// </summary>
char* ServerIP_Buff_carousel;
/// <summary>
/// 轮询 错误切换根据错误次数切换的指定错误次数
/// </summary>
int g_failover_Strategy_number;
/// <summary>
/// 轮询 错误切换根据时间切换的间隔时间
/// </summary>
int g_failover_Strategy_time;
/// <summary>
/// 轮询模式的ip数量
/// </summary>
int g_ip_number_carousel;
/// <summary>
/// 轮询模式的ip数组,保存所有的ip指针
/// </summary>
char* g_ip_array_carousel[200];
/// <summary>
/// 轮询模式下当前使用的ip索引,通过此id从数组里取ip指针
/// </summary>
int g_ip_index_carousel;
/// <summary>
///
/// </summary>
__int64 g_time;
/// <summary>
/// 轮询 定时切换 间隔时间
/// </summary>
int g_rotate_Strategy_time;
/// <summary>
/// 根据错误时间进行轮询
/// </summary>
__int64 g_failover_time;
/// <summary>
/// 错误次数
/// </summary>
int g_failover_number;
int dword_10037E80;
char* carousel(rotationstruc* rotation_opt, char* ServerIP, int number)
{
char* ipsrc;
int ip_id;
signed int failover_Strategy_number;
signed int failover_Strategy_time;
char* result;
__time64_t current_time;
int v10;
v10 = 0;
current_time = _time64(0);
if (ServerIP_Buff_carousel)
{
failover_Strategy_number = g_failover_Strategy_number;
failover_Strategy_time = g_failover_Strategy_time;
}
else
{
ServerIP_Buff_carousel = (char*)malloc(strlen(ServerIP) + 1);
strncpy(ServerIP_Buff_carousel, ServerIP, strlen(ServerIP) + 1);
g_ip_number_carousel = 0;
for (ipsrc = strtok(ServerIP_Buff_carousel, ","); ipsrc; ipsrc = strtok(0, ","))
{
ip_id = g_ip_number_carousel++;
g_ip_array_carousel[ip_id] = ipsrc;
}
g_ip_index_carousel = 0;
g_time = _time64(0);
failover_Strategy_number = rotation_opt->failover_Strategy_number;
g_rotate_Strategy_time = rotation_opt->rotate_Strategy_time;
failover_Strategy_time = rotation_opt->failover_Strategy_time;
g_failover_time = 0i64;
g_failover_Strategy_number = failover_Strategy_number;
g_failover_Strategy_time = failover_Strategy_time;
}
if (number)
{
if (failover_Strategy_number > -1 && ++g_failover_number > failover_Strategy_number)
{
v10 = 1;
}
if (failover_Strategy_time > -1)
{
if (g_failover_time)
{
if (current_time > g_failover_time + failover_Strategy_time)
{
v10 = 1;
}
}
else
{
g_failover_time = _time64(0);
}
}
}
else if (!dword_10037E80)
{
g_failover_time = 0i64;
g_failover_number = 0;
}
if (g_rotate_Strategy_time <= -1)
{
goto LABEL_23;
}
if (!v10)
{
if (dword_10037E80)
{
result = g_ip_array_carousel[g_ip_index_carousel + 1];
dword_10037E80 = 0;
return result;
}
if (current_time <= g_time + g_rotate_Strategy_time)
{
result = g_ip_array_carousel[g_ip_index_carousel];
dword_10037E80 = 1;
return result;
}
v10 = 1;
LABEL_23:
if (!v10)
{
if (!dword_10037E80)
{
result = g_ip_array_carousel[g_ip_index_carousel];
dword_10037E80 = 1;
return result;
}
result = g_ip_array_carousel[g_ip_index_carousel + 1];
dword_10037E80 = 0;
return result;
}
}
g_ip_index_carousel += 2;
g_failover_number = 0;
dword_10037E80 = 0;
g_failover_time = 0i64;
if (g_ip_index_carousel >= g_ip_number_carousel)
{
g_ip_index_carousel = 0;
}
g_time = _time64(0);
if (!dword_10037E80)
{
result = g_ip_array_carousel[g_ip_index_carousel];
dword_10037E80 = 1;
return result;
}
result = g_ip_array_carousel[g_ip_index_carousel + 1];
dword_10037E80 = 0;
return result;
}
char* g_ServerIP_Buff;
char* no_rotation(char* ServerIP)
{
char* result;
if (g_ServerIP_Buff)
{
result = strtok(0, ",");
if (result)
{
return result;
}
free(g_ServerIP_Buff);
}
g_ServerIP_Buff = (char*)malloc(strlen(ServerIP) + 1);
strncpy(g_ServerIP_Buff, ServerIP, strlen(ServerIP) + 1);
return strtok(g_ServerIP_Buff, ",");
}
/// <summary>
/// 轮询模式 错误切换 定时切换
/// </summary>
/// <param name="rotation_opt"></param>
/// <param name="ServerIP"></param>
/// <param name="number"></param>
/// <returns></returns>
char* beacon_Rotation_Strategy(rotationstruc* rotation_opt, char* ServerIP, int number)
{
if (rotation_opt->strategyID == 1)// random 随机选择
{
return (char*)random_selection(ServerIP);
}
if (rotation_opt->strategyID == 2)//rotate或failover
{
return (char*)carousel(rotation_opt, ServerIP, number);
}
return no_rotation(ServerIP); // 非轮询
}