mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 03:53:09 +00:00
multicast support remote publish/subscribe
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <lua.h>
|
||||
#include <lauxlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
struct mc_package {
|
||||
int reference;
|
||||
@@ -10,6 +11,19 @@ struct mc_package {
|
||||
void *data;
|
||||
};
|
||||
|
||||
static int
|
||||
pack(lua_State *L, void *data, size_t size) {
|
||||
struct mc_package * pack = skynet_malloc(sizeof(struct mc_package));
|
||||
pack->reference = 0;
|
||||
pack->size = (uint32_t)size;
|
||||
pack->data = data;
|
||||
struct mc_package ** ret = skynet_malloc(sizeof(*ret));
|
||||
*ret = pack;
|
||||
lua_pushlightuserdata(L, ret);
|
||||
lua_pushinteger(L, sizeof(ret));
|
||||
return 2;
|
||||
}
|
||||
|
||||
/*
|
||||
lightuserdata
|
||||
integer size
|
||||
@@ -23,15 +37,37 @@ mc_packlocal(lua_State *L) {
|
||||
if (size != (uint32_t)size) {
|
||||
return luaL_error(L, "Size should be 32bit integer");
|
||||
}
|
||||
struct mc_package * pack = skynet_malloc(sizeof(struct mc_package));
|
||||
pack->reference = 0;
|
||||
pack->size = (uint32_t)size;
|
||||
pack->data = data;
|
||||
struct mc_package ** ret = skynet_malloc(sizeof(*ret));
|
||||
*ret = pack;
|
||||
lua_pushlightuserdata(L, ret);
|
||||
lua_pushinteger(L, sizeof(ret));
|
||||
return 2;
|
||||
return pack(L, data, size);
|
||||
}
|
||||
|
||||
/*
|
||||
lightuserdata
|
||||
integer size
|
||||
|
||||
return lightuserdata, sizeof(struct mc_package *)
|
||||
*/
|
||||
static int
|
||||
mc_packremote(lua_State *L) {
|
||||
void * data = lua_touserdata(L, 1);
|
||||
size_t size = luaL_checkunsigned(L, 2);
|
||||
if (size != (uint32_t)size) {
|
||||
return luaL_error(L, "Size should be 32bit integer");
|
||||
}
|
||||
void * msg = skynet_malloc(size);
|
||||
memcpy(msg, data, size);
|
||||
return pack(L, msg, size);
|
||||
}
|
||||
|
||||
static int
|
||||
mc_packstring(lua_State *L) {
|
||||
size_t size;
|
||||
const char * msg = luaL_checklstring(L, 1, &size);
|
||||
if (size != (uint32_t)size) {
|
||||
return luaL_error(L, "string is too long");
|
||||
}
|
||||
void * data = skynet_malloc(size);
|
||||
memcpy(data, msg, size);
|
||||
return pack(L, data, size);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -89,6 +125,20 @@ mc_closelocal(lua_State *L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
lightuserdata struct mc_package **
|
||||
return lightuserdata/size
|
||||
*/
|
||||
static int
|
||||
mc_remote(lua_State *L) {
|
||||
struct mc_package **ptr = lua_touserdata(L,1);
|
||||
struct mc_package *pack = *ptr;
|
||||
lua_pushlightuserdata(L, pack->data);
|
||||
lua_pushunsigned(L, pack->size);
|
||||
skynet_free(pack);
|
||||
return 2;
|
||||
}
|
||||
|
||||
int
|
||||
luaopen_multicast_c(lua_State *L) {
|
||||
luaL_Reg l[] = {
|
||||
@@ -96,6 +146,9 @@ luaopen_multicast_c(lua_State *L) {
|
||||
{ "unpack", mc_unpacklocal },
|
||||
{ "bind", mc_bindrefer },
|
||||
{ "close", mc_closelocal },
|
||||
{ "remote", mc_remote },
|
||||
{ "packstring", mc_packstring },
|
||||
{ "packremote", mc_packremote },
|
||||
{ NULL, NULL },
|
||||
};
|
||||
luaL_checkversion(L);
|
||||
|
||||
Reference in New Issue
Block a user