-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
91 lines (82 loc) · 1.35 KB
/
main.cpp
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
#include <iostream>
#include "game_channel.h"
#include "AOI_world.h"
#include "NamePool.h"
#include "timer_channel.h"
using namespace std;
void daemonlize()
{
/*fork------>退掉父进程*/
int ichildpid = fork();
if (0 > ichildpid)
{
exit(-1);
}
if (ichildpid > 0)
{
/*父进程*/
exit(0);
}
/*子进程*/
/*设置会话id*/
setsid();
/*重定向0 1 2*/
int nullfd = open("/dev/null", O_RDWR);
if (nullfd >= 0)
{
dup2(nullfd, 0);
dup2(nullfd, 1);
dup2(nullfd, 2);
close(nullfd);
}
/*循环fork---》父进程wait--》子进程执行游戏业务*/
while (1)
{
int ipid = fork();
if (ipid < 0)
{
exit(-1);
}
if (ipid > 0)
{
//父进程
int status;
wait(&status);
/*若子进程正常退出--》不要循环fork新子进程*/
if (0 == status)
{
exit(0);
}
}
else
{
//子进程
break;
}
}
}
int main()
{
/*让进程变成守护进程*/
daemonlize();
/*加载随机姓名文件*/
if (false == NamePool::GetInstance().LoadFile())
{
cout << "加载姓名文件失败" << endl;
return -1;
}
/*初始化框架*/
if (true == ZinxKernel::ZinxKernelInit())
{
/*添加监听通道到kernel*/
if (true != ZinxKernel::Zinx_Add_Channel(*(new ZinxTCPListen(8899, new game_channel_fact()))))
{
cout << "listen failed" << endl;
}
ZinxKernel::Zinx_Add_Channel(*(new timer_channel()));
/*run*/
ZinxKernel::Zinx_Run();
ZinxKernel::ZinxKernelFini();
}
return 0;
}