From f3647338ce1722a62196a8c2dca0ab5566689602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=91=E9=A3=8E?= Date: Fri, 12 Oct 2012 14:05:22 +0800 Subject: [PATCH] localcast message module --- lualib-src/lua-localcast.c | 60 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lualib-src/lua-localcast.c diff --git a/lualib-src/lua-localcast.c b/lualib-src/lua-localcast.c new file mode 100644 index 000000000..b0a546ba6 --- /dev/null +++ b/lualib-src/lua-localcast.c @@ -0,0 +1,60 @@ +#include +#include +#include "luacompat52.h" +#include "localcast.h" + +#include +#include +#include + +/* + 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;in;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; +}