luat_netclient_rtt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 "netclient.h"
  36. #include "luat_malloc.h"
  37. #define LUAT_LOG_TAG "luat.netc"
  38. #include "luat_log.h"
  39. #define BUFF_SIZE (1024)
  40. #define MAX_VAL(A, B) ((A) > (B) ? (A) : (B))
  41. #define STRCMP(a, R, b) (strcmp((a), (b)) R 0)
  42. static netclient_t *netclient_create(void);
  43. static rt_int32_t netclient_destory(netclient_t *thiz);
  44. static rt_int32_t socket_init(netclient_t *thiz, const char *hostname, int port);
  45. static rt_int32_t socket_deinit(netclient_t *thiz);
  46. static rt_int32_t pipe_init(netclient_t *thiz);
  47. static rt_int32_t pipe_deinit(netclient_t *thiz);
  48. static void select_handle(netclient_t *thiz, char *sock_buff);
  49. static rt_int32_t netclient_thread_init(netclient_t *thiz);
  50. static void netclient_thread_entry(void *param);
  51. static rt_uint32_t netc_seq = 1;
  52. uint32_t netc_next_no(void) {
  53. if (netc_seq > 0xFFFF00) {
  54. netc_seq = 0xFF;
  55. }
  56. return netc_seq++;
  57. }
  58. static void EVENT(int netc_id, tpc_cb_t cb, int lua_ref, int tp, size_t len, void* buff) {
  59. netc_ent_t* ent;
  60. LLOGI("netc[%ld] event type=%d", netc_id, tp);
  61. if (cb == RT_NULL) return;
  62. if (tp != NETC_EVENT_RECV || len < 0) len = 0;
  63. //len = 0;
  64. ent = luat_heap_malloc(sizeof(netc_ent_t));
  65. if (ent == NULL) {
  66. LLOGE("netc[%ld] EVENT call rt_malloc return NULL!", netc_id);
  67. return;
  68. }
  69. ent->netc_id = netc_id;
  70. ent->lua_ref = lua_ref;
  71. ent->len = len;
  72. ent->event = tp;
  73. if (len > 0) {
  74. ent->buff = luat_heap_malloc(len);
  75. if (ent->buff == RT_NULL) {
  76. LLOGE("netc[%ld] EVENT call rt_malloc buff return NULL!", netc_id);
  77. luat_heap_free(ent);
  78. return;
  79. }
  80. rt_memcpy(ent->buff, buff, len);
  81. }
  82. else {
  83. ent->buff = RT_NULL;
  84. }
  85. cb(ent);
  86. }
  87. static rt_int32_t netclient_destory(netclient_t *thiz)
  88. {
  89. int res = 0;
  90. if (thiz == RT_NULL)
  91. {
  92. LLOGE("netclient del : param is NULL, delete failed");
  93. return -1;
  94. }
  95. LLOGI("netc[%ld] destory begin", thiz->id);
  96. if (thiz->sock_fd != -1)
  97. socket_deinit(thiz);
  98. pipe_deinit(thiz);
  99. LLOGI("netc[%ld] destory end", thiz->id);
  100. thiz->closed = 1;
  101. return 0;
  102. }
  103. static rt_int32_t socket_init(netclient_t *thiz, const char *url, int port)
  104. {
  105. struct sockaddr_in dst_addr;
  106. struct hostent *hostname;
  107. rt_int32_t res = 0;
  108. if (thiz == RT_NULL)
  109. return -1;
  110. if (thiz->type == NETC_TYPE_TCP)
  111. thiz->sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  112. else
  113. {
  114. thiz->sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
  115. }
  116. if (thiz->sock_fd == -1)
  117. {
  118. LLOGE("netc[%ld] socket init : socket create failed", thiz->id);
  119. return -1;
  120. }
  121. hostname = gethostbyname(url);
  122. if (hostname == NULL) {
  123. LLOGW("netc[%ld] dns name relove fail, retry at 2000ms", thiz->id);
  124. rt_thread_mdelay(2000);
  125. hostname = gethostbyname(url);
  126. if (hostname == NULL) {
  127. LLOGW("netc[%ld] dns name relove fail again, quit", thiz->id);
  128. return -1;
  129. }
  130. }
  131. thiz->ipv4 = (*((struct in_addr *)hostname->h_addr)).s_addr;
  132. // TODO: print ip for hostname
  133. //LLOGI("socket host=%s port=%d", hostname, port);
  134. dst_addr.sin_family = AF_INET;
  135. dst_addr.sin_port = htons(port);
  136. dst_addr.sin_addr = *((struct in_addr *)hostname->h_addr);
  137. memset(&(dst_addr.sin_zero), 0, sizeof(dst_addr.sin_zero));
  138. res = connect(thiz->sock_fd, (struct sockaddr *)&dst_addr, sizeof(struct sockaddr));
  139. if (res == -1)
  140. {
  141. LLOGE("netc[%ld] connect failed", thiz->id);
  142. return -1;
  143. }
  144. LLOGI("netc[%ld] connect succeed", thiz->id);
  145. //send(thiz->sock_fd, str, strlen(str), 0);
  146. return 0;
  147. }
  148. static rt_int32_t socket_deinit(netclient_t *thiz)
  149. {
  150. int res = 0;
  151. if (thiz == RT_NULL)
  152. {
  153. LLOGE("socket deinit : param is NULL, socket deinit failed");
  154. return -1;
  155. }
  156. if (thiz->sock_fd < 0)
  157. return 0;
  158. res = closesocket(thiz->sock_fd);
  159. //RT_ASSERT(res == 0);
  160. thiz->sock_fd = -1;
  161. LLOGI("netc[%ld] socket close succeed", thiz->id);
  162. return 0;
  163. }
  164. static rt_int32_t pipe_init(netclient_t *thiz)
  165. {
  166. char dev_name[32];
  167. rt_pipe_t *pipe = RT_NULL;
  168. if (thiz == RT_NULL)
  169. {
  170. LLOGE("pipe init : param is NULL");
  171. return -1;
  172. }
  173. snprintf(thiz->pipe_name, sizeof(thiz->pipe_name), "p%06X", thiz->id);
  174. pipe = rt_pipe_create(thiz->pipe_name, PIPE_BUFSZ);
  175. if (pipe == RT_NULL)
  176. {
  177. thiz->pipe_name[0] = 0x00;
  178. LLOGE("netc[%ld] pipe create failed", thiz->id);
  179. return -1;
  180. }
  181. snprintf(dev_name, sizeof(dev_name), "/dev/%s", thiz->pipe_name);
  182. thiz->pipe_read_fd = open(dev_name, O_RDONLY, 0);
  183. if (thiz->pipe_read_fd < 0)
  184. goto fail_read;
  185. thiz->pipe_write_fd = open(dev_name, O_WRONLY, 0);
  186. if (thiz->pipe_write_fd < 0)
  187. goto fail_write;
  188. LLOGI("netc[%ld] pipe init succeed", thiz->id);
  189. return 0;
  190. fail_write:
  191. close(thiz->pipe_read_fd);
  192. fail_read:
  193. rt_pipe_delete(thiz->pipe_name);
  194. thiz->pipe_name[0] = 0x00;
  195. return -1;
  196. }
  197. static rt_int32_t pipe_deinit(netclient_t *thiz)
  198. {
  199. int res = 0;
  200. if (thiz == RT_NULL)
  201. {
  202. LLOGE("pipe deinit : param is NULL, pipe deinit failed");
  203. return -1;
  204. }
  205. if (thiz->pipe_read_fd != -1) {
  206. close(thiz->pipe_read_fd);
  207. thiz->pipe_read_fd = -1;
  208. res ++;
  209. }
  210. if (thiz->pipe_write_fd != -1) {
  211. res = close(thiz->pipe_write_fd);
  212. thiz->pipe_write_fd = -1;
  213. res ++;
  214. }
  215. if (thiz->pipe_name[0] != 0) {
  216. rt_pipe_delete(thiz->pipe_name);
  217. res ++;
  218. }
  219. if (res)
  220. LLOGI("netc[%ld] pipe close succeed", thiz->id);
  221. return 0;
  222. }
  223. static rt_int32_t netclient_thread_init(netclient_t *thiz)
  224. {
  225. rt_thread_t th;
  226. char tname[12];
  227. rt_sprintf(tname, "n%06X", thiz->id);
  228. th = rt_thread_create(tname, netclient_thread_entry, thiz, 2048, 20, 10);
  229. if (th == RT_NULL)
  230. {
  231. LLOGE("netc[%ld] thread create fail", thiz->id);
  232. return -1;
  233. }
  234. if (rt_thread_startup(th) != RT_EOK) {
  235. rt_thread_detach(th);
  236. LLOGE("netc[%ld] thread start fail", thiz->id);
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. static void select_handle(netclient_t *thiz, char *sock_buff)
  242. {
  243. fd_set fds;
  244. rt_int32_t max_fd = 0, res = 0;
  245. struct sockaddr_in from = {0};
  246. max_fd = MAX_VAL(thiz->sock_fd, thiz->pipe_read_fd) + 1;
  247. FD_ZERO(&fds);
  248. while (1)
  249. {
  250. FD_SET(thiz->sock_fd, &fds);
  251. FD_SET(thiz->pipe_read_fd, &fds);
  252. res = select(max_fd, &fds, RT_NULL, RT_NULL, RT_NULL);
  253. /* exception handling: exit */
  254. if (res <= 0) {
  255. LLOGI("netc[%ld] select result=%d, goto cleanup", thiz->id, res);
  256. goto exit;
  257. }
  258. /* socket is ready */
  259. if (FD_ISSET(thiz->sock_fd, &fds))
  260. {
  261. #if 1
  262. if (thiz->type == NETC_TYPE_TCP)
  263. res = recv(thiz->sock_fd, sock_buff, BUFF_SIZE, 0);
  264. else
  265. {
  266. res = recvfrom(thiz->sock_fd, sock_buff, BUFF_SIZE, 0, &from, sizeof(struct sockaddr_in));
  267. }
  268. if (res > 0) {
  269. LLOGI("netc[%ld] data recv len=%d", thiz->id, res);
  270. if (thiz->rx) {
  271. EVENT(thiz->id, thiz->rx, thiz->cb_recv, NETC_EVENT_RECV, res, sock_buff);
  272. }
  273. }
  274. else {
  275. LLOGI("netc[%ld] recv return error=%d", thiz->id, res);
  276. if (thiz->rx) {
  277. EVENT(thiz->id, thiz->rx, thiz->cb_error, NETC_EVENT_ERROR, res, sock_buff);
  278. }
  279. goto exit;
  280. }
  281. #endif
  282. //EVENT(thiz->id, thiz->rx, thiz->cb_recv, NETC_EVENT_RECV, res, sock_buff);
  283. }
  284. /* pipe is read */
  285. if (FD_ISSET(thiz->pipe_read_fd, &fds))
  286. {
  287. /* read pipe */
  288. res = read(thiz->pipe_read_fd, sock_buff, BUFF_SIZE);
  289. if (res <= 0) {
  290. thiz->closed = 0;
  291. goto exit;
  292. }
  293. else if (thiz->closed) {
  294. goto exit;
  295. }
  296. else if (res > 0) {
  297. if (thiz->type == NETC_TYPE_TCP)
  298. send(thiz->sock_fd, sock_buff, res, 0);
  299. else
  300. {
  301. from.sin_addr.s_addr = thiz->ipv4;
  302. from.sin_port = htons(thiz->port);
  303. from.sin_family = AF_INET;
  304. sendto(thiz->sock_fd, sock_buff, res, 0, &from, sizeof(struct sockaddr_in));
  305. }
  306. }
  307. }
  308. }
  309. exit:
  310. LLOGI("netc[%ld] select loop exit, cleanup", thiz->id);
  311. return;
  312. }
  313. static void netclient_thread_entry(void *param)
  314. {
  315. netclient_t *thiz = param;
  316. char *sock_buff = RT_NULL;
  317. if (socket_init(thiz, thiz->hostname, thiz->port) != 0) {
  318. LLOGW("netc[%ld] connect fail", thiz->id);
  319. if (thiz->rx) {
  320. EVENT(thiz->id, thiz->rx, thiz->cb_connect, NETC_EVENT_CONNECT_FAIL, 0, RT_NULL);
  321. }
  322. goto netc_exit;
  323. }
  324. else {
  325. LLOGI("netc[%ld] connect ok", thiz->id);
  326. if (thiz->rx) {
  327. EVENT(thiz->id, thiz->rx, thiz->cb_connect, NETC_EVENT_CONNECT_OK, 0, RT_NULL);
  328. }
  329. }
  330. sock_buff = rt_malloc(BUFF_SIZE);
  331. if (sock_buff == RT_NULL)
  332. {
  333. LLOGE("netc[%ld] fail to malloc sock_buff!!!", thiz->id);
  334. goto netc_exit;
  335. }
  336. memset(sock_buff, 0, BUFF_SIZE);
  337. select_handle(thiz, sock_buff);
  338. rt_free(sock_buff);
  339. //if (thiz != NULL) {
  340. thiz->closed = 1;
  341. netclient_destory(thiz);
  342. // if (thiz->rx) {
  343. // EVENT(thiz->id, thiz->rx, thiz->cb_close, NETC_EVENT_CLOSE, 0, RT_NULL);
  344. // }
  345. //}
  346. netc_exit:
  347. thiz->closed = 1;
  348. EVENT(thiz->id, thiz->rx, thiz->self_ref, NETC_EVENT_END, 0, RT_NULL);
  349. LLOGW("netc[%ld] thread end", thiz->id);
  350. }
  351. int32_t netclient_start(netclient_t * thiz) {
  352. if (pipe_init(thiz) != 0)
  353. goto quit;
  354. if (netclient_thread_init(thiz) != 0)
  355. goto quit;
  356. LLOGI("netc[%ld] start succeed", thiz->id);
  357. return 0;
  358. quit:
  359. netclient_destory(thiz);
  360. return 1;
  361. }
  362. void netclient_close(netclient_t *thiz)
  363. {
  364. LLOGI("netc[%ld] deinit start", thiz->id);
  365. int fd = thiz->sock_fd;
  366. if (fd != -1 && fd != 0) {
  367. closesocket(fd);
  368. }
  369. rt_thread_mdelay(1);
  370. netclient_destory(thiz);
  371. LLOGI("netc[%ld] deinit end", thiz->id);
  372. }
  373. int32_t netclient_send(netclient_t *thiz, const void *buff, size_t len, int flags)
  374. {
  375. size_t bytes = 0;
  376. if (thiz == RT_NULL)
  377. {
  378. LLOGW("netclient send : param is NULL");
  379. return -1;
  380. }
  381. if (buff == RT_NULL)
  382. {
  383. LLOGW("netc[%ld] send : buff is NULL", thiz->id);
  384. return -1;
  385. }
  386. if (thiz->pipe_write_fd == -1) {
  387. LLOGW("netc[%ld] socket is closed!!!", thiz->id);
  388. return -1;
  389. }
  390. //LLOGD("netc[%ld] send data len=%d buff=[%s]", this->id, len, buff);
  391. bytes = write(thiz->pipe_write_fd, buff, len);
  392. return bytes;
  393. }
  394. int32_t netclient_rebind(netclient_t * thiz) {
  395. LLOGW("netclient_rebind not support yet!!");
  396. return -1;
  397. }
  398. #endif