forked from cloudwu/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua-multicast.c
161 lines (139 loc) · 3.25 KB
/
lua-multicast.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
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
#include "skynet.h"
#include <lua.h>
#include <lauxlib.h>
#include <stdint.h>
#include <string.h>
#include "atomic.h"
struct mc_package {
int reference;
uint32_t size;
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
return lightuserdata, sizeof(struct mc_package *)
*/
static int
mc_packlocal(lua_State *L) {
void * data = lua_touserdata(L, 1);
size_t size = (size_t)luaL_checkinteger(L, 2);
if (size != (uint32_t)size) {
return luaL_error(L, "Size should be 32bit integer");
}
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 = (size_t)luaL_checkinteger(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);
}
/*
lightuserdata struct mc_package **
integer size (must be sizeof(struct mc_package *)
return package, lightuserdata, size
*/
static int
mc_unpacklocal(lua_State *L) {
struct mc_package ** pack = lua_touserdata(L,1);
int sz = luaL_checkinteger(L,2);
if (sz != sizeof(pack)) {
return luaL_error(L, "Invalid multicast package size %d", sz);
}
lua_pushlightuserdata(L, *pack);
lua_pushlightuserdata(L, (*pack)->data);
lua_pushinteger(L, (lua_Integer)((*pack)->size));
return 3;
}
/*
lightuserdata struct mc_package **
integer reference
return mc_package *
*/
static int
mc_bindrefer(lua_State *L) {
struct mc_package ** pack = lua_touserdata(L,1);
int ref = luaL_checkinteger(L,2);
if ((*pack)->reference != 0) {
return luaL_error(L, "Can't bind a multicast package more than once");
}
(*pack)->reference = ref;
lua_pushlightuserdata(L, *pack);
skynet_free(pack);
return 1;
}
/*
lightuserdata struct mc_package *
*/
static int
mc_closelocal(lua_State *L) {
struct mc_package *pack = lua_touserdata(L,1);
int ref = ATOM_DEC(&pack->reference);
if (ref <= 0) {
skynet_free(pack->data);
skynet_free(pack);
if (ref < 0) {
return luaL_error(L, "Invalid multicast package reference %d", ref);
}
}
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_pushinteger(L, (lua_Integer)(pack->size));
skynet_free(pack);
return 2;
}
static int
mc_nextid(lua_State *L) {
uint32_t id = (uint32_t)luaL_checkinteger(L, 1);
id += 256;
lua_pushinteger(L, (uint32_t)id);
return 1;
}
int
luaopen_multicast_core(lua_State *L) {
luaL_Reg l[] = {
{ "pack", mc_packlocal },
{ "unpack", mc_unpacklocal },
{ "bind", mc_bindrefer },
{ "close", mc_closelocal },
{ "remote", mc_remote },
{ "packremote", mc_packremote },
{ "nextid", mc_nextid },
{ NULL, NULL },
};
luaL_checkversion(L);
luaL_newlib(L,l);
return 1;
}