-
Notifications
You must be signed in to change notification settings - Fork 2
/
bindconf.c
executable file
·129 lines (104 loc) · 2.87 KB
/
bindconf.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
// C89
#include <stdlib.h>
#include <string.h>
// POSIX
#include <arpa/inet.h>
#include <sys/mman.h>
#include <libtaomee/log.h>
#include <libtaomee/conf_parser/config.h>
// Self-define
#include "bindconf.h"
// global varibles for bindconf.c
bind_config_t bindconf;
enum {
bind_conf_max_field_num = 4
};
/**
* load_bind_file - parse bind config file @file_name
*
* return: 0 on success, otherwise -1
*/
int load_bind_file(const char* file_name)
{
int ret_code = -1;
char* buf;
if ( mmap_config_file(file_name, &buf) > 0 ) {
char* start = buf;
char* end;
char* field[bind_conf_max_field_num];
bind_config_elem_t* bc;
size_t len = strlen(buf);
while (buf + len > start) {
end = strchr(start, '\n');
if ( end && *end ) {
*end = '\0';
}
if ( (*start != '#') && (str_split(0, start, field, bind_conf_max_field_num) == bind_conf_max_field_num) ) {
bc = &(bindconf.configs[bindconf.bind_num]);
// Online
bc->online_id = atoi(field[0]); // online id
strncpy(bc->online_name, field[1], sizeof(bc->online_name) - 1); // online name
strncpy(bc->bind_ip, field[2], sizeof(bc->bind_ip) - 1); // online ip
bc->bind_port = atoi(field[3]); // online port
// increase bind_num
++(bindconf.bind_num);
}
start = end + 1;
if (bindconf.bind_num > max_listen_fds) {
goto exit_entry;
}
}
munmap(buf, len);
ret_code = 0;
}
exit_entry:
BOOT_LOG(ret_code, "load bind file:%s", file_name);
}
const char * getstring_by_index(lua_State* L , int index )
{
static char buf[4096];
memset(buf,0,sizeof(buf) );
lua_pushinteger(L, index);
lua_gettable(L, -2);
strncpy(buf ,luaL_checkstring(L, -1),sizeof(buf)-1 );
lua_pop(L, 1);
return buf;
}
double getnumber_by_index(lua_State* L , int index )
{
double ret;
lua_pushinteger(L, index);
lua_gettable(L, -2);
ret=luaL_checknumber(L, -1);
lua_pop(L, 1);
return ret;
}
int load_bind_from_lua(lua_State * L )
{
int ret_code=-1;
bind_config_elem_t* bc;
lua_getglobal(L,"async_server_bind_map");
//bind_map
lua_pushnil(L);
while (lua_next(L, -2)) {
const char * p=NULL;
//int v;
bc = &(bindconf.configs[bindconf.bind_num]);
bc->online_id=getnumber_by_index(L,1 );
p=getstring_by_index(L,2 );
strncpy(bc->online_name, p, sizeof(bc->online_name) - 1); // online name
p=getstring_by_index(L,3 );
strncpy(bc->bind_ip, p, sizeof(bc->bind_ip) - 1); // online ip
bc->bind_port=getnumber_by_index(L,4 );
++(bindconf.bind_num);
if (bindconf.bind_num > max_listen_fds) {
goto exit_entry;
}
lua_pop(L, 1);
}
//去掉 - async_server_bind_map
lua_pop(L, 1);
ret_code=0;
exit_entry:
BOOT_LOG(ret_code, "load bind info from lua " );
}