1 int
 2 luaopen_netpack(lua_State *L) {
 3     luaL_checkversion(L);
 4     luaL_Reg l[] = {
 5         { "pop", lpop },//从队列头弹出一个netpack,netpack没有被释放
 6         { "pack", lpack },//将string打包成2个字节长度+内容
 7         { "clear", lclear },//释放queue
 8         { "tostring", ltostring },//将userdata转成string
 9         { NULL, NULL },
10     };
11     luaL_newlib(L,l);
12 
13     // the order is same with macros : TYPE_* (defined top)
14     lua_pushliteral(L, "data");//将对应的标签放进栈
15     lua_pushliteral(L, "more");
16     lua_pushliteral(L, "error");
17     lua_pushliteral(L, "open");
18     lua_pushliteral(L, "close");
19     lua_pushliteral(L, "warning");
20 
21     lua_pushcclosure(L, lfilter, 6);
22     lua_setfield(L, -2, "filter");//根据buf的type,解析参数
23 
24     return 1;
25 }
View Code

相关文章: