-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
executable file
·191 lines (158 loc) · 3.4 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
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
/*
* main.cpp
*
* Created on: 2011-12-12
* Author: luocj
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#else
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/file.h>
#endif
#include "frame_errordef.h"
#include "frame_logengine.h"
#include "main_frame.h"
#include "def/server_errordef.h"
#include "def/server_namespace.h"
#include "lightframe.h"
using namespace FRAME_NAMESPACE;
using namespace FRAME_GATESERVER_NAMESPACE;
#define LOGPREFIX "log/"SERVER_NAME_STRING
#ifndef WIN32
void sigusr1_handle(int32_t nSigVal)
{
g_eServerCommand = enmServerCommand_Reload;
signal(SIGUSR1, sigusr1_handle);
}
void sigusr2_handle(int32_t nSigVal)
{
g_eServerCommand = enmServerCommand_Terminate;
signal(SIGUSR2, sigusr2_handle);
}
#endif
void print_version()
{
/*
printf("%s, %s\n", SERVER_NAME, SERVER_VERSION);
#ifdef DEBUG
printf("only for debug\n");
#else
printf("for release\n");
#endif
*/
char version[128] = { 0 };
GET_VERSION(version);
printf("%s, %s\n", SERVER_NAME_STRING, version);
#ifdef DEBUG
printf("Build for debug\n");
#else
printf("Build for release\n");
#endif
#ifdef SVNVER
printf("svn version no:%d\n", SVNVER);
#endif
#ifdef SVNDIR
printf("source dir in svn:%s\n", SVNDIR);
#endif
printf("\n");
print_version_common_lib();
print_version_frame_lib();
}
void print_help()
{
printf("directories:\n");
printf("\t./config: include all config files\n");
printf("\t./log: include all log file\n");
printf("config files:\n");
printf("\t"SERVER_NAME_STRING".xml: main config file of "SERVER_NAME_STRING"\n");
printf("log files:\n");
printf("\t"SERVER_NAME_STRING"_yyyymmdd.log\n");
printf("\t"SERVER_NAME_STRING"_main_s_s_yyyymmdd.log\n");
printf("\t"SERVER_NAME_STRING"_netio_s_s_yyyymmdd.log\n");
}
int32_t main(int argc, char** argv)
{
bool bResume = false;
bool bDaemon = false;
//创建框架实例对象
CREATE_MAINFRAME_INSTANCE();
if(argc <= 1)
{
g_bTestMode = true;
}
//读取命令行参数
if (argc > 1)
{
if (0 == strcasecmp((const char*)argv[1], "-v"))
{
print_version();
exit(0);
}
if (0 == strcasecmp((const char*)argv[1], "-h"))
{
print_help();
exit(0);
}
if (0 == strcasecmp((const char*)argv[1], "-rd"))
{
bResume = true;
bDaemon = true;
}
if (0 == strcasecmp(argv[1], "-d"))
{
bResume = false;
bDaemon = true;
}
}
#ifndef WIN32
int lock_fd = open((const char *)argv[0], O_RDONLY);
if (lock_fd < 0)
{
exit(1);
}
if (flock(lock_fd, LOCK_EX | LOCK_NB) < 0)
{
CFrameLogEngine::WriteLog(LOGPREFIX, enmLogLevel_Error, SERVER_NAME_STRING" was launched!\n");
exit(1);
}
#endif
#ifndef WIN32
//初始化为守护进程
if (bDaemon)
{
g_Frame.SetDaemon();
}
//安装信号处理函数
signal(SIGUSR1, sigusr1_handle);
signal(SIGUSR2, sigusr2_handle);
#endif
int32_t ret = S_OK;
//初始化服务器
ret = g_Frame.Initialize();
if (0 <= ret)
{
//启动服务器
ret = g_Frame.Start();
if (0 <= ret)
{
//主线程进入运行状态
g_Frame.Run();
}
}
//结束服务器
ret = g_Frame.Uninitialize();
if (0 > ret)
{
exit(1);
}
CFrameLogEngine::WriteLog(LOGPREFIX, enmLogLevel_Notice, SERVER_NAME_STRING" exited safe!\n\n\n");
exit(0);
}