forked from ideosoft/astra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
233 lines (197 loc) · 5.47 KB
/
main.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
/*
* Astra Main App
* http://cesbo.com/astra
*
* Copyright (C) 2012-2013, Andrey Dyldin <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <astra.h>
#ifndef _WIN32
# include <signal.h>
#endif
#include <setjmp.h>
#include "config.h"
bool is_sighup = false;
#ifndef _WIN32
static void signal_handler(int signum)
{
switch(signum)
{
case SIGHUP:
asc_log_hup();
is_sighup = true;
return;
case SIGPIPE:
return;
default:
astra_exit();
}
}
#else
static bool WINAPI signal_handler(DWORD signum)
{
switch(signum)
{
case CTRL_C_EVENT:
astra_exit();
break;
case CTRL_BREAK_EVENT:
astra_exit();
break;
default:
break;
}
return true;
}
#endif
static void asc_srand(void)
{
unsigned long a = clock();
unsigned long b = time(NULL);
#ifndef _WIN32
unsigned long c = getpid();
#else
unsigned long c = GetCurrentProcessId();
#endif
a = a - b; a = a - c; a = a ^ (c >> 13);
b = b - c; b = b - a; b = b ^ (a << 8);
c = c - a; c = c - b; c = c ^ (b >> 13);
a = a - b; a = a - c; a = a ^ (c >> 12);
b = b - c; b = b - a; b = b ^ (a << 16);
c = c - a; c = c - b; c = c ^ (b >> 5);
a = a - b; a = a - c; a = a ^ (c >> 3);
b = b - c; b = b - a; b = b ^ (a << 10);
c = c - a; c = c - b; c = c ^ (b >> 15);
srand(c);
}
int main(int argc, const char **argv)
{
#ifndef _WIN32
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGPIPE, signal_handler);
signal(SIGHUP, signal_handler);
signal(SIGQUIT, signal_handler);
#else
SetConsoleCtrlHandler((PHANDLER_ROUTINE)signal_handler, true);
#endif
astra_reload_entry:
asc_srand();
asc_thread_core_init();
asc_timer_core_init();
asc_socket_core_init();
asc_event_core_init();
lua = luaL_newstate();
luaL_openlibs(lua);
/* load modules */
for(int i = 0; astra_mods[i]; i++)
astra_mods[i](lua);
/* change package.path */
lua_getglobal(lua, "package");
#ifndef _WIN32
# define ASC_PATH_SEP "/"
#else
# define ASC_PATH_SEP "\\"
#endif
lua_pushfstring(lua, "." ASC_PATH_SEP "?.lua");
lua_setfield(lua, -2, "path");
lua_pushstring(lua, "");
lua_setfield(lua, -2, "cpath");
lua_pop(lua, 1);
/* argv table */
lua_newtable(lua);
for(int i = 1; i < argc; ++i)
{
lua_pushinteger(lua, i);
lua_pushstring(lua, argv[i]);
lua_settable(lua, -3);
}
lua_setglobal(lua, "argv");
#define GC_TIMEOUT (1 * 1000 * 1000)
uint64_t current_time = asc_utime();
uint64_t gc_check_timeout = current_time;
/* start */
const int main_loop_status = setjmp(main_loop);
if(main_loop_status == 0)
{
lua_getglobal(lua, "inscript");
if(lua_isfunction(lua, -1))
{
lua_call(lua, 0, 0);
}
else
{
lua_pop(lua, 1);
if(argc < 2)
{
printf("Astra " ASTRA_VERSION_STR);
printf("Usage: %s script.lua [OPTIONS]\n", argv[0]);
astra_exit();
}
int ret = -1;
if(argv[1][0] == '-' && argv[1][1] == 0)
ret = luaL_dofile(lua, NULL);
else if(!access(argv[1], R_OK))
ret = luaL_dofile(lua, argv[1]);
else
{
printf("Error: initial script isn't found\n");
astra_exit();
}
if(ret != 0)
luaL_error(lua, "[main] %s", lua_tostring(lua, -1));
}
while(true)
{
is_main_loop_idle = true;
asc_event_core_loop();
asc_timer_core_loop();
asc_thread_core_loop();
if(is_sighup)
{
is_sighup = false;
lua_getglobal(lua, "on_sighup");
if(lua_isfunction(lua, -1))
{
lua_call(lua, 0, 0);
is_main_loop_idle = false;
}
else
lua_pop(lua, 1);
}
if(is_main_loop_idle)
{
current_time = asc_utime();
if((current_time - gc_check_timeout) >= GC_TIMEOUT)
{
gc_check_timeout = current_time;
lua_gc(lua, LUA_GCCOLLECT, 0);
}
asc_usleep(1000);
}
}
}
/* destroy */
lua_close(lua);
asc_event_core_destroy();
asc_socket_core_destroy();
asc_timer_core_destroy();
asc_thread_core_destroy();
asc_log_info("[main] %s", (main_loop_status == 2) ? "reload" : "exit");
asc_log_core_destroy();
if(main_loop_status == 2)
goto astra_reload_entry;
return 0;
}