forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua-localcast.c
60 lines (56 loc) · 1.13 KB
/
lua-localcast.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
#include <lua.h>
#include <lauxlib.h>
#include "luacompat52.h"
#include "localcast.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
/*
table handles
string msg
lightuserdata ptr
integer sz
*/
static int
_pack_message(lua_State *L) {
luaL_checktype(L,1,LUA_TTABLE);
int type = lua_type(L,2);
void * msg = NULL;
size_t sz = 0;
switch(type) {
case LUA_TSTRING: {
const char * str = lua_tolstring(L,2,&sz);
msg = malloc(sz);
memcpy(msg, str, sz);
break;
}
case LUA_TLIGHTUSERDATA:
msg = lua_touserdata(L,2);
sz = luaL_checkinteger(L,3);
break;
default:
luaL_error(L, "type error : %s", lua_typename(L,type));
break;
}
struct localcast *lc = malloc(sizeof(struct localcast));
lc->n = lua_rawlen(L,1);
uint32_t *group = malloc(lc->n * sizeof(uint32_t));
int i;
for (i=0;i<lc->n;i++) {
lua_rawgeti(L,1,i+1);
group[i] = lua_tounsigned(L,-1);
lua_pop(L,1);
}
lc->msg = msg;
lc->sz = sz;
lc->group = group;
lua_pushlightuserdata(L,lc);
lua_pushinteger(L, sizeof(*lc));
return 2;
};
int
luaopen_mcast_c(lua_State *L) {
luaL_checkversion(L);
lua_pushcfunction(L, _pack_message);
return 1;
}