luat_netclient_rtt.c 12 KB

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