luat_netclient_rtt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * File : netclient.c
  3. * This file is part of RT-Thread
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-08-10 never the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #ifdef RT_USING_SAL
  27. #include <sys/socket.h>
  28. #include <sys/select.h>
  29. #include <sys/types.h>
  30. #include <unistd.h>
  31. #include <netdb.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <stdlib.h>
  35. //#include "elog.h"
  36. #include "netclient.h"
  37. #include "luat_malloc.h"
  38. #include "rtthread.h"
  39. #define DBG_TAG "luat.netc"
  40. #define DBG_LVL DBG_INFO
  41. #include <rtdbg.h>
  42. #define BUFF_SIZE (1024)
  43. #define MAX_VAL(A, B) ((A) > (B) ? (A) : (B))
  44. #define STRCMP(a, R, b) (strcmp((a), (b)) R 0)
  45. static netclient_t *netclient_create(void);
  46. static rt_int32_t netclient_destory(netclient_t *thiz);
  47. static rt_int32_t socket_init(netclient_t *thiz, const char *hostname, int port);
  48. static rt_int32_t socket_deinit(netclient_t *thiz);
  49. static rt_int32_t pipe_init(netclient_t *thiz);
  50. static rt_int32_t pipe_deinit(netclient_t *thiz);
  51. static void select_handle(netclient_t *thiz, char *sock_buff);
  52. static rt_int32_t netclient_thread_init(netclient_t *thiz);
  53. static void netclient_thread_entry(void *param);
  54. static rt_uint32_t netc_seq = 1;
  55. uint32_t netc_next_no(void) {
  56. if (netc_seq > 0xFFFF00) {
  57. netc_seq = 0xFF;
  58. }
  59. return netc_seq++;
  60. }
  61. static void EVENT(int netc_id, tpc_cb_t cb, int lua_ref, int tp, size_t len, void* buff) {
  62. netc_ent_t* ent;
  63. LOG_I("netc[%ld] event type=%d", netc_id, tp);
  64. if (cb == RT_NULL) return;
  65. if (tp != NETC_EVENT_RECV || len < 0) len = 0;
  66. //len = 0;
  67. ent = luat_heap_malloc(sizeof(netc_ent_t));
  68. if (ent == NULL) {
  69. LOG_E("netc[%ld] EVENT call rt_malloc return NULL!", netc_id);
  70. return;
  71. }
  72. ent->netc_id = netc_id;
  73. ent->lua_ref = lua_ref;
  74. ent->len = len;
  75. ent->event = tp;
  76. if (len > 0) {
  77. ent->buff = luat_heap_malloc(len);
  78. if (ent->buff == RT_NULL) {
  79. LOG_E("netc[%ld] EVENT call rt_malloc buff return NULL!", netc_id);
  80. luat_heap_free(ent);
  81. return;
  82. }
  83. rt_memcpy(ent->buff, buff, len);
  84. }
  85. else {
  86. ent->buff = RT_NULL;
  87. }
  88. cb(ent);
  89. }
  90. static rt_int32_t netclient_destory(netclient_t *thiz)
  91. {
  92. int res = 0;
  93. if (thiz == RT_NULL)
  94. {
  95. LOG_E("netclient del : param is NULL, delete failed");
  96. return -1;
  97. }
  98. LOG_I("netc[%ld] destory begin", thiz->id);
  99. if (thiz->sock_fd != -1)
  100. socket_deinit(thiz);
  101. pipe_deinit(thiz);
  102. LOG_I("netc[%ld] destory end", thiz->id);
  103. thiz->closed = 1;
  104. return 0;
  105. }
  106. static rt_int32_t socket_init(netclient_t *thiz, const char *url, int port)
  107. {
  108. struct sockaddr_in dst_addr;
  109. struct hostent *hostname;
  110. rt_int32_t res = 0;
  111. if (thiz == RT_NULL)
  112. return -1;
  113. thiz->sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  114. if (thiz->sock_fd == -1)
  115. {
  116. LOG_E("netc[%ld] socket init : socket create failed", thiz->id);
  117. return -1;
  118. }
  119. hostname = gethostbyname(url);
  120. if (hostname == NULL) {
  121. LOG_W("netc[%ld] dns name relove fail, retry at 2000ms", thiz->id);
  122. rt_thread_mdelay(2000);
  123. hostname = gethostbyname(url);
  124. if (hostname == NULL) {
  125. LOG_W("netc[%ld] dns name relove fail again, quit", thiz->id);
  126. return -1;
  127. }
  128. }
  129. // TODO: print ip for hostname
  130. //LOG_I("socket host=%s port=%d", hostname, port);
  131. dst_addr.sin_family = AF_INET;
  132. dst_addr.sin_port = htons(port);
  133. dst_addr.sin_addr = *((struct in_addr *)hostname->h_addr);
  134. memset(&(dst_addr.sin_zero), 0, sizeof(dst_addr.sin_zero));
  135. res = connect(thiz->sock_fd, (struct sockaddr *)&dst_addr, sizeof(struct sockaddr));
  136. if (res == -1)
  137. {
  138. LOG_E("netc[%ld] connect failed", thiz->id);
  139. return -1;
  140. }
  141. LOG_I("netc[%ld] connected succeed", thiz->id);
  142. //send(thiz->sock_fd, str, strlen(str), 0);
  143. return 0;
  144. }
  145. static rt_int32_t socket_deinit(netclient_t *thiz)
  146. {
  147. int res = 0;
  148. if (thiz == RT_NULL)
  149. {
  150. LOG_E("socket deinit : param is NULL, socket deinit failed");
  151. return -1;
  152. }
  153. if (thiz->sock_fd < 0)
  154. return 0;
  155. res = closesocket(thiz->sock_fd);
  156. //RT_ASSERT(res == 0);
  157. thiz->sock_fd = -1;
  158. LOG_I("netc[%ld] socket close succeed", thiz->id);
  159. return 0;
  160. }
  161. static rt_int32_t pipe_init(netclient_t *thiz)
  162. {
  163. char dev_name[32];
  164. rt_pipe_t *pipe = RT_NULL;
  165. if (thiz == RT_NULL)
  166. {
  167. LOG_E("pipe init : param is NULL");
  168. return -1;
  169. }
  170. snprintf(thiz->pipe_name, sizeof(thiz->pipe_name), "p%06X", thiz->id);
  171. pipe = rt_pipe_create(thiz->pipe_name, PIPE_BUFSZ);
  172. if (pipe == RT_NULL)
  173. {
  174. thiz->pipe_name[0] = 0x00;
  175. LOG_E("netc[%ld] pipe create failed", thiz->id);
  176. return -1;
  177. }
  178. snprintf(dev_name, sizeof(dev_name), "/dev/%s", thiz->pipe_name);
  179. thiz->pipe_read_fd = open(dev_name, O_RDONLY, 0);
  180. if (thiz->pipe_read_fd < 0)
  181. goto fail_read;
  182. thiz->pipe_write_fd = open(dev_name, O_WRONLY, 0);
  183. if (thiz->pipe_write_fd < 0)
  184. goto fail_write;
  185. LOG_I("netc[%ld] pipe init succeed", thiz->id);
  186. return 0;
  187. fail_write:
  188. close(thiz->pipe_read_fd);
  189. fail_read:
  190. rt_pipe_delete(thiz->pipe_name);
  191. thiz->pipe_name[0] = 0x00;
  192. return -1;
  193. }
  194. static rt_int32_t pipe_deinit(netclient_t *thiz)
  195. {
  196. int res = 0;
  197. if (thiz == RT_NULL)
  198. {
  199. LOG_E("pipe deinit : param is NULL, pipe deinit failed");
  200. return -1;
  201. }
  202. if (thiz->pipe_read_fd != -1) {
  203. close(thiz->pipe_read_fd);
  204. thiz->pipe_read_fd = -1;
  205. res ++;
  206. }
  207. if (thiz->pipe_write_fd != -1) {
  208. res = close(thiz->pipe_write_fd);
  209. thiz->pipe_write_fd = -1;
  210. res ++;
  211. }
  212. if (thiz->pipe_name[0] != 0) {
  213. rt_pipe_delete(thiz->pipe_name);
  214. res ++;
  215. }
  216. if (res)
  217. LOG_I("netc[%ld] pipe close succeed", thiz->id);
  218. return 0;
  219. }
  220. static rt_int32_t netclient_thread_init(netclient_t *thiz)
  221. {
  222. rt_thread_t th;
  223. char tname[12];
  224. rt_sprintf(tname, "n%06X", thiz->id);
  225. th = rt_thread_create(tname, netclient_thread_entry, thiz, 2048, 20, 10);
  226. if (th == RT_NULL)
  227. {
  228. LOG_E("netc[%ld] thread create fail", thiz->id);
  229. return -1;
  230. }
  231. if (rt_thread_startup(th) != RT_EOK) {
  232. rt_thread_detach(th);
  233. LOG_E("netc[%ld] thread start fail", thiz->id);
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. static void select_handle(netclient_t *thiz, char *sock_buff)
  239. {
  240. fd_set fds;
  241. rt_int32_t max_fd = 0, res = 0;
  242. max_fd = MAX_VAL(thiz->sock_fd, thiz->pipe_read_fd) + 1;
  243. FD_ZERO(&fds);
  244. while (1)
  245. {
  246. FD_SET(thiz->sock_fd, &fds);
  247. FD_SET(thiz->pipe_read_fd, &fds);
  248. res = select(max_fd, &fds, RT_NULL, RT_NULL, RT_NULL);
  249. /* exception handling: exit */
  250. if (res <= 0) {
  251. LOG_I("netc[%ld] select result=%d, goto cleanup", thiz->id, res);
  252. goto exit;
  253. }
  254. /* socket is ready */
  255. if (FD_ISSET(thiz->sock_fd, &fds))
  256. {
  257. #if 1
  258. res = recv(thiz->sock_fd, sock_buff, BUFF_SIZE, 0);
  259. if (res > 0) {
  260. LOG_I("netc[%ld] data recv len=%d", thiz->id, res);
  261. if (thiz->rx) {
  262. EVENT(thiz->id, thiz->rx, thiz->cb_recv, NETC_EVENT_RECV, res, sock_buff);
  263. }
  264. }
  265. else {
  266. LOG_I("netc[%ld] recv return error=%d", thiz->id, res);
  267. if (thiz->rx) {
  268. EVENT(thiz->id, thiz->rx, thiz->cb_error, NETC_EVENT_ERROR, res, sock_buff);
  269. }
  270. goto exit;
  271. }
  272. #endif
  273. //EVENT(thiz->id, thiz->rx, thiz->cb_recv, NETC_EVENT_RECV, res, sock_buff);
  274. }
  275. /* pipe is read */
  276. if (FD_ISSET(thiz->pipe_read_fd, &fds))
  277. {
  278. /* read pipe */
  279. res = read(thiz->pipe_read_fd, sock_buff, BUFF_SIZE);
  280. if (res <= 0) {
  281. thiz->closed = 0;
  282. goto exit;
  283. }
  284. else if (thiz->closed) {
  285. goto exit;
  286. }
  287. else if (res > 0) {
  288. send(thiz->sock_fd, sock_buff, res, 0);
  289. }
  290. }
  291. }
  292. exit:
  293. LOG_I("netc[%ld] select loop exit, cleanup", thiz->id);
  294. return;
  295. }
  296. static void netclient_thread_entry(void *param)
  297. {
  298. netclient_t *thiz = param;
  299. char *sock_buff = RT_NULL;
  300. if (socket_init(thiz, thiz->hostname, thiz->port) != 0) {
  301. LOG_W("netc[%ld] connect fail", thiz->id);
  302. if (thiz->rx) {
  303. EVENT(thiz->id, thiz->rx, thiz->cb_connect, NETC_EVENT_CONNECT_OK, 0, RT_NULL);
  304. }
  305. goto netc_exit;
  306. }
  307. else {
  308. LOG_I("netc[%ld] connect ok", thiz->id);
  309. if (thiz->rx) {
  310. EVENT(thiz->id, thiz->rx, thiz->cb_connect, NETC_EVENT_CONNECT_FAIL, 0, RT_NULL);
  311. }
  312. }
  313. sock_buff = rt_malloc(BUFF_SIZE);
  314. if (sock_buff == RT_NULL)
  315. {
  316. LOG_E("netc[%ld] fail to malloc sock_buff!!!", thiz->id);
  317. goto netc_exit;
  318. }
  319. memset(sock_buff, 0, BUFF_SIZE);
  320. select_handle(thiz, sock_buff);
  321. rt_free(sock_buff);
  322. //if (thiz != NULL) {
  323. thiz->closed = 1;
  324. netclient_destory(thiz);
  325. // if (thiz->rx) {
  326. // EVENT(thiz->id, thiz->rx, thiz->cb_close, NETC_EVENT_CLOSE, 0, RT_NULL);
  327. // }
  328. //}
  329. netc_exit:
  330. thiz->closed = 1;
  331. EVENT(thiz->id, thiz->rx, 0, NETC_EVENT_END, 0, RT_NULL);
  332. LOG_W("netc[%ld] thread end", thiz->id);
  333. }
  334. int32_t netclient_start(netclient_t * thiz) {
  335. if (pipe_init(thiz) != 0)
  336. goto quit;
  337. if (netclient_thread_init(thiz) != 0)
  338. goto quit;
  339. LOG_I("netc[%ld] start succeed", thiz->id);
  340. return 0;
  341. quit:
  342. netclient_destory(thiz);
  343. return 1;
  344. }
  345. void netclient_close(netclient_t *thiz)
  346. {
  347. LOG_I("netc[%ld] deinit start", thiz->id);
  348. int fd = thiz->sock_fd;
  349. if (fd != -1 && fd != 0) {
  350. closesocket(fd);
  351. }
  352. rt_thread_mdelay(1);
  353. netclient_destory(thiz);
  354. LOG_I("netc[%ld] deinit end", thiz->id);
  355. }
  356. int32_t netclient_send(netclient_t *thiz, const void *buff, size_t len)
  357. {
  358. size_t bytes = 0;
  359. if (thiz == RT_NULL)
  360. {
  361. LOG_W("netclient send : param is NULL");
  362. return -1;
  363. }
  364. if (buff == RT_NULL)
  365. {
  366. LOG_W("netc[%ld] send : buff is NULL", thiz->id);
  367. return -1;
  368. }
  369. if (thiz->pipe_write_fd == -1) {
  370. LOG_W("netc[%ld] socket is closed!!!", thiz->id);
  371. return -1;
  372. }
  373. //LOG_D("netc[%ld] send data len=%d buff=[%s]", this->id, len, buff);
  374. bytes = write(thiz->pipe_write_fd, buff, len);
  375. return bytes;
  376. }
  377. #endif