net.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. // Module for network
  2. //#include "lua.h"
  3. #include "lualib.h"
  4. #include "lauxlib.h"
  5. #include "platform.h"
  6. #include "auxmods.h"
  7. #include "lrotable.h"
  8. #include "c_string.h"
  9. #include "c_stdlib.h"
  10. #include "c_types.h"
  11. #include "mem.h"
  12. #include "espconn.h"
  13. #ifdef CLIENT_SSL_ENABLE
  14. unsigned char *default_certificate;
  15. unsigned int default_certificate_len = 0;
  16. unsigned char *default_private_key;
  17. unsigned int default_private_key_len = 0;
  18. #endif
  19. #define TCP ESPCONN_TCP
  20. #define UDP ESPCONN_UDP
  21. static ip_addr_t host_ip; // for dns
  22. #if 0
  23. static int expose_array(lua_State* L, char *array, unsigned short len);
  24. #endif
  25. #define MAX_SOCKET 5
  26. static int socket_num = 0;
  27. static int socket[MAX_SOCKET];
  28. static lua_State *gL = NULL;
  29. static int tcpserver_cb_connect_ref = LUA_NOREF; // for tcp server connected callback
  30. static uint16_t tcp_server_timeover = 30;
  31. static struct espconn *pTcpServer = NULL;
  32. static struct espconn *pUdpServer = NULL;
  33. typedef struct lnet_userdata
  34. {
  35. struct espconn *pesp_conn;
  36. int self_ref;
  37. int cb_connect_ref;
  38. int cb_reconnect_ref;
  39. int cb_disconnect_ref;
  40. int cb_receive_ref;
  41. int cb_send_ref;
  42. int cb_dns_found_ref;
  43. #ifdef CLIENT_SSL_ENABLE
  44. uint8_t secure;
  45. #endif
  46. }lnet_userdata;
  47. static void net_server_disconnected(void *arg) // for tcp server only
  48. {
  49. NODE_DBG("net_server_disconnected is called.\n");
  50. struct espconn *pesp_conn = arg;
  51. if(pesp_conn == NULL)
  52. return;
  53. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  54. if(nud == NULL)
  55. return;
  56. if(gL == NULL)
  57. return;
  58. #if 0
  59. char temp[20] = {0};
  60. c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
  61. NODE_DBG("remote ");
  62. NODE_DBG(temp);
  63. NODE_DBG(":");
  64. NODE_DBG("%d",pesp_conn->proto.tcp->remote_port);
  65. NODE_DBG(" disconnected.\n");
  66. #endif
  67. if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
  68. {
  69. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
  70. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
  71. lua_call(gL, 1, 0);
  72. }
  73. int i;
  74. lua_gc(gL, LUA_GCSTOP, 0);
  75. for(i=0;i<MAX_SOCKET;i++){
  76. if( (LUA_NOREF!=socket[i]) && (socket[i] == nud->self_ref) ){
  77. // found the saved client
  78. nud->pesp_conn->reverse = NULL;
  79. nud->pesp_conn = NULL; // the espconn is made by low level sdk, do not need to free, delete() will not free it.
  80. nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
  81. luaL_unref(gL, LUA_REGISTRYINDEX, socket[i]);
  82. socket[i] = LUA_NOREF;
  83. socket_num--;
  84. break;
  85. }
  86. }
  87. lua_gc(gL, LUA_GCRESTART, 0);
  88. }
  89. static void net_socket_disconnected(void *arg) // tcp only
  90. {
  91. NODE_DBG("net_socket_disconnected is called.\n");
  92. struct espconn *pesp_conn = arg;
  93. if(pesp_conn == NULL)
  94. return;
  95. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  96. if(nud == NULL)
  97. return;
  98. if(nud->cb_disconnect_ref != LUA_NOREF && nud->self_ref != LUA_NOREF)
  99. {
  100. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
  101. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
  102. lua_call(gL, 1, 0);
  103. }
  104. if(pesp_conn->proto.tcp)
  105. c_free(pesp_conn->proto.tcp);
  106. pesp_conn->proto.tcp = NULL;
  107. if(nud->pesp_conn)
  108. c_free(nud->pesp_conn);
  109. nud->pesp_conn = NULL; // espconn is already disconnected
  110. lua_gc(gL, LUA_GCSTOP, 0);
  111. if(nud->self_ref != LUA_NOREF){
  112. luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref);
  113. nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
  114. }
  115. lua_gc(gL, LUA_GCRESTART, 0);
  116. }
  117. static void net_server_reconnected(void *arg, sint8_t err)
  118. {
  119. NODE_DBG("net_server_reconnected is called.\n");
  120. net_server_disconnected(arg);
  121. }
  122. static void net_socket_reconnected(void *arg, sint8_t err)
  123. {
  124. NODE_DBG("net_socket_reconnected is called.\n");
  125. net_socket_disconnected(arg);
  126. }
  127. static void net_socket_received(void *arg, char *pdata, unsigned short len)
  128. {
  129. NODE_DBG("net_socket_received is called.\n");
  130. struct espconn *pesp_conn = arg;
  131. if(pesp_conn == NULL)
  132. return;
  133. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  134. if(nud == NULL)
  135. return;
  136. if(nud->cb_receive_ref == LUA_NOREF)
  137. return;
  138. if(nud->self_ref == LUA_NOREF)
  139. return;
  140. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_receive_ref);
  141. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
  142. // expose_array(gL, pdata, len);
  143. // *(pdata+len) = 0;
  144. // NODE_DBG(pdata);
  145. // NODE_DBG("\n");
  146. lua_pushlstring(gL, pdata, len);
  147. // lua_pushinteger(gL, len);
  148. lua_call(gL, 2, 0);
  149. }
  150. static void net_socket_sent(void *arg)
  151. {
  152. // NODE_DBG("net_socket_sent is called.\n");
  153. struct espconn *pesp_conn = arg;
  154. if(pesp_conn == NULL)
  155. return;
  156. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  157. if(nud == NULL)
  158. return;
  159. if(nud->cb_send_ref == LUA_NOREF)
  160. return;
  161. if(nud->self_ref == LUA_NOREF)
  162. return;
  163. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_send_ref);
  164. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(server) to callback func in lua
  165. lua_call(gL, 1, 0);
  166. }
  167. static void net_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  168. {
  169. NODE_DBG("net_dns_found is called.\n");
  170. struct espconn *pesp_conn = arg;
  171. if(pesp_conn == NULL){
  172. NODE_DBG("pesp_conn null.\n");
  173. return;
  174. }
  175. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  176. if(nud == NULL){
  177. NODE_DBG("nud null.\n");
  178. return;
  179. }
  180. if(nud->cb_dns_found_ref == LUA_NOREF){
  181. NODE_DBG("cb_dns_found_ref null.\n");
  182. return;
  183. }
  184. if(ipaddr == NULL)
  185. {
  186. NODE_ERR( "DNS Fail!\n" );
  187. return;
  188. }
  189. // ipaddr->addr is a uint32_t ip
  190. char ip_str[20];
  191. c_memset(ip_str, 0, sizeof(ip_str));
  192. if(host_ip.addr == 0 && ipaddr->addr != 0)
  193. {
  194. c_sprintf(ip_str, IPSTR, IP2STR(&(ipaddr->addr)));
  195. }
  196. if(nud->self_ref == LUA_NOREF){
  197. NODE_DBG("self_ref null.\n");
  198. return;
  199. }
  200. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_dns_found_ref); // the callback function
  201. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(conn) to callback func in lua
  202. lua_pushstring(gL, ip_str); // the ip para
  203. lua_call(gL, 2, 0);
  204. if((pesp_conn->type == ESPCONN_TCP && pesp_conn->proto.tcp->remote_port == 0)
  205. || (pesp_conn->type == ESPCONN_UDP && pesp_conn->proto.udp->remote_port == 0) ){
  206. lua_gc(gL, LUA_GCSTOP, 0);
  207. if(nud->self_ref != LUA_NOREF){
  208. luaL_unref(gL, LUA_REGISTRYINDEX, nud->self_ref);
  209. nud->self_ref = LUA_NOREF; // unref this, and the net.socket userdata will delete it self
  210. }
  211. lua_gc(gL, LUA_GCRESTART, 0);
  212. }
  213. }
  214. static void net_server_connected(void *arg) // for tcp only
  215. {
  216. NODE_DBG("net_server_connected is called.\n");
  217. struct espconn *pesp_conn = arg;
  218. int i = 0;
  219. lnet_userdata *skt = NULL;
  220. if(pesp_conn == NULL)
  221. return;
  222. #if 0
  223. char temp[20] = {0};
  224. c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
  225. NODE_DBG("remote ");
  226. NODE_DBG(temp);
  227. NODE_DBG(":");
  228. NODE_DBG("%d",pesp_conn->proto.tcp->remote_port);
  229. NODE_DBG(" connected.\n");
  230. #endif
  231. for(i=0;i<MAX_SOCKET;i++){
  232. if(socket[i] == LUA_NOREF) // found empty slot
  233. {
  234. break;
  235. }
  236. }
  237. if(i>=MAX_SOCKET) // can't create more socket
  238. {
  239. NODE_ERR("MAX_SOCKET\n");
  240. pesp_conn->reverse = NULL; // not accept this conn
  241. if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port)
  242. espconn_disconnect(pesp_conn);
  243. return;
  244. }
  245. if(tcpserver_cb_connect_ref == LUA_NOREF)
  246. return;
  247. if(!gL)
  248. return;
  249. lua_rawgeti(gL, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref); // get function
  250. // create a new client object
  251. skt = (lnet_userdata *)lua_newuserdata(gL, sizeof(lnet_userdata));
  252. if(!skt){
  253. NODE_ERR("can't newudata\n");
  254. lua_pop(gL, 1);
  255. return;
  256. }
  257. // set its metatable
  258. luaL_getmetatable(gL, "net.socket");
  259. lua_setmetatable(gL, -2);
  260. // pre-initialize it, in case of errors
  261. skt->self_ref = LUA_NOREF;
  262. lua_pushvalue(gL, -1); // copy the top of stack
  263. skt->self_ref = luaL_ref(gL, LUA_REGISTRYINDEX); // ref to it self, for module api to find the userdata
  264. socket[i] = skt->self_ref; // save to socket array
  265. socket_num++;
  266. skt->cb_connect_ref = LUA_NOREF; // this socket already connected
  267. skt->cb_reconnect_ref = LUA_NOREF;
  268. skt->cb_disconnect_ref = LUA_NOREF;
  269. skt->cb_receive_ref = LUA_NOREF;
  270. skt->cb_send_ref = LUA_NOREF;
  271. skt->cb_dns_found_ref = LUA_NOREF;
  272. #ifdef CLIENT_SSL_ENABLE
  273. skt->secure = 0; // as a server SSL is not supported.
  274. #endif
  275. skt->pesp_conn = pesp_conn; // point to the espconn made by low level sdk
  276. pesp_conn->reverse = skt; // let espcon carray the info of this userdata(net.socket)
  277. espconn_regist_recvcb(pesp_conn, net_socket_received);
  278. espconn_regist_sentcb(pesp_conn, net_socket_sent);
  279. espconn_regist_disconcb(pesp_conn, net_server_disconnected);
  280. espconn_regist_reconcb(pesp_conn, net_server_reconnected);
  281. // now socket[i] has the client ref, and stack top has the userdata
  282. lua_call(gL, 1, 0); // function(conn)
  283. }
  284. static void net_socket_connected(void *arg)
  285. {
  286. NODE_DBG("net_socket_connected is called.\n");
  287. struct espconn *pesp_conn = arg;
  288. if(pesp_conn == NULL)
  289. return;
  290. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  291. if(nud == NULL)
  292. return;
  293. // can receive and send data, even if there is no connected callback in lua.
  294. espconn_regist_recvcb(pesp_conn, net_socket_received);
  295. espconn_regist_sentcb(pesp_conn, net_socket_sent);
  296. espconn_regist_disconcb(pesp_conn, net_socket_disconnected);
  297. if(nud->cb_connect_ref == LUA_NOREF)
  298. return;
  299. if(nud->self_ref == LUA_NOREF)
  300. return;
  301. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->cb_connect_ref);
  302. lua_rawgeti(gL, LUA_REGISTRYINDEX, nud->self_ref); // pass the userdata(client) to callback func in lua
  303. lua_call(gL, 1, 0);
  304. }
  305. // Lua: s = net.create(type, secure/timeout, function(conn))
  306. static int net_create( lua_State* L, const char* mt )
  307. {
  308. NODE_DBG("net_create is called.\n");
  309. struct espconn *pesp_conn = NULL;
  310. lnet_userdata *nud, *temp = NULL;
  311. unsigned type;
  312. #ifdef CLIENT_SSL_ENABLE
  313. unsigned secure = 0;
  314. #endif
  315. uint8_t stack = 1;
  316. bool isserver = false;
  317. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  318. isserver = true;
  319. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  320. isserver = false;
  321. else
  322. {
  323. NODE_DBG("wrong metatable for net_create.\n");
  324. return 0;
  325. }
  326. type = luaL_checkinteger( L, stack );
  327. if ( type != ESPCONN_TCP && type != ESPCONN_UDP )
  328. return luaL_error( L, "wrong arg type" );
  329. stack++;
  330. #ifdef CLIENT_SSL_ENABLE
  331. if(!isserver){
  332. if ( lua_isnumber(L, stack) )
  333. {
  334. secure = lua_tointeger(L, stack);
  335. stack++;
  336. if ( secure != 0 && secure != 1 ){
  337. return luaL_error( L, "wrong arg type" );
  338. }
  339. } else {
  340. secure = 0; // default to 0
  341. }
  342. }
  343. #endif
  344. if(isserver && type == ESPCONN_TCP){
  345. if ( lua_isnumber(L, stack) )
  346. {
  347. unsigned to = lua_tointeger(L, stack);
  348. stack++;
  349. if ( to < 1 || to > 28800 ){
  350. return luaL_error( L, "wrong arg type" );
  351. }
  352. tcp_server_timeover = (uint16_t)to;
  353. } else {
  354. tcp_server_timeover = 30; // default to 30
  355. }
  356. }
  357. // create a object
  358. nud = (lnet_userdata *)lua_newuserdata(L, sizeof(lnet_userdata));
  359. // pre-initialize it, in case of errors
  360. nud->self_ref = LUA_NOREF;
  361. nud->cb_connect_ref = LUA_NOREF;
  362. nud->cb_reconnect_ref = LUA_NOREF;
  363. nud->cb_disconnect_ref = LUA_NOREF;
  364. nud->cb_receive_ref = LUA_NOREF;
  365. nud->cb_send_ref = LUA_NOREF;
  366. nud->cb_dns_found_ref = LUA_NOREF;
  367. nud->pesp_conn = NULL;
  368. #ifdef CLIENT_SSL_ENABLE
  369. nud->secure = secure;
  370. #endif
  371. // set its metatable
  372. luaL_getmetatable(L, mt);
  373. lua_setmetatable(L, -2);
  374. // create the espconn struct
  375. if(isserver && type==ESPCONN_TCP && pTcpServer){
  376. if(tcpserver_cb_connect_ref != LUA_NOREF){ // self_ref should be unref in close()
  377. lua_pop(L,1);
  378. return luaL_error(L, "only one tcp server allowed");
  379. }
  380. pesp_conn = nud->pesp_conn = pTcpServer;
  381. } else if(isserver && type==ESPCONN_UDP && pUdpServer){
  382. temp = (lnet_userdata *)pUdpServer->reverse;
  383. if(temp && temp->self_ref != LUA_NOREF){
  384. lua_pop(L,1);
  385. return luaL_error(L, "only one udp server allowed");
  386. }
  387. pesp_conn = nud->pesp_conn = pUdpServer;
  388. } else {
  389. pesp_conn = nud->pesp_conn = (struct espconn *)c_zalloc(sizeof(struct espconn));
  390. if(!pesp_conn)
  391. return luaL_error(L, "not enough memory");
  392. pesp_conn->proto.tcp = NULL;
  393. pesp_conn->proto.udp = NULL;
  394. pesp_conn->reverse = NULL;
  395. if( type==ESPCONN_TCP )
  396. {
  397. pesp_conn->proto.tcp = (esp_tcp *)c_zalloc(sizeof(esp_tcp));
  398. if(!pesp_conn->proto.tcp){
  399. c_free(pesp_conn);
  400. pesp_conn = nud->pesp_conn = NULL;
  401. return luaL_error(L, "not enough memory");
  402. }
  403. NODE_DBG("TCP server/socket is set.\n");
  404. }
  405. else if( type==ESPCONN_UDP )
  406. {
  407. pesp_conn->proto.udp = (esp_udp *)c_zalloc(sizeof(esp_udp));
  408. if(!pesp_conn->proto.udp){
  409. c_free(pesp_conn);
  410. pesp_conn = nud->pesp_conn = NULL;
  411. return luaL_error(L, "not enough memory");
  412. }
  413. NODE_DBG("UDP server/socket is set.\n");
  414. }
  415. }
  416. pesp_conn->type = type;
  417. pesp_conn->state = ESPCONN_NONE;
  418. // reverse is for the callback function
  419. pesp_conn->reverse = nud;
  420. if(isserver && type==ESPCONN_TCP && pTcpServer==NULL){
  421. pTcpServer = pesp_conn;
  422. } else if(isserver && type==ESPCONN_UDP && pUdpServer==NULL){
  423. pUdpServer = pesp_conn;
  424. }
  425. gL = L; // global L for net module.
  426. // if call back function is specified, call it with para userdata
  427. // luaL_checkanyfunction(L, 2);
  428. if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
  429. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  430. lua_pushvalue(L, -2); // copy the self_ref(userdata) to the top
  431. lua_call(L, 1, 0);
  432. }
  433. return 1;
  434. }
  435. // static int net_close( lua_State* L, const char* mt );
  436. // Lua: net.delete( socket/server )
  437. // call close() first
  438. // server: disconnect server, unref everything
  439. // socket: unref everything
  440. static int net_delete( lua_State* L, const char* mt )
  441. {
  442. NODE_DBG("net_delete is called.\n");
  443. bool isserver = false;
  444. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  445. isserver = true;
  446. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  447. isserver = false;
  448. else
  449. {
  450. NODE_DBG("wrong metatable for net_delete.\n");
  451. return 0;
  452. }
  453. // net_close( L, mt ); // close it first
  454. lnet_userdata *nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  455. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  456. if(nud==NULL){
  457. NODE_DBG("userdata is nil.\n");
  458. return 0;
  459. }
  460. if(nud->pesp_conn){ // for client connected to tcp server, this should set NULL in disconnect cb
  461. nud->pesp_conn->reverse = NULL;
  462. if(!isserver) // socket is freed here
  463. {
  464. if(nud->pesp_conn->type == ESPCONN_UDP){
  465. if(nud->pesp_conn->proto.udp)
  466. c_free(nud->pesp_conn->proto.udp);
  467. nud->pesp_conn->proto.udp = NULL;
  468. } else if (nud->pesp_conn->type == ESPCONN_TCP) {
  469. if(nud->pesp_conn->proto.tcp)
  470. c_free(nud->pesp_conn->proto.tcp);
  471. nud->pesp_conn->proto.tcp = NULL;
  472. }
  473. c_free(nud->pesp_conn);
  474. }
  475. nud->pesp_conn = NULL; // for socket, it will free this when disconnected
  476. }
  477. // free (unref) callback ref
  478. if(LUA_NOREF!=nud->cb_connect_ref){
  479. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
  480. nud->cb_connect_ref = LUA_NOREF;
  481. }
  482. if(LUA_NOREF!=nud->cb_reconnect_ref){
  483. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_reconnect_ref);
  484. nud->cb_reconnect_ref = LUA_NOREF;
  485. }
  486. if(LUA_NOREF!=nud->cb_disconnect_ref){
  487. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
  488. nud->cb_disconnect_ref = LUA_NOREF;
  489. }
  490. if(LUA_NOREF!=nud->cb_receive_ref){
  491. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_receive_ref);
  492. nud->cb_receive_ref = LUA_NOREF;
  493. }
  494. if(LUA_NOREF!=nud->cb_send_ref){
  495. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
  496. nud->cb_send_ref = LUA_NOREF;
  497. }
  498. if(LUA_NOREF!=nud->cb_dns_found_ref){
  499. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
  500. nud->cb_dns_found_ref = LUA_NOREF;
  501. }
  502. lua_gc(gL, LUA_GCSTOP, 0);
  503. if(LUA_NOREF!=nud->self_ref){
  504. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  505. nud->self_ref = LUA_NOREF;
  506. }
  507. lua_gc(gL, LUA_GCRESTART, 0);
  508. return 0;
  509. }
  510. static void socket_connect(struct espconn *pesp_conn)
  511. {
  512. if(pesp_conn == NULL)
  513. return;
  514. lnet_userdata *nud = (lnet_userdata *)pesp_conn->reverse;
  515. if(nud == NULL)
  516. return;
  517. if( pesp_conn->type == ESPCONN_TCP )
  518. {
  519. #ifdef CLIENT_SSL_ENABLE
  520. if(nud->secure){
  521. espconn_secure_connect(pesp_conn);
  522. }
  523. else
  524. #endif
  525. {
  526. espconn_connect(pesp_conn);
  527. }
  528. }
  529. else if (pesp_conn->type == ESPCONN_UDP)
  530. {
  531. espconn_create(pesp_conn);
  532. }
  533. NODE_DBG("socket_connect is called.\n");
  534. }
  535. static void socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg);
  536. static dns_reconn_count = 0;
  537. static void socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
  538. {
  539. NODE_DBG("socket_dns_found is called.\n");
  540. struct espconn *pesp_conn = arg;
  541. if(pesp_conn == NULL){
  542. NODE_DBG("pesp_conn null.\n");
  543. return;
  544. }
  545. if(ipaddr == NULL)
  546. {
  547. dns_reconn_count++;
  548. if( dns_reconn_count >= 5 ){
  549. NODE_ERR( "DNS Fail!\n" );
  550. return;
  551. }
  552. NODE_ERR( "DNS retry %d!\n", dns_reconn_count );
  553. host_ip.addr = 0;
  554. espconn_gethostbyname(pesp_conn, name, &host_ip, socket_dns_found);
  555. return;
  556. }
  557. // ipaddr->addr is a uint32_t ip
  558. if(ipaddr->addr != 0)
  559. {
  560. dns_reconn_count = 0;
  561. if( pesp_conn->type == ESPCONN_TCP )
  562. {
  563. c_memcpy(pesp_conn->proto.tcp->remote_ip, &(ipaddr->addr), 4);
  564. NODE_DBG("TCP ip is set: ");
  565. NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr)));
  566. NODE_DBG("\n");
  567. }
  568. else if (pesp_conn->type == ESPCONN_UDP)
  569. {
  570. c_memcpy(pesp_conn->proto.udp->remote_ip, &(ipaddr->addr), 4);
  571. NODE_DBG("UDP ip is set: ");
  572. NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr)));
  573. NODE_DBG("\n");
  574. }
  575. socket_connect(pesp_conn);
  576. }
  577. }
  578. // Lua: server:listen( port, ip, function(con) )
  579. // Lua: socket:connect( port, ip, function(con) )
  580. static int net_start( lua_State* L, const char* mt )
  581. {
  582. NODE_DBG("net_start is called.\n");
  583. struct espconn *pesp_conn = NULL;
  584. lnet_userdata *nud;
  585. unsigned port;
  586. size_t il;
  587. bool isserver = false;
  588. ip_addr_t ipaddr;
  589. const char *domain;
  590. uint8_t stack = 1;
  591. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  592. isserver = true;
  593. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  594. isserver = false;
  595. else
  596. {
  597. NODE_DBG("wrong metatable for net_start.\n");
  598. return 0;
  599. }
  600. nud = (lnet_userdata *)luaL_checkudata(L, stack, mt);
  601. luaL_argcheck(L, nud, stack, "Server/Socket expected");
  602. stack++;
  603. if(nud==NULL){
  604. NODE_DBG("userdata is nil.\n");
  605. return 0;
  606. }
  607. pesp_conn = nud->pesp_conn;
  608. port = luaL_checkinteger( L, stack );
  609. stack++;
  610. if( pesp_conn->type == ESPCONN_TCP )
  611. {
  612. if(isserver)
  613. pesp_conn->proto.tcp->local_port = port;
  614. else{
  615. pesp_conn->proto.tcp->remote_port = port;
  616. pesp_conn->proto.tcp->local_port = espconn_port();
  617. }
  618. NODE_DBG("TCP port is set: %d.\n", port);
  619. }
  620. else if (pesp_conn->type == ESPCONN_UDP)
  621. {
  622. if(isserver)
  623. pesp_conn->proto.udp->local_port = port;
  624. else{
  625. pesp_conn->proto.udp->remote_port = port;
  626. pesp_conn->proto.udp->local_port = espconn_port();
  627. }
  628. NODE_DBG("UDP port is set: %d.\n", port);
  629. }
  630. if( lua_isstring(L,stack) ) // deal with the domain string
  631. {
  632. domain = luaL_checklstring( L, stack, &il );
  633. stack++;
  634. if (domain == NULL)
  635. {
  636. if(isserver)
  637. domain = "0.0.0.0";
  638. else
  639. domain = "127.0.0.1";
  640. }
  641. ipaddr.addr = ipaddr_addr(domain);
  642. if( pesp_conn->type == ESPCONN_TCP )
  643. {
  644. if(isserver)
  645. c_memcpy(pesp_conn->proto.tcp->local_ip, &ipaddr.addr, 4);
  646. else
  647. c_memcpy(pesp_conn->proto.tcp->remote_ip, &ipaddr.addr, 4);
  648. NODE_DBG("TCP ip is set: ");
  649. NODE_DBG(IPSTR, IP2STR(&ipaddr.addr));
  650. NODE_DBG("\n");
  651. }
  652. else if (pesp_conn->type == ESPCONN_UDP)
  653. {
  654. if(isserver)
  655. c_memcpy(pesp_conn->proto.udp->local_ip, &ipaddr.addr, 4);
  656. else
  657. c_memcpy(pesp_conn->proto.udp->remote_ip, &ipaddr.addr, 4);
  658. NODE_DBG("UDP ip is set: ");
  659. NODE_DBG(IPSTR, IP2STR(&ipaddr.addr));
  660. NODE_DBG("\n");
  661. }
  662. }
  663. // call back function when a connection is obtained, tcp only
  664. if ( pesp_conn->type == ESPCONN_TCP ) {
  665. if (lua_type(L, stack) == LUA_TFUNCTION || lua_type(L, stack) == LUA_TLIGHTFUNCTION){
  666. lua_pushvalue(L, stack); // copy argument (func) to the top of stack
  667. if(isserver) // for tcp server connected callback
  668. {
  669. if(tcpserver_cb_connect_ref != LUA_NOREF)
  670. luaL_unref(L, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref);
  671. tcpserver_cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  672. }
  673. else
  674. {
  675. if(nud->cb_connect_ref != LUA_NOREF)
  676. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
  677. nud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  678. }
  679. }
  680. }
  681. if(!isserver || pesp_conn->type == ESPCONN_UDP){ // self_ref is only needed by socket userdata, or udp server
  682. lua_pushvalue(L, 1); // copy to the top of stack
  683. if(nud->self_ref != LUA_NOREF)
  684. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  685. nud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  686. }
  687. if( pesp_conn->type == ESPCONN_TCP )
  688. {
  689. if(isserver){ // no secure server support for now
  690. espconn_regist_connectcb(pesp_conn, net_server_connected);
  691. // tcp server, SSL is not supported
  692. #ifdef CLIENT_SSL_ENABLE
  693. // if(nud->secure)
  694. // espconn_secure_accept(pesp_conn);
  695. // else
  696. #endif
  697. espconn_accept(pesp_conn); // if it's a server, no need to dns.
  698. espconn_regist_time(pesp_conn, tcp_server_timeover, 0);
  699. }
  700. else{
  701. espconn_regist_connectcb(pesp_conn, net_socket_connected);
  702. espconn_regist_reconcb(pesp_conn, net_socket_reconnected);
  703. #ifdef CLIENT_SSL_ENABLE
  704. if(nud->secure){
  705. if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port)
  706. espconn_secure_disconnect(pesp_conn);
  707. // espconn_secure_connect(pesp_conn);
  708. }
  709. else
  710. #endif
  711. {
  712. if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port)
  713. espconn_disconnect(pesp_conn);
  714. // espconn_connect(pesp_conn);
  715. }
  716. }
  717. }
  718. else if (pesp_conn->type == ESPCONN_UDP)
  719. {
  720. espconn_regist_recvcb(pesp_conn, net_socket_received);
  721. espconn_regist_sentcb(pesp_conn, net_socket_sent);
  722. if(pesp_conn->proto.tcp->remote_port || pesp_conn->proto.tcp->local_port)
  723. espconn_delete(pesp_conn);
  724. if(isserver)
  725. espconn_create(pesp_conn); // if it's a server, no need to dns.
  726. }
  727. if(!isserver){
  728. if((ipaddr.addr == IPADDR_NONE) && (c_memcmp(domain,"255.255.255.255",16) != 0))
  729. {
  730. host_ip.addr = 0;
  731. dns_reconn_count = 0;
  732. if(ESPCONN_OK == espconn_gethostbyname(pesp_conn, domain, &host_ip, socket_dns_found)){
  733. socket_dns_found(domain, &host_ip, pesp_conn); // ip is returned in host_ip.
  734. }
  735. }
  736. else
  737. {
  738. socket_connect(pesp_conn);
  739. }
  740. }
  741. return 0;
  742. }
  743. // Lua: server/socket:close()
  744. // server disconnect everything, unref everything
  745. // client disconnect and unref itself
  746. static int net_close( lua_State* L, const char* mt )
  747. {
  748. NODE_DBG("net_close is called.\n");
  749. bool isserver = false;
  750. int i = 0;
  751. lnet_userdata *nud = NULL, *skt = NULL;
  752. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  753. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  754. if(nud == NULL)
  755. return 0;
  756. if(nud->pesp_conn == NULL)
  757. return 0;
  758. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  759. isserver = true;
  760. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  761. isserver = false;
  762. else
  763. {
  764. NODE_DBG("wrong metatable for net_close.\n");
  765. return 0;
  766. }
  767. if(isserver && nud->pesp_conn->type == ESPCONN_TCP && tcpserver_cb_connect_ref != LUA_NOREF){
  768. luaL_unref(L, LUA_REGISTRYINDEX, tcpserver_cb_connect_ref);
  769. tcpserver_cb_connect_ref = LUA_NOREF;
  770. }
  771. int n = lua_gettop(L);
  772. skt = nud;
  773. do{
  774. if(isserver && skt == NULL){
  775. if(socket[i] != LUA_NOREF){ // there is client socket exists
  776. lua_rawgeti(L, LUA_REGISTRYINDEX, socket[i]); // get the referenced user_data to stack top
  777. #if 0
  778. socket[i] = LUA_NOREF;
  779. socket_num--;
  780. #endif // do this in net_server_disconnected
  781. i++;
  782. if(lua_isuserdata(L,-1)){
  783. skt = lua_touserdata(L,-1);
  784. } else {
  785. lua_pop(L, 1);
  786. continue;
  787. }
  788. }else{
  789. // skip LUA_NOREF
  790. i++;
  791. continue;
  792. }
  793. }
  794. if(skt==NULL){
  795. NODE_DBG("userdata is nil.\n");
  796. continue;
  797. }
  798. if(skt->pesp_conn) // disconnect the connection
  799. {
  800. if(skt->pesp_conn->type == ESPCONN_TCP)
  801. {
  802. #ifdef CLIENT_SSL_ENABLE
  803. if(skt->secure){
  804. if(skt->pesp_conn->proto.tcp->remote_port || skt->pesp_conn->proto.tcp->local_port)
  805. espconn_secure_disconnect(skt->pesp_conn);
  806. }
  807. else
  808. #endif
  809. {
  810. if(skt->pesp_conn->proto.tcp->remote_port || skt->pesp_conn->proto.tcp->local_port)
  811. espconn_disconnect(skt->pesp_conn);
  812. }
  813. }else if(skt->pesp_conn->type == ESPCONN_UDP)
  814. {
  815. if(skt->pesp_conn->proto.tcp->remote_port || skt->pesp_conn->proto.tcp->local_port)
  816. espconn_delete(skt->pesp_conn);
  817. // a udp server/socket unref it self here. not in disconnect.
  818. if(LUA_NOREF!=skt->self_ref){ // for a udp self_ref is NOREF
  819. luaL_unref(L, LUA_REGISTRYINDEX, skt->self_ref);
  820. skt->self_ref = LUA_NOREF; // for a socket, now only var in lua is ref to the userdata
  821. }
  822. }
  823. }
  824. #if 0
  825. // unref the self_ref
  826. if(LUA_NOREF!=skt->self_ref){ // for a server self_ref is NOREF
  827. luaL_unref(L, LUA_REGISTRYINDEX, skt->self_ref);
  828. skt->self_ref = LUA_NOREF; // for a socket, now only var in lua is ref to the userdata
  829. }
  830. #endif
  831. lua_settop(L, n); // reset the stack top
  832. skt = NULL;
  833. } while( isserver && i<MAX_SOCKET);
  834. #if 0
  835. // unref the self_ref, for both socket and server
  836. if(LUA_NOREF!=nud->self_ref){ // for a server self_ref is NOREF
  837. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  838. nud->self_ref = LUA_NOREF; // now only var in lua is ref to the userdata
  839. }
  840. #endif
  841. return 0;
  842. }
  843. // Lua: socket/udpserver:on( "method", function(s) )
  844. static int net_on( lua_State* L, const char* mt )
  845. {
  846. NODE_DBG("net_on is called.\n");
  847. bool isserver = false;
  848. lnet_userdata *nud;
  849. size_t sl;
  850. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  851. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  852. if(nud==NULL){
  853. NODE_DBG("userdata is nil.\n");
  854. return 0;
  855. }
  856. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  857. isserver = true;
  858. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  859. isserver = false;
  860. else
  861. {
  862. NODE_DBG("wrong metatable for net_on.\n");
  863. return 0;
  864. }
  865. const char *method = luaL_checklstring( L, 2, &sl );
  866. if (method == NULL)
  867. return luaL_error( L, "wrong arg type" );
  868. luaL_checkanyfunction(L, 3);
  869. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  870. if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 10 && c_strcmp(method, "connection") == 0){
  871. if(nud->cb_connect_ref != LUA_NOREF)
  872. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_connect_ref);
  873. nud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  874. }else if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 12 && c_strcmp(method, "reconnection") == 0){
  875. if(nud->cb_reconnect_ref != LUA_NOREF)
  876. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_reconnect_ref);
  877. nud->cb_reconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  878. }else if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 13 && c_strcmp(method, "disconnection") == 0){
  879. if(nud->cb_disconnect_ref != LUA_NOREF)
  880. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_disconnect_ref);
  881. nud->cb_disconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  882. }else if((!isserver || nud->pesp_conn->type == ESPCONN_UDP) && sl == 7 && c_strcmp(method, "receive") == 0){
  883. if(nud->cb_receive_ref != LUA_NOREF)
  884. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_receive_ref);
  885. nud->cb_receive_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  886. }else if((!isserver || nud->pesp_conn->type == ESPCONN_UDP) && sl == 4 && c_strcmp(method, "sent") == 0){
  887. if(nud->cb_send_ref != LUA_NOREF)
  888. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
  889. nud->cb_send_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  890. }else if(!isserver && nud->pesp_conn->type == ESPCONN_TCP && sl == 3 && c_strcmp(method, "dns") == 0){
  891. if(nud->cb_dns_found_ref != LUA_NOREF)
  892. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
  893. nud->cb_dns_found_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  894. }else{
  895. lua_pop(L, 1);
  896. return luaL_error( L, "method not supported" );
  897. }
  898. return 0;
  899. }
  900. // Lua: server/socket:send( string, function(sent) )
  901. static int net_send( lua_State* L, const char* mt )
  902. {
  903. // NODE_DBG("net_send is called.\n");
  904. bool isserver = false;
  905. struct espconn *pesp_conn = NULL;
  906. lnet_userdata *nud;
  907. size_t l;
  908. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  909. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  910. if(nud==NULL){
  911. NODE_DBG("userdata is nil.\n");
  912. return 0;
  913. }
  914. if(nud->pesp_conn == NULL){
  915. NODE_DBG("nud->pesp_conn is NULL.\n");
  916. return 0;
  917. }
  918. pesp_conn = nud->pesp_conn;
  919. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  920. isserver = true;
  921. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  922. isserver = false;
  923. else
  924. {
  925. NODE_DBG("wrong metatable for net_send.\n");
  926. return 0;
  927. }
  928. if(isserver && nud->pesp_conn->type == ESPCONN_TCP){
  929. return luaL_error( L, "tcp server send not supported" );
  930. }
  931. #if 0
  932. char temp[20] = {0};
  933. c_sprintf(temp, IPSTR, IP2STR( &(pesp_conn->proto.tcp->remote_ip) ) );
  934. NODE_DBG("remote ");
  935. NODE_DBG(temp);
  936. NODE_DBG(":");
  937. NODE_DBG("%d",pesp_conn->proto.tcp->remote_port);
  938. NODE_DBG(" sending data.\n");
  939. #endif
  940. const char *payload = luaL_checklstring( L, 2, &l );
  941. if (l>1460 || payload == NULL)
  942. return luaL_error( L, "need <1460 payload" );
  943. if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION){
  944. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  945. if(nud->cb_send_ref != LUA_NOREF)
  946. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_send_ref);
  947. nud->cb_send_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  948. }
  949. #ifdef CLIENT_SSL_ENABLE
  950. if(nud->secure)
  951. espconn_secure_sent(pesp_conn, (unsigned char *)payload, l);
  952. else
  953. #endif
  954. espconn_sent(pesp_conn, (unsigned char *)payload, l);
  955. return 0;
  956. }
  957. // Lua: socket:dns( string, function(socket, ip) )
  958. static int net_dns( lua_State* L, const char* mt )
  959. {
  960. NODE_DBG("net_dns is called.\n");
  961. bool isserver = false;
  962. struct espconn *pesp_conn = NULL;
  963. lnet_userdata *nud;
  964. size_t l;
  965. nud = (lnet_userdata *)luaL_checkudata(L, 1, mt);
  966. luaL_argcheck(L, nud, 1, "Server/Socket expected");
  967. if(nud==NULL){
  968. NODE_DBG("userdata is nil.\n");
  969. return 0;
  970. }
  971. if (mt!=NULL && c_strcmp(mt, "net.server")==0)
  972. isserver = true;
  973. else if (mt!=NULL && c_strcmp(mt, "net.socket")==0)
  974. isserver = false;
  975. else
  976. {
  977. NODE_DBG("wrong metatable for net_send.\n");
  978. return 0;
  979. }
  980. pesp_conn = nud->pesp_conn;
  981. if(!isserver || pesp_conn->type == ESPCONN_UDP){ // self_ref is only needed by socket userdata, or udp server
  982. lua_pushvalue(L, 1); // copy to the top of stack
  983. if(nud->self_ref != LUA_NOREF)
  984. luaL_unref(L, LUA_REGISTRYINDEX, nud->self_ref);
  985. nud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  986. }
  987. const char *domain = luaL_checklstring( L, 2, &l );
  988. if (l>128 || domain == NULL)
  989. return luaL_error( L, "need <128 domain" );
  990. if (lua_type(L, 3) == LUA_TFUNCTION || lua_type(L, 3) == LUA_TLIGHTFUNCTION){
  991. lua_pushvalue(L, 3); // copy argument (func) to the top of stack
  992. if(nud->cb_dns_found_ref != LUA_NOREF)
  993. luaL_unref(L, LUA_REGISTRYINDEX, nud->cb_dns_found_ref);
  994. nud->cb_dns_found_ref = luaL_ref(L, LUA_REGISTRYINDEX);
  995. }
  996. host_ip.addr = 0;
  997. espconn_gethostbyname(pesp_conn, domain, &host_ip, net_dns_found);
  998. return 0;
  999. }
  1000. // Lua: s = net.createServer(type, function(server))
  1001. static int net_createServer( lua_State* L )
  1002. {
  1003. const char *mt = "net.server";
  1004. return net_create(L, mt);
  1005. }
  1006. // Lua: server:delete()
  1007. static int net_server_delete( lua_State* L )
  1008. {
  1009. const char *mt = "net.server";
  1010. return net_delete(L, mt);
  1011. }
  1012. // Lua: server:listen( port, ip )
  1013. static int net_server_listen( lua_State* L )
  1014. {
  1015. const char *mt = "net.server";
  1016. return net_start(L, mt);
  1017. }
  1018. // Lua: server:close()
  1019. static int net_server_close( lua_State* L )
  1020. {
  1021. const char *mt = "net.server";
  1022. return net_close(L, mt);
  1023. }
  1024. // Lua: udpserver:on( "method", function(udpserver) )
  1025. static int net_udpserver_on( lua_State* L )
  1026. {
  1027. const char *mt = "net.server";
  1028. return net_on(L, mt);
  1029. }
  1030. // Lua: udpserver:send(string, function() )
  1031. static int net_udpserver_send( lua_State* L )
  1032. {
  1033. const char *mt = "net.server";
  1034. return net_send(L, mt);;
  1035. }
  1036. // Lua: s = net.createConnection(type, function(conn))
  1037. static int net_createConnection( lua_State* L )
  1038. {
  1039. const char *mt = "net.socket";
  1040. return net_create(L, mt);
  1041. }
  1042. // Lua: socket:delete()
  1043. static int net_socket_delete( lua_State* L )
  1044. {
  1045. const char *mt = "net.socket";
  1046. return net_delete(L, mt);
  1047. }
  1048. // Lua: socket:connect( port, ip )
  1049. static int net_socket_connect( lua_State* L )
  1050. {
  1051. const char *mt = "net.socket";
  1052. return net_start(L, mt);
  1053. }
  1054. // Lua: socket:close()
  1055. static int net_socket_close( lua_State* L )
  1056. {
  1057. const char *mt = "net.socket";
  1058. return net_close(L, mt);
  1059. }
  1060. // Lua: socket:on( "method", function(socket) )
  1061. static int net_socket_on( lua_State* L )
  1062. {
  1063. const char *mt = "net.socket";
  1064. return net_on(L, mt);
  1065. }
  1066. // Lua: socket:send( string, function() )
  1067. static int net_socket_send( lua_State* L )
  1068. {
  1069. const char *mt = "net.socket";
  1070. return net_send(L, mt);
  1071. }
  1072. // Lua: socket:dns( string, function(ip) )
  1073. static int net_socket_dns( lua_State* L )
  1074. {
  1075. const char *mt = "net.socket";
  1076. return net_dns(L, mt);
  1077. }
  1078. #if 0
  1079. static int net_array_index( lua_State* L )
  1080. {
  1081. char** parray = luaL_checkudata(L, 1, "net.array");
  1082. int index = luaL_checkint(L, 2);
  1083. lua_pushnumber(L, (*parray)[index-1]);
  1084. return 1;
  1085. }
  1086. static int net_array_newindex( lua_State* L )
  1087. {
  1088. char** parray = luaL_checkudata(L, 1, "net.array");
  1089. int index = luaL_checkint(L, 2);
  1090. int value = luaL_checkint(L, 3);
  1091. (*parray)[index-1] = value;
  1092. return 0;
  1093. }
  1094. // expose an array to lua, by storing it in a userdata with the array metatable
  1095. static int expose_array(lua_State* L, char *array, unsigned short len) {
  1096. char** parray = lua_newuserdata(L, len);
  1097. *parray = array;
  1098. luaL_getmetatable(L, "net.array");
  1099. lua_setmetatable(L, -2);
  1100. return 1;
  1101. }
  1102. #endif
  1103. // Module function map
  1104. #define MIN_OPT_LEVEL 2
  1105. #include "lrodefs.h"
  1106. static const LUA_REG_TYPE net_server_map[] =
  1107. {
  1108. { LSTRKEY( "listen" ), LFUNCVAL ( net_server_listen ) },
  1109. { LSTRKEY( "close" ), LFUNCVAL ( net_server_close ) },
  1110. { LSTRKEY( "on" ), LFUNCVAL ( net_udpserver_on ) },
  1111. { LSTRKEY( "send" ), LFUNCVAL ( net_udpserver_send ) },
  1112. // { LSTRKEY( "delete" ), LFUNCVAL ( net_server_delete ) },
  1113. { LSTRKEY( "__gc" ), LFUNCVAL ( net_server_delete ) },
  1114. #if LUA_OPTIMIZE_MEMORY > 0
  1115. { LSTRKEY( "__index" ), LROVAL ( net_server_map ) },
  1116. #endif
  1117. { LNILKEY, LNILVAL }
  1118. };
  1119. static const LUA_REG_TYPE net_socket_map[] =
  1120. {
  1121. { LSTRKEY( "connect" ), LFUNCVAL( net_socket_connect ) },
  1122. { LSTRKEY( "close" ), LFUNCVAL ( net_socket_close ) },
  1123. { LSTRKEY( "on" ), LFUNCVAL ( net_socket_on ) },
  1124. { LSTRKEY( "send" ), LFUNCVAL ( net_socket_send ) },
  1125. { LSTRKEY( "dns" ), LFUNCVAL ( net_socket_dns ) },
  1126. // { LSTRKEY( "delete" ), LFUNCVAL ( net_socket_delete ) },
  1127. { LSTRKEY( "__gc" ), LFUNCVAL ( net_socket_delete ) },
  1128. #if LUA_OPTIMIZE_MEMORY > 0
  1129. { LSTRKEY( "__index" ), LROVAL ( net_socket_map ) },
  1130. #endif
  1131. { LNILKEY, LNILVAL }
  1132. };
  1133. #if 0
  1134. static const LUA_REG_TYPE net_array_map[] =
  1135. {
  1136. { LSTRKEY( "__index" ), LFUNCVAL( net_array_index ) },
  1137. { LSTRKEY( "__newindex" ), LFUNCVAL( net_array_newindex ) },
  1138. { LNILKEY, LNILVAL }
  1139. };
  1140. #endif
  1141. const LUA_REG_TYPE net_map[] =
  1142. {
  1143. { LSTRKEY( "createServer" ), LFUNCVAL ( net_createServer ) },
  1144. { LSTRKEY( "createConnection" ), LFUNCVAL ( net_createConnection ) },
  1145. #if LUA_OPTIMIZE_MEMORY > 0
  1146. { LSTRKEY( "TCP" ), LNUMVAL( TCP ) },
  1147. { LSTRKEY( "UDP" ), LNUMVAL( UDP ) },
  1148. { LSTRKEY( "__metatable" ), LROVAL( net_map ) },
  1149. #endif
  1150. { LNILKEY, LNILVAL }
  1151. };
  1152. LUALIB_API int luaopen_net( lua_State *L )
  1153. {
  1154. int i;
  1155. for(i=0;i<MAX_SOCKET;i++)
  1156. {
  1157. socket[i] = LUA_NOREF;
  1158. }
  1159. #if LUA_OPTIMIZE_MEMORY > 0
  1160. luaL_rometatable(L, "net.server", (void *)net_server_map); // create metatable for net.server
  1161. luaL_rometatable(L, "net.socket", (void *)net_socket_map); // create metatable for net.socket
  1162. #if 0
  1163. luaL_rometatable(L, "net.array", (void *)net_array_map); // create metatable for net.array
  1164. #endif
  1165. return 0;
  1166. #else // #if LUA_OPTIMIZE_MEMORY > 0
  1167. int n;
  1168. luaL_register( L, AUXLIB_NET, net_map );
  1169. // Set it as its own metatable
  1170. lua_pushvalue( L, -1 );
  1171. lua_setmetatable( L, -2 );
  1172. // Module constants
  1173. MOD_REG_NUMBER( L, "TCP", TCP );
  1174. MOD_REG_NUMBER( L, "UDP", UDP );
  1175. n = lua_gettop(L);
  1176. // create metatable
  1177. luaL_newmetatable(L, "net.server");
  1178. // metatable.__index = metatable
  1179. lua_pushliteral(L, "__index");
  1180. lua_pushvalue(L,-2);
  1181. lua_rawset(L,-3);
  1182. // Setup the methods inside metatable
  1183. luaL_register( L, NULL, net_server_map );
  1184. lua_settop(L, n);
  1185. // create metatable
  1186. luaL_newmetatable(L, "net.socket");
  1187. // metatable.__index = metatable
  1188. lua_pushliteral(L, "__index");
  1189. lua_pushvalue(L,-2);
  1190. lua_rawset(L,-3);
  1191. // Setup the methods inside metatable
  1192. luaL_register( L, NULL, net_socket_map );
  1193. #if 0
  1194. lua_settop(L, n);
  1195. // create metatable
  1196. luaL_newmetatable(L, "net.array");
  1197. // Setup the methods inside metatable
  1198. luaL_register( L, NULL, net_array_map );
  1199. #endif
  1200. return 1;
  1201. #endif // #if LUA_OPTIMIZE_MEMORY > 0
  1202. }