netio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 "lwip/opt.h"
  33. #if LWIP_TCP
  34. #include <rtthread.h>
  35. #include "lwip/tcp.h"
  36. #define DBG_SECTION_NAME "netio"
  37. #define DBG_LEVEL DBG_INFO
  38. #include <rtdbg.h>
  39. /*
  40. * This implements a netio server.
  41. * The client sends a command word (4 bytes) then a data length word (4 bytes).
  42. * If the command is "receive", the server is to consume "data length" bytes into
  43. * a circular buffer until the first byte is non-zero, then it is to consume
  44. * another command/data pair.
  45. * If the command is "send", the server is to send "data length" bytes from a circular
  46. * buffer with the first byte being zero, until "some time" (6 seconds in the
  47. * current netio126.zip download) has passed and then send one final buffer with
  48. * the first byte being non-zero. Then it is to consume another command/data pair.
  49. */
  50. /* See http://www.nwlab.net/art/netio/netio.html to get the netio tool */
  51. /* implementation options */
  52. #define NETIO_BUF_SIZE (4 * 1024)
  53. #define NETIO_USE_STATIC_BUF 0
  54. /* NetIO server state definition */
  55. #define NETIO_STATE_WAIT_FOR_CMD 0
  56. #define NETIO_STATE_RECV_DATA 1
  57. #define NETIO_STATE_SEND_DATA 2
  58. #define NETIO_STATE_SEND_DATA_LAST 3
  59. #define NETIO_STATE_DONE 4
  60. struct netio_state
  61. {
  62. u32_t state;
  63. u32_t cmd;
  64. u32_t data_len;
  65. u32_t cntr;
  66. u8_t *buf_ptr;
  67. u32_t buf_pos;
  68. u32_t first_byte;
  69. u32_t time_stamp;
  70. };
  71. /* NetIO command protocol definition */
  72. #define NETIO_CMD_QUIT 0
  73. #define NETIO_CMD_C2S 1
  74. #define NETIO_CMD_S2C 2
  75. #define NETIO_CMD_RES 3
  76. static err_t netio_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
  77. static void
  78. netio_close(void *arg, struct tcp_pcb *pcb)
  79. {
  80. err_t err;
  81. struct netio_state *ns = arg;
  82. ns->state = NETIO_STATE_DONE;
  83. tcp_recv(pcb, NULL);
  84. err = tcp_close(pcb);
  85. if (err != ERR_OK)
  86. {
  87. /* closing failed, try again later */
  88. tcp_recv(pcb, netio_recv);
  89. }
  90. else
  91. {
  92. /* closing succeeded */
  93. #if NETIO_USE_STATIC_BUF != 1
  94. if (ns->buf_ptr != NULL)
  95. {
  96. mem_free(ns->buf_ptr);
  97. }
  98. #endif
  99. tcp_arg(pcb, NULL);
  100. tcp_poll(pcb, NULL, 0);
  101. tcp_sent(pcb, NULL);
  102. if (arg != NULL)
  103. {
  104. mem_free(arg);
  105. }
  106. }
  107. }
  108. static err_t
  109. netio_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
  110. {
  111. struct netio_state *ns = arg;
  112. u8_t *data_ptr;
  113. u32_t data_cntr;
  114. struct pbuf *q = p;
  115. u16_t len;
  116. if (p != NULL)
  117. {
  118. tcp_recved(pcb, p->tot_len);
  119. }
  120. if (err == ERR_OK && q != NULL)
  121. {
  122. while (q != NULL)
  123. {
  124. data_cntr = q->len;
  125. data_ptr = q->payload;
  126. while (data_cntr--)
  127. {
  128. if (ns->state == NETIO_STATE_DONE)
  129. {
  130. netio_close(ns, pcb);
  131. break;
  132. }
  133. else if (ns->state == NETIO_STATE_WAIT_FOR_CMD)
  134. {
  135. if (ns->cntr < 4)
  136. {
  137. /* build up the CMD field */
  138. ns->cmd <<= 8;
  139. ns->cmd |= *data_ptr++;
  140. ns->cntr++;
  141. }
  142. else if (ns->cntr < 8)
  143. {
  144. /* build up the DATA field */
  145. ns->data_len <<= 8;
  146. ns->data_len |= *data_ptr++;
  147. ns->cntr++;
  148. if (ns->cntr == 8)
  149. {
  150. /* now we have full command and data words */
  151. ns->cntr = 0;
  152. ns->buf_pos = 0;
  153. ns->buf_ptr[0] = 0;
  154. if (ns->cmd == NETIO_CMD_C2S)
  155. {
  156. ns->state = NETIO_STATE_RECV_DATA;
  157. }
  158. else if (ns->cmd == NETIO_CMD_S2C)
  159. {
  160. ns->state = NETIO_STATE_SEND_DATA;
  161. /* start timer */
  162. ns->time_stamp = rt_tick_get();
  163. /* send first round of data */
  164. len = tcp_sndbuf(pcb);
  165. len = LWIP_MIN(len, ns->data_len - ns->cntr);
  166. len = LWIP_MIN(len, NETIO_BUF_SIZE - ns->buf_pos);
  167. do
  168. {
  169. err = tcp_write(pcb, ns->buf_ptr + ns->buf_pos, len, TCP_WRITE_FLAG_COPY);
  170. if (err == ERR_MEM)
  171. {
  172. len /= 2;
  173. }
  174. }
  175. while ((err == ERR_MEM) && (len > 1));
  176. ns->buf_pos += len;
  177. ns->cntr += len;
  178. }
  179. else
  180. {
  181. /* unrecognized command, punt */
  182. ns->cntr = 0;
  183. ns->buf_pos = 0;
  184. ns->buf_ptr[0] = 0;
  185. netio_close(ns, pcb);
  186. break;
  187. }
  188. }
  189. }
  190. else
  191. {
  192. /* in trouble... shouldn't be in this state! */
  193. }
  194. }
  195. else if (ns->state == NETIO_STATE_RECV_DATA)
  196. {
  197. int chunk_length;
  198. if (ns->cntr == 0)
  199. {
  200. /* save the first byte of this new round of data
  201. * this will not match ns->buf_ptr[0] in the case that
  202. * NETIO_BUF_SIZE is less than ns->data_len.
  203. */
  204. ns->first_byte = *data_ptr;
  205. }
  206. if (ns->cntr + (data_cntr + 1) < ns->data_len) chunk_length = data_cntr + 1;
  207. else chunk_length = (ns->data_len - ns->cntr);
  208. ns->buf_pos += chunk_length;
  209. data_ptr += chunk_length;
  210. ns->cntr += chunk_length;
  211. data_cntr -= (chunk_length - 1);
  212. if (ns->buf_pos >= NETIO_BUF_SIZE)
  213. {
  214. /* circularize the buffer */
  215. ns->buf_pos %= NETIO_BUF_SIZE;
  216. }
  217. if (ns->cntr == ns->data_len)
  218. {
  219. ns->cntr = 0;
  220. if (ns->first_byte != 0)
  221. {
  222. /* if this last round did not start with 0,
  223. * go look for another command */
  224. ns->state = NETIO_STATE_WAIT_FOR_CMD;
  225. ns->data_len = 0;
  226. ns->cmd = 0;
  227. /* TODO LWIP_DEBUGF( print out some throughput calculation results... ); */
  228. }
  229. else
  230. {
  231. /* stay here and wait on more data */
  232. }
  233. }
  234. }
  235. else if (ns->state == NETIO_STATE_SEND_DATA
  236. || ns->state == NETIO_STATE_SEND_DATA_LAST)
  237. {
  238. /* I don't think this should happen... */
  239. }
  240. else
  241. {
  242. /* done / quit */
  243. netio_close(ns, pcb);
  244. break;
  245. } /* end of ns->state condition */
  246. } /* end of while data still in this pbuf */
  247. q = q->next;
  248. }
  249. pbuf_free(p);
  250. }
  251. else
  252. {
  253. /* error or closed by other side */
  254. if (p != NULL)
  255. {
  256. pbuf_free(p);
  257. }
  258. /* close the connection */
  259. netio_close(ns, pcb);
  260. }
  261. return ERR_OK;
  262. }
  263. static err_t
  264. netio_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
  265. {
  266. struct netio_state *ns = arg;
  267. err_t err = ERR_OK;
  268. if (ns->cntr >= ns->data_len && ns->state == NETIO_STATE_SEND_DATA)
  269. {
  270. /* done with this round of sending */
  271. ns->buf_pos = 0;
  272. ns->cntr = 0;
  273. /* check if timer expired */
  274. if (rt_tick_get() - ns->time_stamp > 600)
  275. {
  276. ns->buf_ptr[0] = 1;
  277. ns->state = NETIO_STATE_SEND_DATA_LAST;
  278. }
  279. else
  280. {
  281. ns->buf_ptr[0] = 0;
  282. }
  283. }
  284. if (ns->state == NETIO_STATE_SEND_DATA_LAST || ns->state == NETIO_STATE_SEND_DATA)
  285. {
  286. len = tcp_sndbuf(pcb);
  287. len = LWIP_MIN(len, ns->data_len - ns->cntr);
  288. len = LWIP_MIN(len, NETIO_BUF_SIZE - ns->buf_pos);
  289. if (ns->cntr < ns->data_len)
  290. {
  291. do
  292. {
  293. err = tcp_write(pcb, ns->buf_ptr + ns->buf_pos, len, TCP_WRITE_FLAG_COPY);
  294. if (err == ERR_MEM)
  295. {
  296. len /= 2;
  297. }
  298. }
  299. while ((err == ERR_MEM) && (len > 1));
  300. ns->buf_pos += len;
  301. if (ns->buf_pos >= NETIO_BUF_SIZE)
  302. {
  303. ns->buf_pos = 0;
  304. }
  305. ns->cntr += len;
  306. }
  307. }
  308. if (ns->cntr >= ns->data_len && ns->state == NETIO_STATE_SEND_DATA_LAST)
  309. {
  310. /* we have buffered up all our data to send this last round, go look for a command */
  311. ns->state = NETIO_STATE_WAIT_FOR_CMD;
  312. ns->cntr = 0;
  313. /* TODO LWIP_DEBUGF( print out some throughput calculation results... ); */
  314. }
  315. return ERR_OK;
  316. }
  317. static err_t
  318. netio_poll(void *arg, struct tcp_pcb *pcb)
  319. {
  320. struct netio_state *ns = arg;
  321. if (ns->state == NETIO_STATE_SEND_DATA)
  322. {
  323. }
  324. else if (ns->state == NETIO_STATE_DONE)
  325. {
  326. netio_close(ns, pcb);
  327. }
  328. return ERR_OK;
  329. }
  330. #if NETIO_USE_STATIC_BUF == 1
  331. static u8_t netio_buf[NETIO_BUF_SIZE];
  332. #endif
  333. static err_t
  334. netio_accept(void *arg, struct tcp_pcb *pcb, err_t err)
  335. {
  336. struct netio_state *ns;
  337. LWIP_UNUSED_ARG(err);
  338. ns = mem_malloc(sizeof(struct netio_state));
  339. if (ns == NULL)
  340. {
  341. return ERR_MEM;
  342. }
  343. ns->state = NETIO_STATE_WAIT_FOR_CMD;
  344. ns->data_len = 0;
  345. ns->cmd = 0;
  346. ns->cntr = 0;
  347. ns->buf_pos = 0;
  348. #if NETIO_USE_STATIC_BUF == 1
  349. ns->buf_ptr = netio_buf;
  350. #else
  351. ns->buf_ptr = mem_malloc(NETIO_BUF_SIZE);
  352. if (ns->buf_ptr == NULL)
  353. {
  354. mem_free(ns);
  355. return ERR_MEM;
  356. }
  357. #endif
  358. ns->buf_ptr[0] = 0;
  359. tcp_arg(pcb, ns);
  360. tcp_sent(pcb, netio_sent);
  361. tcp_recv(pcb, netio_recv);
  362. tcp_poll(pcb, netio_poll, 4); /* every 2 seconds */
  363. return ERR_OK;
  364. }
  365. static void netio_init(void)
  366. {
  367. static rt_bool_t init_ok = RT_FALSE;
  368. struct tcp_pcb *pcb;
  369. if (!init_ok)
  370. {
  371. pcb = tcp_new();
  372. tcp_bind(pcb, IP_ADDR_ANY, 18767);
  373. pcb = tcp_listen(pcb);
  374. tcp_accept(pcb, netio_accept);
  375. init_ok = RT_TRUE;
  376. LOG_I("netio server start successfully");
  377. }
  378. else
  379. {
  380. LOG_I("netio server has already run");
  381. }
  382. }
  383. #ifdef FINSH_USING_MSH
  384. #include <finsh.h>
  385. MSH_CMD_EXPORT_ALIAS(netio_init, netio, initalise netio server);
  386. #endif /* FINSH_USING_MSH */
  387. #endif /* LWIP_TCP */