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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
#include <stdlib.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <utlist.h>
#include "lua_binding_function.h"
/* ***** ***** ***** ***** ***** ***** */
int lua_get_worker_thread_num(struct lua_state *state)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 1)
{
lua_settop(L, 0);
return 0;
}
struct stellar *st = (struct stellar *)lua_topointer(L, -1);
lua_settop(L, 0);
int thread_num = stellar_get_worker_thread_num(st);
lua_pushinteger(L, thread_num);
return 1;
}
int lua_get_current_thread_id(struct lua_state *state)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 1)
{
lua_settop(L, 0);
return 0;
}
struct stellar *st = (struct stellar *)lua_topointer(L, -1);
lua_settop(L, 0);
int thread_id = stellar_get_current_thread_id(st);
lua_pushinteger(L, thread_id);
return 1;
}
int lua_get_stellar_pointer(struct lua_state *state)
{
lua_State *L = (lua_State *)state;
lua_settop(L, 0);
struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state);
struct stellar *st = plugin_manage->st;
lua_pushlightuserdata(L, (void *)st);
return 1;
}
/*
int lua_get_plugin_manage_pointer(struct lua_state *state)
{
lua_settop((lua_State *)state, 0);
struct lua_plugin_manage * plugin_manage = lua_state_get_plugin_manage(state);
lua_pushlightuserdata((lua_State *)state, (void *)plugin_manage);
return 1;
}
*/
/* ***** ***** ***** ***** ***** ***** */
int lua_session_plugin_regist(struct lua_state *state)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 4 || lua_type(L, -4) != LUA_TLIGHTUSERDATA)
{
lua_settop(L, 0);
return 0;
}
/* stack -1 */
int plugin_env_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
if (plugin_env_ref_id == LUA_REFNIL)
plugin_env_ref_id = 0;
/* stack -2 */
int ctx_free_fn_ref_id = 0;
if (lua_type(L, -1) == LUA_TFUNCTION)
ctx_free_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
else
lua_pop(L, 1);
/* stack -3 */
int ctx_new_fn_ref_id = 0;
if (lua_type(L, -1) == LUA_TFUNCTION)
ctx_new_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
else
lua_pop(L, 1);
/* stack -4 */
struct stellar *st = (struct stellar *)lua_topointer(L, -1);
lua_settop(L, 0);
struct lua_session_plugin_env *new_plugin_env = (struct lua_session_plugin_env *)calloc(1, sizeof(struct lua_session_plugin_env));
memset(new_plugin_env, 0, sizeof(struct lua_session_plugin_env));
new_plugin_env->lua_ctx_new_fn_ref_id = ctx_new_fn_ref_id;
new_plugin_env->lua_ctx_free_fn_ref_id = ctx_free_fn_ref_id;
new_plugin_env->lua_plug_env_ref_id = plugin_env_ref_id;
int plugin_id = stellar_session_plugin_register(st, lpm_ctx_new_func, lpm_ctx_free_func, (void *)new_plugin_env);
struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state);
LL_APPEND(plugin_manage->session_plugin_env_list, new_plugin_env);
new_plugin_env->plugin_manage = plugin_manage;
new_plugin_env->session_plugin_id = plugin_id;
lua_settop(L, 0);
lua_pushinteger(L, plugin_id);
return 1;
}
int lua_packet_plugin_regist(struct lua_state *state)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 4 || lua_type(L, -4) != LUA_TLIGHTUSERDATA)
{
lua_settop(L, 0);
return 0;
}
/* stack -1 */
int plugin_env_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
if (plugin_env_ref_id == LUA_REFNIL)
plugin_env_ref_id = 0;
/* stack -2 */
int on_packet_fn_ref_id = 0;
if (lua_type(L, -1) == LUA_TFUNCTION)
on_packet_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
else
lua_pop(L, 1);
/* stack -3 */
int ip_protocol = lua_tointeger(L, -1);
lua_pop(L, 1);
/* stack -4 */
struct stellar *st = (struct stellar *)lua_topointer(L, -1);
lua_settop(L, 0);
struct lua_packet_plugin_env *new_plugin_env = (struct lua_packet_plugin_env *)calloc(1, sizeof(struct lua_packet_plugin_env));
memset(new_plugin_env, 0, sizeof(struct lua_packet_plugin_env));
new_plugin_env->lua_plug_env_ref_id = plugin_env_ref_id;
new_plugin_env->lua_on_packet_fn_ref_id = on_packet_fn_ref_id;
int plugin_id = stellar_packet_plugin_register(st, (unsigned char)ip_protocol, lpm_on_packet_func, (void *)new_plugin_env);
struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state);
LL_APPEND(plugin_manage->packet_plugin_env_list, new_plugin_env);
new_plugin_env->plugin_manage = plugin_manage;
new_plugin_env->packet_plugin_id = plugin_id;
lua_settop(L, 0);
lua_pushinteger(L, plugin_id);
return 1;
}
/* ***** ***** ***** ***** ***** ***** */
int lua_session_get_type(struct lua_state *state)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 1 || lua_type(L, -1) != LUA_TLIGHTUSERDATA)
{
lua_settop(L, 0);
return 0;
}
struct session *sess = (struct session *)lua_topointer(L, -1);
lua_settop(L, 0);
if (!sess)
return 0;
lua_pushinteger(L, (int)session_get_type(sess));
return 1;
}
/* ***** ***** ***** ***** ***** ***** */
#define MQ_TYPE_PACKET 1
#define MQ_TYPE_SESSION 2
static int lua_plugin_manage_msg_mq_create_topic(struct lua_state *state, int type)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 4 || lua_type(L, -3) != LUA_TSTRING || lua_type(L, -4) != LUA_TLIGHTUSERDATA)
{
lua_settop(L, 0);
return 0;
}
/* stack -1 */
int free_arg_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
if (free_arg_ref_id == LUA_REFNIL)
free_arg_ref_id = 0;
/* stack -2 */
int free_fn_ref_id = 0;
if (lua_type(L, -1) == LUA_TFUNCTION)
free_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
else
lua_pop(L, 1);
/* stack -3 */
char *topic_name = strdup((char *)lua_tostring(L, -1));
lua_pop(L, 1);
/* stack -4 */
struct stellar *st = (struct stellar *)lua_topointer(L, -1);
lua_settop(L, 0);
struct lua_message_free_arg *new_message_free_arg = (struct lua_message_free_arg *)calloc(1, sizeof(struct lua_message_free_arg));
memset(new_message_free_arg, 0, sizeof(struct lua_message_free_arg));
new_message_free_arg->lua_msg_free_fn_ref_id = free_fn_ref_id;
new_message_free_arg->lua_msg_free_arg_ref_id = free_arg_ref_id;
int topic_id = -1;
if (type == MQ_TYPE_PACKET)
topic_id = stellar_packet_mq_create_topic(st, (const char *)topic_name, lpm_packet_message_free_func, new_message_free_arg);
else if (type == MQ_TYPE_SESSION)
topic_id = stellar_session_mq_create_topic(st, (const char *)topic_name, lpm_session_message_free_func, new_message_free_arg);
if (topic_id >= 0)
{
struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state);
LL_APPEND(plugin_manage->message_free_arg_list, new_message_free_arg);
new_message_free_arg->topic_id = topic_id;
new_message_free_arg->plugin_manage = plugin_manage;
lua_pop(L, 1);
}
else
{
if (new_message_free_arg)
free(new_message_free_arg);
return 0;
}
lua_pushinteger(L, topic_id);
return 1;
}
static int lua_plugin_manage_msg_mq_get_topic_id(struct lua_state *state, int type)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TSTRING || lua_type(L, -2) != LUA_TLIGHTUSERDATA)
{
lua_settop(L, 0);
return 0;
}
char *topic_name = strdup(lua_tostring(L, -1));
lua_pop(L, 1);
struct stellar *st = (struct stellar *)lua_topointer(L, -1);
lua_settop(L, 0);
int topic_id = -1;
if (type == MQ_TYPE_PACKET)
topic_id = stellar_packet_mq_get_topic_id(st, (const char *)topic_name);
else if (type == MQ_TYPE_SESSION)
topic_id = stellar_session_mq_get_topic_id(st, (const char *)topic_name);
if (topic_name)
free(topic_name);
lua_pushinteger(L, topic_id);
return 1;
}
static int lua_plugin_manage_msg_mq_update_topic(struct lua_state *state, int type)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 4 || lua_type(L, -3) != LUA_TNUMBER || lua_type(L, -4) != LUA_TLIGHTUSERDATA)
goto err;
/* stack -1 */
int free_arg_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
if (free_arg_ref_id == LUA_REFNIL)
free_arg_ref_id = 0;
/* stack -2 */
int free_fn_ref_id = 0;
if (lua_type(L, -1) == LUA_TFUNCTION)
free_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
else
lua_pop(L, 1);
/* stack -3 */
int topic_id = lua_tointeger(L, -1);
lua_pop(L, 1);
/* stack -4 */
struct stellar *st = (struct stellar *)lua_topointer(L, -1);
lua_settop(L, 0);
struct lua_message_free_arg *new_message_free_arg = (struct lua_message_free_arg *)calloc(1, sizeof(struct lua_message_free_arg));
memset(new_message_free_arg, 0, sizeof(struct lua_message_free_arg));
new_message_free_arg->topic_id = topic_id;
new_message_free_arg->lua_msg_free_fn_ref_id = free_fn_ref_id;
new_message_free_arg->lua_msg_free_arg_ref_id = free_arg_ref_id;
int update_result = -1;
if (type == MQ_TYPE_PACKET)
update_result = stellar_packet_mq_update_topic(st, topic_id, lpm_packet_message_free_func, new_message_free_arg);
else if (type == MQ_TYPE_SESSION)
update_result = stellar_session_mq_update_topic(st, topic_id, lpm_session_message_free_func, new_message_free_arg);
if (update_result)
{
if (new_message_free_arg)
free(new_message_free_arg);
goto err;
}
struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state);
new_message_free_arg->plugin_manage = plugin_manage;
LL_APPEND(plugin_manage->message_free_arg_list, new_message_free_arg);
lua_settop(L, 0);
lua_pushboolean(L, 1);
return 1;
err:
lua_settop(L, 0);
lua_pushboolean(L, 0);
return 1;
}
static int lua_plugin_manage_msg_mq_destory_topic(struct lua_state *state, int type)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 2)
goto err;
int topic_id = lua_tointeger(L, -1);
lua_pop(L, 1);
struct stellar *st = (struct stellar *)lua_topointer(L, -1);
lua_settop(L, 0);
int destory_result = -1;
if (type == MQ_TYPE_PACKET)
destory_result = stellar_packet_mq_destroy_topic(st, topic_id);
else if (type == MQ_TYPE_SESSION)
destory_result = stellar_session_mq_destroy_topic(st, topic_id);
if (destory_result < 0)
goto err;
lua_pushboolean(L, 1);
return 1;
err:
lua_settop(L, 0);
lua_pushboolean(L, 0);
return 1;
}
static int lua_plugin_manage_msg_mq_subscribe_topic(struct lua_state *state, int type)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 4 || lua_type(L, -4) != LUA_TLIGHTUSERDATA)
goto err;
int plugin_id = lua_tointeger(L, -1);
lua_pop(L, 1);
int on_message_fn_ref_id = 0;
if (lua_type(L, -1) == LUA_TFUNCTION)
on_message_fn_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
else
lua_pop(L, 1);
int topic_id = lua_tointeger(L, -1);
lua_pop(L, 1);
struct stellar *st = (struct stellar *)lua_topointer(L, -1);
lua_settop(L, 0);
int subscribe_result = -1;
if (type == MQ_TYPE_PACKET)
subscribe_result = stellar_packet_mq_subscribe(st, topic_id, lpm_on_packet_msg_func, plugin_id);
else if (type == MQ_TYPE_SESSION)
subscribe_result = stellar_session_mq_subscribe(st, topic_id, lpm_on_session_msg_func, plugin_id);
if (subscribe_result)
goto err;
struct lua_on_message_fn *on_message = NULL;
struct lua_plugin_manage *plugin_manage = lua_state_get_plugin_manage(state);
if (type == MQ_TYPE_PACKET)
on_message = hash_find_on_msg_fn(plugin_manage->on_packet_message_hashlist, topic_id, plugin_id);
else if (type == MQ_TYPE_SESSION)
on_message = hash_find_on_msg_fn(plugin_manage->on_session_message_hashlist, topic_id, plugin_id);
if (on_message)
{
on_message->lua_on_msg_fn_ref_id = on_message_fn_ref_id;
}
else
{
if (type == MQ_TYPE_PACKET)
on_message = hash_on_msg_fn_insert(plugin_manage->on_packet_message_hashlist, topic_id, plugin_id);
else if (type == MQ_TYPE_SESSION)
on_message = hash_on_msg_fn_insert(plugin_manage->on_session_message_hashlist, topic_id, plugin_id);
if (!on_message)
goto err;
else
on_message->lua_on_msg_fn_ref_id = on_message_fn_ref_id;
}
lua_settop(L, 0);
lua_pushboolean(L, 1);
return 1;
err:
lua_settop(L, 0);
lua_pushboolean(L, 0);
return 1;
}
static int lua_plugin_manage_msg_mq_publish_message(struct lua_state *state, int type)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 3)
goto err;
int msg_ref_id = luaL_ref(L, LUA_REGISTRYINDEX);
if (msg_ref_id == LUA_REFNIL)
msg_ref_id = 0;
int topic_id = lua_tointeger(L, -1);
lua_pop(L, 1);
void *p = (void *)lua_topointer(L, -1);
lua_settop(L, 0);
struct lua_context *new_context = lua_context_new(state);
new_context->lua_context_ref_id = msg_ref_id;
int publish_result = -1;
if (type == MQ_TYPE_PACKET)
publish_result = packet_mq_publish_message((struct packet *)p, topic_id, new_context);
else if (type == MQ_TYPE_SESSION)
publish_result = session_mq_publish_message((struct session *)p, topic_id, new_context);
if (publish_result)
{
luaL_unref(L, LUA_REGISTRYINDEX, new_context->lua_context_ref_id);
free(new_context);
goto err;
}
lua_pushboolean(L, 1);
return 1;
err:
lua_settop(L, 0);
lua_pushboolean(L, 0);
return 1;
}
/* ***** ***** ***** ***** ***** ***** */
int lua_packet_mq_create_topic(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_create_topic(state, MQ_TYPE_PACKET);
}
int lua_packet_mq_get_topic_id(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_get_topic_id(state, MQ_TYPE_PACKET);
}
int lua_packet_mq_update_topic(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_update_topic(state, MQ_TYPE_PACKET);
}
int lua_packet_mq_destory_topic(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_destory_topic(state, MQ_TYPE_PACKET);
}
int lua_packet_mq_subscribe_topic(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_subscribe_topic(state, MQ_TYPE_PACKET);
}
int lua_packet_mq_publish_message(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_publish_message(state, MQ_TYPE_PACKET);
}
/* ***** ***** ***** ***** ***** ***** */
int lua_session_mq_create_topic(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_create_topic(state, MQ_TYPE_SESSION);
}
int lua_session_mq_get_topic_id(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_get_topic_id(state, MQ_TYPE_SESSION);
}
int lua_session_mq_update_topic(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_update_topic(state, MQ_TYPE_SESSION);
}
int lua_session_mq_destory_topic(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_destory_topic(state, MQ_TYPE_SESSION);
}
int lua_session_mq_subscribe_topic(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_subscribe_topic(state, MQ_TYPE_SESSION);
}
int lua_session_mq_publish_message(struct lua_state *state)
{
return lua_plugin_manage_msg_mq_publish_message(state, MQ_TYPE_SESSION);
}
int lua_session_mq_ignore_message(struct lua_state *state)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 3)
goto err;
int plugin_id = lua_tointeger(L, -1);
lua_pop(L, 1);
int topic_id = lua_tointeger(L, -1);
lua_pop(L, 1);
struct session *sess = (struct session *)lua_topointer(L, -1);
lua_settop(L, 0);
if (session_mq_ignore_message(sess, topic_id, plugin_id))
goto err;
lua_pushboolean(L, 1);
return 1;
err:
lua_settop(L, 0);
lua_pushboolean(L, 0);
return 1;
}
int lua_session_mq_unignore_message(struct lua_state *state)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 4)
goto err;
int plugin_id = lua_tointeger(L, -1);
lua_pop(L, 1);
int topic_id = lua_tointeger(L, -1);
lua_pop(L, 1);
struct session *sess = (struct session *)lua_topointer(L, -1);
lua_settop(L, 0);
if (session_mq_unignore_message(sess, topic_id, plugin_id))
goto err;
lua_pushboolean(L, 1);
return 1;
err:
lua_settop(L, 0);
lua_pushboolean(L, 0);
return 1;
}
int lua_session_mq_topic_is_active(struct lua_state *state)
{
lua_State *L = (lua_State *)state;
if (lua_gettop(L) != 2)
goto err;
int topic_id = lua_tointeger(L, -1);
lua_pop(L, 1);
struct session *sess = (struct session *)lua_topointer(L, -1);
lua_settop(L, 0);
/* 1 means active */
if (session_mq_topic_is_active(sess, topic_id) != 1)
goto err;
lua_pushboolean(L, 1);
return 1;
err:
lua_settop(L, 0);
lua_pushboolean(L, 0);
return 1;
}
|