netio.c 10.0 KB

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