-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnode_lua.h
266 lines (241 loc) · 7.09 KB
/
node_lua.h
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#ifndef NODE_LUA_H_
#define NODE_LUA_H_
#include "common.h"
#include "singleton.h"
#include "context_mgr.h"
#include "worker_mgr.h"
#include "context.h"
#include "lbinary.h"
class message_t;
class network_t;
class worker_mgr_t;
class node_lua_t : public singleton_t<node_lua_t> {
public:
node_lua_t(int argc, char* argv[], char* env[]);
~node_lua_t();
/* context send by ctx */
bool context_send(context_t* ctx, message_t& msg);
template < class type >
FORCE_INLINE bool context_send(context_t* ctx, uint32_t source, int session, int msg_type, type data)
{
message_t msg(source, session, msg_type, data);
return context_send(ctx, msg);
}
/* context send by handle */
bool context_send(uint32_t handle, message_t& msg);
template < class type >
FORCE_INLINE bool context_send(uint32_t handle, uint32_t source, int session, int msg_type, type data)
{
message_t msg(source, session, msg_type, data);
return context_send(handle, msg);
}
/* specific context send interface */
FORCE_INLINE bool context_send_string_safe(uint32_t handle, uint32_t source, int session, int msg_type, const char *data)
{
char* str = data ? nl_strdup(data) : NULL;
message_t msg(source, session, msg_type, str);
if (context_send(handle, msg))
return true;
message_clean(msg);
return false;
}
FORCE_INLINE bool context_send_string_safe(context_t* ctx, uint32_t source, int session, int msg_type, const char *data)
{
char* str = data ? nl_strdup(data) : NULL;
message_t msg(source, session, msg_type, str);
if (context_send(ctx, msg))
return true;
message_clean(msg);
return false;
}
/* specific context send interface */
FORCE_INLINE bool context_send_binary_safe(uint32_t handle, uint32_t source, int session, int msg_type, const char *data, int32_t length)
{
binary_t binary = { data ? sdsnewlen(data, length) : NULL };
message_t msg(source, session, msg_type, binary);
if (context_send(handle, msg))
return true;
message_clean(msg);
return false;
}
FORCE_INLINE bool context_send_binary_safe(context_t* ctx, uint32_t source, int session, int msg_type, const char *data, int32_t length)
{
binary_t binary = { data ? sdsnewlen(data, length) : NULL };
message_t msg(source, session, msg_type, binary);
if (context_send(ctx, msg))
return true;
message_clean(msg);
return false;
}
FORCE_INLINE bool context_send_buffer_safe(uint32_t handle, uint32_t source, int session, int msg_type, buffer_t& buffer)
{
buffer_grab(buffer);
message_t msg(source, session, msg_type, buffer);
bool ret = context_send(handle, msg);
if (!ret) {
buffer_release(buffer);
}
return ret;
}
FORCE_INLINE bool context_send_buffer_safe(context_t* ctx, uint32_t source, int session, int msg_type, buffer_t& buffer)
{
buffer_grab(buffer);
message_t msg(source, session, msg_type, buffer);
bool ret = context_send(ctx, msg);
if (!ret) {
buffer_release(buffer);
}
return ret;
}
FORCE_INLINE bool context_send_buffer_release(uint32_t handle, uint32_t source, int session, int msg_type, buffer_t& buffer)
{
message_t msg(source, session, msg_type, buffer);
bool ret = context_send(handle, msg);
if (!ret) {
buffer_release(buffer);
}
return ret;
}
FORCE_INLINE bool context_send_buffer_release(context_t* ctx, uint32_t source, int session, int msg_type, buffer_t& buffer)
{
message_t msg(source, session, msg_type, buffer);
bool ret = context_send(ctx, msg);
if (!ret) {
buffer_release(buffer);
}
return ret;
}
FORCE_INLINE bool context_send_array_release(uint32_t handle, uint32_t source, int session, int msg_type, message_array_t* array)
{
message_t msg(source, session, msg_type, array);
bool ret = context_send(handle, msg);
if (!ret) {
message_clean(msg);
}
return ret;
}
FORCE_INLINE bool context_send_array_release(context_t* ctx, uint32_t source, int session, int msg_type, message_array_t* array)
{
message_t msg(source, session, msg_type, array);
bool ret = context_send(ctx, msg);
if (!ret) {
message_clean(msg);
}
return ret;
}
template < class type >
uint32_t context_create(uint32_t parent, int32_t argc, char* argv[], char* env[]) {
uint32_t handle = 0;
type *ctx = new type();
if (m_ctx_mgr->register_context(ctx, parent)) {
if (ctx->init(argc, argv, env)) {
ctx->set_inited(true);
handle = ctx->get_handle();
m_worker_mgr->push_context(ctx);
ctx->release();
m_worker_mgr->check_load(m_ctx_mgr->get_context_count());
return handle;
}
m_ctx_mgr->retire_context(ctx);
}
ctx->release();
context_check_alive(parent);
return 0;
}
/* send destroy signal, can be called anywhere */
void context_destroy(uint32_t handle, uint32_t src_handle, const char* reason, ...)
{
if (handle == 0) return;
char buffer[512] = { '\0' };
if (reason) {
va_list argp;
va_start(argp, reason);
vsnprintf(buffer, sizeof(buffer), reason, argp);
buffer[sizeof(buffer) - 1] = '\0';
va_end(argp);
}
context_send_string_safe(handle, src_handle, 0, SYSTEM_CTX_DESTROY, buffer);
}
/* destroy self, can be called only on the running context, use it carefully */
void context_destroy(context_t*ctx, uint32_t src_handle, const char* reason, ...)
{
uint32_t handle = ctx->get_handle();
if (handle == 0) return;
char buffer[1024] = { '\0' };
int32_t used = sprintf(buffer, "killed by context:0x%08x", src_handle);
if (reason) {
strcat(buffer + used, ", ");
used = used + 2;
va_list argp;
va_start(argp, reason);
vsnprintf(buffer + used, sizeof(buffer) - used, reason, argp);
buffer[sizeof(buffer) - 1] = '\0';
va_end(argp);
}
ctx->deinit(buffer); /* ctx->m_handle is valid in this function. */
ctx->set_inited(false);
m_ctx_mgr->retire_context(ctx);
context_check_alive(src_handle);
}
void context_check_alive(uint32_t src_handle)
{
uint32_t ctx_size = m_ctx_mgr->get_context_count();
if (ctx_size == 1) {
context_destroy(m_logger->get_handle(), src_handle, NULL);
return;
}
if (ctx_size == 0) {
m_worker_mgr->stop();
return;
}
}
bool log_binary_release(uint32_t src_handle, binary_t binary) {
if (m_logger && context_send(m_logger, src_handle, 0, LOG_MESSAGE, binary)) {
return true;
}
sdsfree(binary.m_data);
return false;
}
bool log_fmt(uint32_t src_handle, const char* fmt, ...) {
if (m_logger && fmt) {
char *buf = NULL;
size_t buflen = 64;
va_list argp;
va_list copy;
va_start(argp, fmt);
while (1) {
buf = (char *)nl_malloc(buflen);
if (buf == NULL) break;
buf[buflen - 2] = '\0';
va_copy(copy, argp);
vsnprintf(buf, buflen, fmt, copy);
if (buf[buflen - 2] != '\0') {
nl_free(buf);
buflen *= 2;
continue;
}
break;
}
va_end(argp);
if (buf && context_send(m_logger, src_handle, 0, LOG_MESSAGE, buf)) {
return true;
}
nl_free(buf);
}
return false;
}
FORCE_INLINE uint32_t is_handle_illegal(uint32_t handle) const {
return handle == m_logger->get_handle();
}
private:
network_t *m_network;
context_mgr_t *m_ctx_mgr;
worker_mgr_t *m_worker_mgr;
context_t *m_logger;
public:
static int32_t m_cpu_count;
private:
static bool m_inited;
static void init_once();
};
#endif