-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetServer.c
358 lines (266 loc) · 6.96 KB
/
NetServer.c
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* -- $Id: NetServer.cpp,v 1.12.2.2.4.9 2004/08/03 06:08:32 yuzhe Exp $ -- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include "SysConfExt.h"
#include "NetLog.h"
#include "Debuger.h"
#include "NetServer.h"
#include "CtrlLinkTbl.h"
#include "DeadThreadTbl.h"
#include "LinkCtrl.h"
/*****************
* 全局变量定义
*****************/
GLOBAL_STATE GlobalState; // 线程状态
unsigned int USER_AUTH_PORT = 6018;
int CurPort;
static void
Timer_Thread_Cleanup (void *pinfo)
{
TRACE (12,
(" ####### Processing Timer_Thread Cleanup Start! ######### \n"));
TRACE (12,
(" ####### Processing Timer_Thread Cleanup Finished! ######### \n"));
TRACE (14, (" Timer thread exit!\n"));
return;
}
static void *
Timer_Thread (void *pinfo)
{
int i;
pthread_cleanup_push (Timer_Thread_Cleanup, NULL);
for (;;)
{
//置位“已超时的Link控制线程”的停止位
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
if (pcld->socket_fd == 0)
continue;
pthread_mutex_lock (&pcld->mutex);
pcld->notinformcnt++;
// TRACE (12, (" Timer Count = %d\n ", pcld->notinformcnt));
//未收到握手报文时间大于32分钟(30秒 * 64),结束控制线程
if (pcld->notinformcnt > MAX_NOTINFORM_CNT)
{
pthread_cancel (pcld->ctrl_tid);
}
pthread_mutex_unlock (&pcld->mutex);
}
//10
sleep (HANDSHAKE_PERIOD);
}
pthread_cleanup_pop (1);
return (void *) 0;
}
/*Join_Thread()
*PURPOSE
*通过pthread_join涵数回收死亡线程,具体原理还不太明白
*ARGUMENTS
*void *pinfo - no use
*/
static void *
Join_Thread (void *pinfo)
{
int i;
while (GlobalState.stop_join_thread != 1)
{
for (i = 0; i < MAX_CONN_NUM; i++)
{
pthread_mutex_lock (&DeadThreadTbl.mutex);
if (DeadThreadTbl.tid_tbl[i] != 0)
{
TRACE (5,
(" Join thread will reclaim the tid : %ld\n ",
DeadThreadTbl.tid_tbl[i]));
pthread_join (DeadThreadTbl.tid_tbl[i], NULL);
DeadThreadTbl.tid_tbl[i] = 0;
TRACE (5, (" Join thread has reclaimed it\n "));
}
pthread_mutex_unlock (&DeadThreadTbl.mutex);
}
//checking every 5 second
usleep (5000);
}
TRACE (14, (" Thread that joins dead_threads exit!\n"));
return (void *) 0;
}
static void
Server_Thread_Cleanup (void *pinfo)
{
TRACE (12,
(" ####### Processing NET_SERVER Cleanup Start! ######### \n"));
//停止所有LINK_CTRL线程
int i;
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
if (pcld->ctrl_tid == 0)
continue;
pthread_cancel (pcld->ctrl_tid);
}
//关闭Listen端口
int sock_fd = *(int *) pinfo;
if (close (sock_fd) == -1)
{
perror ("close");
}
//修改Global变量
// GlobalState.listen_thread_id = 0;
TRACE (12,
(" ####### Processing NET_SERVER Cleanup Finished! ######### \n"));
return;
}
static void *
Server_Thread (void *pinfo)
{
//-- 初始化全局表格 --//
InitDeadThreadTbl (); //初始化死亡线程组织结构表
InitCtrlLinkTbl (); //初始化控制连接组织结构表,该结构描述了每个连接的信息
//-- 回收“死亡线程”启动 --//
TRACE (14, ("Start dead-threads reclaim thread!\n"));
if (pthread_create
(&GlobalState.join_thread_id, NULL, Join_Thread, NULL) != 0)
{
TRACE (6, ("Timer thread start Err!\n"));
}
//-- 定时器线程启动 --//
TRACE (14, ("Start timer thread!\n"));
if (pthread_create
(&GlobalState.timer_thread_id, NULL, Timer_Thread, NULL) != 0)
{
TRACE (6, ("Timer thread start Err!\n"));
}
//FIXME FIXME FIXME FIXME
//-- 准备网络连接 --//
int listen_fd;
if ((listen_fd = socket (AF_INET, SOCK_STREAM, 0)) == -1)
{
perror ("socket");
logRec (" socket err,errno:%d,id:%d\n", errno, getpid ());
return (void *) 0;
}
TRACE (14, ("Succeed to create socket!\n"));
struct sockaddr_in this_addr;
struct sockaddr_in client_addr;
this_addr.sin_family = AF_INET;
this_addr.sin_port = htons (USER_AUTH_PORT);
this_addr.sin_addr.s_addr = INADDR_ANY;
memset (&(this_addr.sin_zero), '\0', 8);
int reuse = 1;
setsockopt (listen_fd, SOL_SOCKET, SO_REUSEADDR, (const void *) &reuse,
sizeof (int));
if (bind
(listen_fd, (struct sockaddr *) &this_addr,
sizeof (struct sockaddr)) == -1)
{
perror ("bind");
return (void *) 0;
}
TRACE (14, ("Succeed to bind!\n"));
if (listen (listen_fd, MAX_CONN_NUM) == -1)
{
perror ("listen");
return (void *) 0;
}
TRACE (14, ("Succeed to listen!\n"));
//PUSH & POP
pthread_cleanup_push (Server_Thread_Cleanup, &listen_fd);
//-- 开始接受客户端连接请求 --//
int link_ctrl_fd = 0;
while (1)
{
TRACE (14, ("Waiting for connection!\n"));
socklen_t sin_size = sizeof (client_addr);
link_ctrl_fd =
accept (listen_fd, (struct sockaddr *) &client_addr,
&sin_size);
if (link_ctrl_fd == -1)
{
TRACE (0, ("!!! accept failed . !!! \n"));
perror ("accept");
break;
}
TRACE (14,
("Got connection request, accept return %d !\n",
listen_fd));
//create with detach status defaultly
pthread_t link_ctrl_tid;
pthread_create (&link_ctrl_tid, NULL,
(void *(*)(void *)) Link_Ctrl,
(void *) (unsigned long long )link_ctrl_fd);
}
/*pthread_cleanup_pop
*PURPOSE
*execute cleanup function routine, or pop up 1 stack element only
*ARGUMENTS
*0 - pop up 1 stack element only
*1 - pop up 1 stack element and execute function toutine as pthread_cleanup_push
*/
pthread_cleanup_pop (1);
return (void *) 0;
}
int
StartNetServer ()
{
//初始化GlobalState变量, '\0' 用printf输出时跟"(int)0"无差别, 但跟'0'不同
memset (&GlobalState, '\0', sizeof (GLOBAL_STATE));
CurPort = USER_AUTH_PORT + 1;
// calling this thread function directlly
Server_Thread(NULL);
return 0;
}
int
StopNetServer (void)
{
//停止所有LINK_CTRL线程
int i;
PCTRL_LINK_DESC pcld = CtrlLinkTbl;
for (i = 0; i < MAX_CONN_NUM; i++, pcld++)
{
if (pcld->ctrl_tid == 0)
continue;
//停止线程
pthread_cancel (pcld->ctrl_tid);
pthread_join (pcld->ctrl_tid, NULL);
}
//停止LISTEN线程
/*
pthread_cancel (GlobalState.listen_thread_id);
sleep (1);
TRACE (12, ("+++++ Terminating Listen Thread ++++++++\n"));
pthread_join (GlobalState.listen_thread_id, NULL);
GlobalState.listen_thread_id = 0;
*/
TRACE (12, ("+++++ Terminating Timer Thread ++++++++\n"));
//停止TIMER线程
pthread_cancel (GlobalState.timer_thread_id);
pthread_join (GlobalState.timer_thread_id, NULL);
GlobalState.timer_thread_id = 0;
TRACE (12, ("+++++ Terminating Join Thread ++++++++\n"));
//停止JOIN线程
GlobalState.stop_join_thread = 1;
pthread_join (GlobalState.join_thread_id, NULL);
GlobalState.join_thread_id = 0;
GlobalState.stop_join_thread = 0;
//全局结构清除
// FIXME CDebuger *pd = CDebuger::Instance();
// pd->SetDebugLevel(0);
//恢复全局变量
memset (&GlobalState, '\0', sizeof (GLOBAL_STATE));
DeinitDeadThreadTbl ();
DeinitCtrlLinkTbl ();
printf ("\n\n # NetServer exits normally. # \n\n ");
return 0;
}