-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.cpp
222 lines (168 loc) · 3.95 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
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
#include "net.h"
#include "timer.h"
#include "timer_event.h"
#include "log.h"
#include "protocal.h"
#include "getopt.h"
#include "daemonize.h"
#include "lua_interface.h"
#include "mysql_part.h"
#include "redis.h"
#include "interface_c.h"
#include "timewheel.h"
#include <unistd.h>
#include <sys/types.h>
#include <sched.h>
#include <signal.h>
#define LUA_GAME_ENGINE_VERSION "1.4.2b201312101700"
#define LUA_GAME_ENGINE_VERSION_MAJOR 1
#define LUA_GAME_ENGINE_VERSION_MINOR 4
#define LUA_GAME_ENGINE_VERSION_BUGFIX 2
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#include "tolua++.h"
/* Exported function */
TOLUA_API int tolua_interface_open (lua_State* tolua_S);
#define CPU_NUM sysconf(_SC_NPROCESSORS_CONF)
static const char* PidPath = "./server.pid";
static int
__cpus_nums()
{
return sysconf(_SC_NPROCESSORS_CONF);
}
static void
__binding_cpu(void)
{
int curr_cpu_max = __cpus_nums();
srand(time(NULL));
int num = rand() % curr_cpu_max;
while(!num) {
num = rand() % curr_cpu_max;
}
log_debug("CPU: %d\n", num);
cpu_set_t mask;
__CPU_ZERO(&mask);
__CPU_SET(num, &mask);
sched_setaffinity(0,sizeof(cpu_set_t),&mask);
}
static void
__reload_config(int signo)
{
int ret, rvalue;
ret = call_lua("reload_config", ">d", &rvalue);
if (ret != 0) {
log_debug("reload_config failed.");
} else {
log_debug("reload_config success.");
}
}
static void
__signal_binding()
{
struct sigaction act;
struct sigaction act2;
sigemptyset(&act.sa_mask);
act.sa_handler = SIG_IGN;
//sigaction(SIGHUP, &act, NULL);
sigaction(SIGPIPE, &act, NULL);
sigemptyset(&act2.sa_mask);
act2.sa_handler = __reload_config;
sigaction(SIGHUP, &act2, NULL);
}
static void
__version()
{
log_debug("%s", LUA_GAME_ENGINE_VERSION);
}
static void
__pidfile()
{
int fd = open(PidPath, O_CREAT | O_RDWR | O_TRUNC, 00666);
if (fd <= 0) {
log_error("create pidfile failed.");
return;
}
char temp[64] = {0};
snprintf(temp, 64, "%d", getpid());
int ret = write(fd, temp, strlen(temp));
if (ret <= 0) {
log_error("write pid failed. %d %d", ret, errno);
}
}
lua_State* L;
CMysql mysql_handle;
CRedis redis_handle;
bool is_daemon = false;
int now; /* 当前系统时间 */
time_wheel_t *g_tw;
#ifdef BeginHandlerNums
Net net(BeginHandlerNums);
#else
Net net(0);
#endif
int
main(int argc, char ** argv)
{
pid_t pid;
char log_prefix[50] = {0};
now = time(NULL);
L = lua_open(); /* initialize Lua */
luaL_openlibs(L); /* load Lua base libraries */
tolua_interface_open(L);
luaL_dofile (L, "script/server.lua"); /* load the script */
// 创建一个新的表
lua_newtable(L);
int result;
char* option = "a:b:c:de:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z:";
while((result = getopt(argc, argv, option)) != -1) {
if(result >= 'a' && result <= 'z') {
if(result == 'd') {
is_daemon = true;
} else {
char s[2]={0};
sprintf(s, "%c", result);
lua_pushstring(L, s);
lua_pushstring(L, optarg);
lua_rawset(L, -3);
}
}
}
lua_setglobal(L, COMMAND_ARGS);
//初始化日志文件
//log_info("Initing\n");
if (is_daemon) {
if ( -1 == daemon(1, 0)) {
fprintf(stdout, "%s\n", "daemon error.");
}
}
pid = getpid();
snprintf(log_prefix, sizeof(log_prefix), "Log_%d", pid);
init_log(log_prefix, "./");
init_timer();
__binding_cpu();
__signal_binding();
__pidfile();
__version();
CProtocal::init();
if(!net.init()) {
log_error("init server failed");
return -1;
}
int ret = -1;
if(call_lua("handle_init", ">d", &ret)) {
return -1;
}
if(ret < 0) {
log_error("call lua function handle_init failed");
return -1;
}
if(!net.start_server()) {
log_error("start server failed");
return -1;
}
return 0;
}