forked from socoding/node-lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_lua.h
More file actions
193 lines (174 loc) · 5.23 KB
/
node_lua.h
File metadata and controls
193 lines (174 loc) · 5.23 KB
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
#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"
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;
}
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();
if (m_ctx_mgr->get_context_count() == 0) {
m_worker_mgr->stop();
}
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[512] = { '\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);
if (m_ctx_mgr->get_context_count() == 0) {
m_worker_mgr->stop();
}
}
private:
network_t *m_network;
context_mgr_t *m_ctx_mgr;
worker_mgr_t *m_worker_mgr;
public:
static int32_t m_cpu_count;
private:
static bool m_inited;
static void init_once();
};
#endif