netio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /**
  2. * @file
  3. * NetIO Server
  4. *
  5. */
  6. /*
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  21. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  23. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  26. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. * OF SUCH DAMAGE.
  28. *
  29. * This file is part of the lwIP TCP/IP stack.
  30. *
  31. */
  32. #include <rtthread.h>
  33. #ifdef PKG_NETUTILS_NETIO
  34. #include "lwip/opt.h"
  35. #if LWIP_TCP
  36. #include "lwip/tcp.h"
  37. /*
  38. * This implements a netio server.
  39. * The client sends a command word (4 bytes) then a data length word (4 bytes).
  40. * If the command is "receive", the server is to consume "data length" bytes into
  41. * a circular buffer until the first byte is non-zero, then it is to consume
  42. * another command/data pair.
  43. * If the command is "send", the server is to send "data length" bytes from a circular
  44. * buffer with the first byte being zero, until "some time" (6 seconds in the
  45. * current netio126.zip download) has passed and then send one final buffer with
  46. * the first byte being non-zero. Then it is to consume another command/data pair.
  47. */
  48. /* See http://www.nwlab.net/art/netio/netio.html to get the netio tool */
  49. /* implementation options */
  50. #define NETIO_BUF_SIZE (4 * 1024)
  51. #define NETIO_USE_STATIC_BUF 0
  52. /* NetIO server state definition */
  53. #define NETIO_STATE_WAIT_FOR_CMD 0
  54. #define NETIO_STATE_RECV_DATA 1
  55. #define NETIO_STATE_SEND_DATA 2
  56. #define NETIO_STATE_SEND_DATA_LAST 3
  57. #define NETIO_STATE_DONE 4
  58. struct netio_state
  59. {
  60. u32_t state;
  61. u32_t cmd;
  62. u32_t data_len;
  63. u32_t cntr;
  64. u8_t *buf_ptr;
  65. u32_t buf_pos;
  66. u32_t first_byte;
  67. u32_t time_stamp;
  68. };
  69. /* NetIO command protocol definition */
  70. #define NETIO_CMD_QUIT 0
  71. #define NETIO_CMD_C2S 1
  72. #define NETIO_CMD_S2C 2
  73. #define NETIO_CMD_RES 3
  74. static err_t netio_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
  75. static void
  76. netio_close(void *arg, struct tcp_pcb *pcb)
  77. {
  78. err_t err;
  79. struct netio_state *ns = arg;
  80. ns->state = NETIO_STATE_DONE;
  81. tcp_recv(pcb, NULL);
  82. err = tcp_close(pcb);
  83. if (err != ERR_OK)
  84. {
  85. /* closing failed, try again later */
  86. tcp_recv(pcb, netio_recv);
  87. }
  88. else
  89. {
  90. /* closing succeeded */
  91. #if NETIO_USE_STATIC_BUF != 1
  92. if (ns->buf_ptr != NULL)
  93. {
  94. mem_free(ns->buf_ptr);
  95. }
  96. #endif
  97. tcp_arg(pcb, NULL);
  98. tcp_poll(pcb, NULL, 0);
  99. tcp_sent(pcb, NULL);
  100. if (arg != NULL)
  101. {
  102. mem_free(arg);
  103. }
  104. }
  105. }
  106. static err_t
  107. netio_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  108. {
  109. struct netio_state *ns = arg;
  110. u8_t *data_ptr;
  111. u32_t data_cntr;
  112. struct pbuf *q = p;
  113. u16_t len;
  114. if (p != NULL)
  115. {
  116. tcp_recved(pcb, p->tot_len);
  117. }
  118. if (err == ERR_OK && q != NULL)
  119. {
  120. while (q != NULL)
  121. {
  122. data_cntr = q->len;
  123. data_ptr = q->payload;
  124. while (data_cntr--)
  125. {
  126. if (ns->state == NETIO_STATE_DONE)
  127. {
  128. netio_close(ns, pcb);
  129. break;
  130. }
  131. else if (ns->state == NETIO_STATE_WAIT_FOR_CMD)
  132. {
  133. if (ns->cntr < 4)
  134. {
  135. /* build up the CMD field */
  136. ns->cmd <<= 8;
  137. ns->cmd |= *data_ptr++;
  138. ns->cntr++;
  139. }
  140. else if (ns->cntr < 8)
  141. {
  142. /* build up the DATA field */
  143. ns->data_len <<= 8;
  144. ns->data_len |= *data_ptr++;
  145. ns->cntr++;
  146. if (ns->cntr == 8)
  147. {
  148. /* now we have full command and data words */
  149. ns->cntr = 0;
  150. ns->buf_pos = 0;
  151. ns->buf_ptr[0] = 0;
  152. if (ns->cmd == NETIO_CMD_C2S)
  153. {
  154. ns->state = NETIO_STATE_RECV_DATA;
  155. }
  156. else if (ns->cmd == NETIO_CMD_S2C)
  157. {
  158. ns->state = NETIO_STATE_SEND_DATA;
  159. /* start timer */
  160. ns->time_stamp = rt_tick_get();
  161. /* send first round of data */
  162. len = tcp_sndbuf(pcb);
  163. len = LWIP_MIN(len, ns->data_len - ns->cntr);
  164. len = LWIP_MIN(len, NETIO_BUF_SIZE - ns->buf_pos);
  165. do
  166. {
  167. err = tcp_write(pcb, ns->buf_ptr + ns->buf_pos, len, TCP_WRITE_FLAG_COPY);
  168. if (err == ERR_MEM)
  169. {
  170. len /= 2;
  171. }
  172. }
  173. while ((err == ERR_MEM) && (len > 1));
  174. ns->buf_pos += len;
  175. ns->cntr += len;
  176. }
  177. else
  178. {
  179. /* unrecognized command, punt */
  180. ns->cntr = 0;
  181. ns->buf_pos = 0;
  182. ns->buf_ptr[0] = 0;
  183. netio_close(ns, pcb);
  184. break;
  185. }
  186. }
  187. }
  188. else
  189. {
  190. /* in trouble... shouldn't be in this state! */
  191. }
  192. }
  193. else if (ns->state == NETIO_STATE_RECV_DATA)
  194. {
  195. int chunk_length;
  196. if (ns->cntr == 0)
  197. {
  198. /* save the first byte of this new round of data
  199. * this will not match ns->buf_ptr[0] in the case that
  200. * NETIO_BUF_SIZE is less than ns->data_len.
  201. */
  202. ns->first_byte = *data_ptr;
  203. }
  204. if (ns->cntr + (data_cntr + 1) < ns->data_len) chunk_length = data_cntr + 1;
  205. else chunk_length = (ns->data_len - ns->cntr);
  206. ns->buf_pos += chunk_length;
  207. data_ptr += chunk_length;
  208. ns->cntr += chunk_length;
  209. data_cntr -= (chunk_length - 1);
  210. if (ns->buf_pos >= NETIO_BUF_SIZE)
  211. {
  212. /* circularize the buffer */
  213. ns->buf_pos %= NETIO_BUF_SIZE;
  214. }
  215. if (ns->cntr == ns->data_len)
  216. {
  217. ns->cntr = 0;
  218. if (ns->first_byte != 0)
  219. {
  220. /* if this last round did not start with 0,
  221. * go look for another command */
  222. ns->state = NETIO_STATE_WAIT_FOR_CMD;
  223. ns->data_len = 0;
  224. ns->cmd = 0;
  225. /* TODO LWIP_DEBUGF( print out some throughput calculation results... ); */
  226. }
  227. else
  228. {
  229. /* stay here and wait on more data */
  230. }
  231. }
  232. }
  233. else if (ns->state == NETIO_STATE_SEND_DATA
  234. || ns->state == NETIO_STATE_SEND_DATA_LAST)
  235. {
  236. /* I don't think this should happen... */
  237. }
  238. else
  239. {
  240. /* done / quit */
  241. netio_close(ns, pcb);
  242. break;
  243. } /* end of ns->state condition */
  244. } /* end of while data still in this pbuf */
  245. q = q->next;
  246. }
  247. pbuf_free(p);
  248. }
  249. else
  250. {
  251. /* error or closed by other side */
  252. if (p != NULL)
  253. {
  254. pbuf_free(p);
  255. }
  256. /* close the connection */
  257. netio_close(ns, pcb);
  258. }
  259. return ERR_OK;
  260. }
  261. static err_t
  262. netio_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
  263. {
  264. struct netio_state *ns = arg;
  265. err_t err = ERR_OK;
  266. if (ns->cntr >= ns->data_len && ns->state == NETIO_STATE_SEND_DATA)
  267. {
  268. /* done with this round of sending */
  269. ns->buf_pos = 0;
  270. ns->cntr = 0;
  271. /* check if timer expired */
  272. if (rt_tick_get() - ns->time_stamp > 600)
  273. {
  274. ns->buf_ptr[0] = 1;
  275. ns->state = NETIO_STATE_SEND_DATA_LAST;
  276. }
  277. else
  278. {
  279. ns->buf_ptr[0] = 0;
  280. }
  281. }
  282. if (ns->state == NETIO_STATE_SEND_DATA_LAST || ns->state == NETIO_STATE_SEND_DATA)
  283. {
  284. len = tcp_sndbuf(pcb);
  285. len = LWIP_MIN(len, ns->data_len - ns->cntr);
  286. len = LWIP_MIN(len, NETIO_BUF_SIZE - ns->buf_pos);
  287. if (ns->cntr < ns->data_len)
  288. {
  289. do
  290. {
  291. err = tcp_write(pcb, ns->buf_ptr + ns->buf_pos, len, TCP_WRITE_FLAG_COPY);
  292. if (err == ERR_MEM)
  293. {
  294. len /= 2;
  295. }
  296. }
  297. while ((err == ERR_MEM) && (len > 1));
  298. ns->buf_pos += len;
  299. if (ns->buf_pos >= NETIO_BUF_SIZE)
  300. {
  301. ns->buf_pos = 0;
  302. }
  303. ns->cntr += len;
  304. }
  305. }
  306. if (ns->cntr >= ns->data_len && ns->state == NETIO_STATE_SEND_DATA_LAST)
  307. {
  308. /* we have buffered up all our data to send this last round, go look for a command */
  309. ns->state = NETIO_STATE_WAIT_FOR_CMD;
  310. ns->cntr = 0;
  311. /* TODO LWIP_DEBUGF( print out some throughput calculation results... ); */
  312. }
  313. return ERR_OK;
  314. }
  315. static err_t
  316. netio_poll(void *arg, struct tcp_pcb *pcb)
  317. {
  318. struct netio_state *ns = arg;
  319. if (ns->state == NETIO_STATE_SEND_DATA)
  320. {
  321. }
  322. else if (ns->state == NETIO_STATE_DONE)
  323. {
  324. netio_close(ns, pcb);
  325. }
  326. return ERR_OK;
  327. }
  328. #if NETIO_USE_STATIC_BUF == 1
  329. static u8_t netio_buf[NETIO_BUF_SIZE];
  330. #endif
  331. static err_t
  332. netio_accept(void *arg, struct tcp_pcb *pcb, err_t err)
  333. {
  334. struct netio_state *ns;
  335. LWIP_UNUSED_ARG(err);
  336. ns = mem_malloc(sizeof(struct netio_state));
  337. if (ns == NULL)
  338. {
  339. return ERR_MEM;
  340. }
  341. ns->state = NETIO_STATE_WAIT_FOR_CMD;
  342. ns->data_len = 0;
  343. ns->cmd = 0;
  344. ns->cntr = 0;
  345. ns->buf_pos = 0;
  346. #if NETIO_USE_STATIC_BUF == 1
  347. ns->buf_ptr = netio_buf;
  348. #else
  349. ns->buf_ptr = mem_malloc(NETIO_BUF_SIZE);
  350. if (ns->buf_ptr == NULL)
  351. {
  352. mem_free(ns);
  353. return ERR_MEM;
  354. }
  355. #endif
  356. ns->buf_ptr[0] = 0;
  357. tcp_arg(pcb, ns);
  358. tcp_sent(pcb, netio_sent);
  359. tcp_recv(pcb, netio_recv);
  360. tcp_poll(pcb, netio_poll, 4); /* every 2 seconds */
  361. return ERR_OK;
  362. }
  363. void netio_init(void)
  364. {
  365. static rt_bool_t init_ok = RT_FALSE;
  366. struct tcp_pcb *pcb;
  367. if (!init_ok)
  368. {
  369. pcb = tcp_new();
  370. tcp_bind(pcb, IP_ADDR_ANY, 18767);
  371. pcb = tcp_listen(pcb);
  372. tcp_accept(pcb, netio_accept);
  373. init_ok = RT_TRUE;
  374. rt_kprintf("NetIO server start successfully\n");
  375. }
  376. else
  377. {
  378. rt_kprintf("netio: server already running\n");
  379. }
  380. }
  381. #endif /* LWIP_TCP */
  382. #ifdef RT_USING_FINSH
  383. #include <finsh.h>
  384. FINSH_FUNCTION_EXPORT(netio_init, netio server);
  385. #ifdef FINSH_USING_MSH
  386. MSH_CMD_EXPORT(netio_init, netio server);
  387. #endif /* FINSH_USING_MSH */
  388. #endif /* RT_USING_FINSH */
  389. #endif /* PKG_NETUTILS_NETIO */