lua-skynet.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #define LUA_LIB
  2. #include "skynet.h"
  3. #include "lua-seri.h"
  4. #define KNRM "\x1B[0m"
  5. #define KRED "\x1B[31m"
  6. #include <lua.h>
  7. #include <lauxlib.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <inttypes.h>
  12. #include <time.h>
  13. #if defined(__APPLE__)
  14. #include <sys/time.h>
  15. #endif
  16. #include "skynet.h"
  17. // return nsec
  18. static int64_t
  19. get_time() {
  20. #if !defined(__APPLE__) || defined(AVAILABLE_MAC_OS_X_VERSION_10_12_AND_LATER)
  21. struct timespec ti;
  22. clock_gettime(CLOCK_MONOTONIC, &ti);
  23. return (int64_t)1000000000 * ti.tv_sec + ti.tv_nsec;
  24. #else
  25. struct timeval tv;
  26. gettimeofday(&tv, NULL);
  27. return (int64_t)1000000000 * tv.tv_sec + tv.tv_usec * 1000;
  28. #endif
  29. }
  30. static int
  31. traceback (lua_State *L) {
  32. const char *msg = lua_tostring(L, 1);
  33. if (msg)
  34. luaL_traceback(L, L, msg, 1);
  35. else {
  36. lua_pushliteral(L, "(no error message)");
  37. }
  38. return 1;
  39. }
  40. struct callback_context {
  41. lua_State *L;
  42. };
  43. static int
  44. _cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
  45. struct callback_context *cb_ctx = (struct callback_context *)ud;
  46. lua_State *L = cb_ctx->L;
  47. int trace = 1;
  48. int r;
  49. lua_pushvalue(L,2);
  50. lua_pushinteger(L, type);
  51. lua_pushlightuserdata(L, (void *)msg);
  52. lua_pushinteger(L,sz);
  53. lua_pushinteger(L, session);
  54. lua_pushinteger(L, source);
  55. r = lua_pcall(L, 5, 0 , trace);
  56. if (r == LUA_OK) {
  57. return 0;
  58. }
  59. const char * self = skynet_command(context, "REG", NULL);
  60. switch (r) {
  61. case LUA_ERRRUN:
  62. skynet_error(context, "lua call [%x to %s : %d msgsz = %d] error : " KRED "%s" KNRM, source , self, session, sz, lua_tostring(L,-1));
  63. break;
  64. case LUA_ERRMEM:
  65. skynet_error(context, "lua memory error : [%x to %s : %d]", source , self, session);
  66. break;
  67. case LUA_ERRERR:
  68. skynet_error(context, "lua error in error : [%x to %s : %d]", source , self, session);
  69. break;
  70. };
  71. lua_pop(L,1);
  72. return 0;
  73. }
  74. static int
  75. forward_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
  76. _cb(context, ud, type, session, source, msg, sz);
  77. // don't delete msg in forward mode.
  78. return 1;
  79. }
  80. static void
  81. clear_last_context(lua_State *L) {
  82. if (lua_getfield(L, LUA_REGISTRYINDEX, "callback_context") == LUA_TUSERDATA) {
  83. lua_pushnil(L);
  84. lua_setiuservalue(L, -2, 2);
  85. }
  86. lua_pop(L, 1);
  87. }
  88. static int
  89. _cb_pre(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
  90. struct callback_context *cb_ctx = (struct callback_context *)ud;
  91. clear_last_context(cb_ctx->L);
  92. skynet_callback(context, ud, _cb);
  93. return _cb(context, cb_ctx, type, session, source, msg, sz);
  94. }
  95. static int
  96. _forward_pre(struct skynet_context *context, void *ud, int type, int session, uint32_t source, const void *msg, size_t sz) {
  97. struct callback_context *cb_ctx = (struct callback_context *)ud;
  98. clear_last_context(cb_ctx->L);
  99. skynet_callback(context, ud, forward_cb);
  100. return forward_cb(context, cb_ctx, type, session, source, msg, sz);
  101. }
  102. static int
  103. lcallback(lua_State *L) {
  104. struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
  105. int forward = lua_toboolean(L, 2);
  106. luaL_checktype(L,1,LUA_TFUNCTION);
  107. lua_settop(L,1);
  108. struct callback_context * cb_ctx = (struct callback_context *)lua_newuserdatauv(L, sizeof(*cb_ctx), 2);
  109. cb_ctx->L = lua_newthread(L);
  110. lua_pushcfunction(cb_ctx->L, traceback);
  111. lua_setiuservalue(L, -2, 1);
  112. lua_getfield(L, LUA_REGISTRYINDEX, "callback_context");
  113. lua_setiuservalue(L, -2, 2);
  114. lua_setfield(L, LUA_REGISTRYINDEX, "callback_context");
  115. lua_xmove(L, cb_ctx->L, 1);
  116. skynet_callback(context, cb_ctx, (forward)?(_forward_pre):(_cb_pre));
  117. return 0;
  118. }
  119. static int
  120. lcommand(lua_State *L) {
  121. struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
  122. const char * cmd = luaL_checkstring(L,1);
  123. const char * result;
  124. const char * parm = NULL;
  125. if (lua_gettop(L) == 2) {
  126. parm = luaL_checkstring(L,2);
  127. }
  128. result = skynet_command(context, cmd, parm);
  129. if (result) {
  130. lua_pushstring(L, result);
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. static int
  136. laddresscommand(lua_State *L) {
  137. struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
  138. const char * cmd = luaL_checkstring(L,1);
  139. const char * result;
  140. const char * parm = NULL;
  141. if (lua_gettop(L) == 2) {
  142. parm = luaL_checkstring(L,2);
  143. }
  144. result = skynet_command(context, cmd, parm);
  145. if (result && result[0] == ':') {
  146. int i;
  147. uint32_t addr = 0;
  148. for (i=1;result[i];i++) {
  149. int c = result[i];
  150. if (c>='0' && c<='9') {
  151. c = c - '0';
  152. } else if (c>='a' && c<='f') {
  153. c = c - 'a' + 10;
  154. } else if (c>='A' && c<='F') {
  155. c = c - 'A' + 10;
  156. } else {
  157. return 0;
  158. }
  159. addr = addr * 16 + c;
  160. }
  161. lua_pushinteger(L, addr);
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. static int
  167. lintcommand(lua_State *L) {
  168. struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
  169. const char * cmd = luaL_checkstring(L,1);
  170. const char * result;
  171. const char * parm = NULL;
  172. char tmp[64]; // for integer parm
  173. if (lua_gettop(L) == 2) {
  174. if (lua_isnumber(L, 2)) {
  175. int32_t n = (int32_t)luaL_checkinteger(L,2);
  176. sprintf(tmp, "%d", n);
  177. parm = tmp;
  178. } else {
  179. parm = luaL_checkstring(L,2);
  180. }
  181. }
  182. result = skynet_command(context, cmd, parm);
  183. if (result) {
  184. char *endptr = NULL;
  185. lua_Integer r = strtoll(result, &endptr, 0);
  186. if (endptr == NULL || *endptr != '\0') {
  187. // may be real number
  188. double n = strtod(result, &endptr);
  189. if (endptr == NULL || *endptr != '\0') {
  190. return luaL_error(L, "Invalid result %s", result);
  191. } else {
  192. lua_pushnumber(L, n);
  193. }
  194. } else {
  195. lua_pushinteger(L, r);
  196. }
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. static int
  202. lgenid(lua_State *L) {
  203. struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
  204. int session = skynet_send(context, 0, 0, PTYPE_TAG_ALLOCSESSION , 0 , NULL, 0);
  205. lua_pushinteger(L, session);
  206. return 1;
  207. }
  208. static const char *
  209. get_dest_string(lua_State *L, int index) {
  210. const char * dest_string = lua_tostring(L, index);
  211. if (dest_string == NULL) {
  212. luaL_error(L, "dest address type (%s) must be a string or number.", lua_typename(L, lua_type(L,index)));
  213. }
  214. return dest_string;
  215. }
  216. static int
  217. send_message(lua_State *L, int source, int idx_type) {
  218. struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
  219. uint32_t dest = (uint32_t)lua_tointeger(L, 1);
  220. const char * dest_string = NULL;
  221. if (dest == 0) {
  222. if (lua_type(L,1) == LUA_TNUMBER) {
  223. return luaL_error(L, "Invalid service address 0");
  224. }
  225. dest_string = get_dest_string(L, 1);
  226. }
  227. int type = luaL_checkinteger(L, idx_type+0);
  228. int session = 0;
  229. if (lua_isnil(L,idx_type+1)) {
  230. type |= PTYPE_TAG_ALLOCSESSION;
  231. } else {
  232. session = luaL_checkinteger(L,idx_type+1);
  233. }
  234. int mtype = lua_type(L,idx_type+2);
  235. switch (mtype) {
  236. case LUA_TSTRING: {
  237. size_t len = 0;
  238. void * msg = (void *)lua_tolstring(L,idx_type+2,&len);
  239. if (len == 0) {
  240. msg = NULL;
  241. }
  242. if (dest_string) {
  243. session = skynet_sendname(context, source, dest_string, type, session , msg, len);
  244. } else {
  245. session = skynet_send(context, source, dest, type, session , msg, len);
  246. }
  247. break;
  248. }
  249. case LUA_TLIGHTUSERDATA: {
  250. void * msg = lua_touserdata(L,idx_type+2);
  251. int size = luaL_checkinteger(L,idx_type+3);
  252. if (dest_string) {
  253. session = skynet_sendname(context, source, dest_string, type | PTYPE_TAG_DONTCOPY, session, msg, size);
  254. } else {
  255. session = skynet_send(context, source, dest, type | PTYPE_TAG_DONTCOPY, session, msg, size);
  256. }
  257. break;
  258. }
  259. default:
  260. luaL_error(L, "invalid param %s", lua_typename(L, lua_type(L,idx_type+2)));
  261. }
  262. if (session < 0) {
  263. if (session == -2) {
  264. // package is too large
  265. lua_pushboolean(L, 0);
  266. return 1;
  267. }
  268. // send to invalid address
  269. // todo: maybe throw an error would be better
  270. return 0;
  271. }
  272. lua_pushinteger(L,session);
  273. return 1;
  274. }
  275. /*
  276. uint32 address
  277. string address
  278. integer type
  279. integer session
  280. string message
  281. lightuserdata message_ptr
  282. integer len
  283. */
  284. static int
  285. lsend(lua_State *L) {
  286. return send_message(L, 0, 2);
  287. }
  288. /*
  289. uint32 address
  290. string address
  291. integer source_address
  292. integer type
  293. integer session
  294. string message
  295. lightuserdata message_ptr
  296. integer len
  297. */
  298. static int
  299. lredirect(lua_State *L) {
  300. uint32_t source = (uint32_t)luaL_checkinteger(L,2);
  301. return send_message(L, source, 3);
  302. }
  303. static int
  304. lerror(lua_State *L) {
  305. struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
  306. int n = lua_gettop(L);
  307. if (n <= 1) {
  308. lua_settop(L, 1);
  309. const char * s = luaL_tolstring(L, 1, NULL);
  310. skynet_error(context, "%s", s);
  311. return 0;
  312. }
  313. luaL_Buffer b;
  314. luaL_buffinit(L, &b);
  315. int i;
  316. for (i=1; i<=n; i++) {
  317. luaL_tolstring(L, i, NULL);
  318. luaL_addvalue(&b);
  319. if (i<n) {
  320. luaL_addchar(&b, ' ');
  321. }
  322. }
  323. luaL_pushresult(&b);
  324. skynet_error(context, "%s", lua_tostring(L, -1));
  325. return 0;
  326. }
  327. static int
  328. ltostring(lua_State *L) {
  329. if (lua_isnoneornil(L,1)) {
  330. return 0;
  331. }
  332. char * msg = lua_touserdata(L,1);
  333. int sz = luaL_checkinteger(L,2);
  334. lua_pushlstring(L,msg,sz);
  335. return 1;
  336. }
  337. static int
  338. lharbor(lua_State *L) {
  339. struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
  340. uint32_t handle = (uint32_t)luaL_checkinteger(L,1);
  341. int harbor = 0;
  342. int remote = skynet_isremote(context, handle, &harbor);
  343. lua_pushinteger(L,harbor);
  344. lua_pushboolean(L, remote);
  345. return 2;
  346. }
  347. static int
  348. lpackstring(lua_State *L) {
  349. luaseri_pack(L);
  350. char * str = (char *)lua_touserdata(L, -2);
  351. int sz = lua_tointeger(L, -1);
  352. lua_pushlstring(L, str, sz);
  353. skynet_free(str);
  354. return 1;
  355. }
  356. static int
  357. ltrash(lua_State *L) {
  358. int t = lua_type(L,1);
  359. switch (t) {
  360. case LUA_TSTRING: {
  361. break;
  362. }
  363. case LUA_TLIGHTUSERDATA: {
  364. void * msg = lua_touserdata(L,1);
  365. luaL_checkinteger(L,2);
  366. skynet_free(msg);
  367. break;
  368. }
  369. default:
  370. luaL_error(L, "skynet.trash invalid param %s", lua_typename(L,t));
  371. }
  372. return 0;
  373. }
  374. static int
  375. lnow(lua_State *L) {
  376. uint64_t ti = skynet_now();
  377. lua_pushinteger(L, ti);
  378. return 1;
  379. }
  380. static int
  381. lhpc(lua_State *L) {
  382. lua_pushinteger(L, get_time());
  383. return 1;
  384. }
  385. #define MAX_LEVEL 3
  386. struct source_info {
  387. const char * source;
  388. int line;
  389. };
  390. /*
  391. string tag
  392. string userstring
  393. thread co (default nil/current L)
  394. integer level (default nil)
  395. */
  396. static int
  397. ltrace(lua_State *L) {
  398. struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
  399. const char * tag = luaL_checkstring(L, 1);
  400. const char * user = luaL_checkstring(L, 2);
  401. if (!lua_isnoneornil(L, 3)) {
  402. lua_State * co = L;
  403. int level;
  404. if (lua_isthread(L, 3)) {
  405. co = lua_tothread (L, 3);
  406. level = luaL_optinteger(L, 4, 1);
  407. } else {
  408. level = luaL_optinteger(L, 3, 1);
  409. }
  410. struct source_info si[MAX_LEVEL];
  411. lua_Debug d;
  412. int index = 0;
  413. do {
  414. if (!lua_getstack(co, level, &d))
  415. break;
  416. lua_getinfo(co, "Sl", &d);
  417. level++;
  418. si[index].source = d.source;
  419. si[index].line = d.currentline;
  420. if (d.currentline >= 0)
  421. ++index;
  422. } while (index < MAX_LEVEL);
  423. switch (index) {
  424. case 1:
  425. skynet_error(context, "<TRACE %s> %" PRId64 " %s : %s:%d", tag, get_time(), user, si[0].source, si[0].line);
  426. break;
  427. case 2:
  428. skynet_error(context, "<TRACE %s> %" PRId64 " %s : %s:%d %s:%d", tag, get_time(), user,
  429. si[0].source, si[0].line,
  430. si[1].source, si[1].line
  431. );
  432. break;
  433. case 3:
  434. skynet_error(context, "<TRACE %s> %" PRId64 " %s : %s:%d %s:%d %s:%d", tag, get_time(), user,
  435. si[0].source, si[0].line,
  436. si[1].source, si[1].line,
  437. si[2].source, si[2].line
  438. );
  439. break;
  440. default:
  441. skynet_error(context, "<TRACE %s> %" PRId64 " %s", tag, get_time(), user);
  442. break;
  443. }
  444. return 0;
  445. }
  446. skynet_error(context, "<TRACE %s> %" PRId64 " %s", tag, get_time(), user);
  447. return 0;
  448. }
  449. LUAMOD_API int
  450. luaopen_skynet_core(lua_State *L) {
  451. luaL_checkversion(L);
  452. luaL_Reg l[] = {
  453. { "send" , lsend },
  454. { "genid", lgenid },
  455. { "redirect", lredirect },
  456. { "command" , lcommand },
  457. { "intcommand", lintcommand },
  458. { "addresscommand", laddresscommand },
  459. { "error", lerror },
  460. { "harbor", lharbor },
  461. { "callback", lcallback },
  462. { "trace", ltrace },
  463. { NULL, NULL },
  464. };
  465. // functions without skynet_context
  466. luaL_Reg l2[] = {
  467. { "tostring", ltostring },
  468. { "pack", luaseri_pack },
  469. { "unpack", luaseri_unpack },
  470. { "packstring", lpackstring },
  471. { "trash" , ltrash },
  472. { "now", lnow },
  473. { "hpc", lhpc }, // getHPCounter
  474. { NULL, NULL },
  475. };
  476. lua_createtable(L, 0, sizeof(l)/sizeof(l[0]) + sizeof(l2)/sizeof(l2[0]) -2);
  477. lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context");
  478. struct skynet_context *ctx = lua_touserdata(L,-1);
  479. if (ctx == NULL) {
  480. return luaL_error(L, "Init skynet context first");
  481. }
  482. luaL_setfuncs(L,l,1);
  483. luaL_setfuncs(L,l2,0);
  484. return 1;
  485. }