50 static struct tcp_pcb *echo_pcb;
69 err_t echo_accept(
void *arg,
struct tcp_pcb *newpcb,
err_t err);
70 err_t echo_recv(
void *arg,
struct tcp_pcb *tpcb,
struct pbuf *p,
err_t err);
71 void echo_error(
void *arg,
err_t err);
72 err_t echo_poll(
void *arg,
struct tcp_pcb *tpcb);
74 void echo_send(
struct tcp_pcb *tpcb,
struct echo_state *es);
75 void echo_close(
struct tcp_pcb *tpcb,
struct echo_state *es);
88 echo_pcb = tcp_listen(echo_pcb);
89 tcp_accept(echo_pcb, echo_accept);
104 echo_accept(
void *arg,
struct tcp_pcb *newpcb,
err_t err)
107 struct echo_state *es;
113 tcp_setprio(newpcb, TCP_PRIO_MIN);
115 es = (
struct echo_state *)
mem_malloc(
sizeof(
struct echo_state));
118 es->state = ES_ACCEPTED;
124 tcp_recv(newpcb, echo_recv);
125 tcp_err(newpcb, echo_error);
126 tcp_poll(newpcb, echo_poll, 0);
137 echo_recv(
void *arg,
struct tcp_pcb *tpcb,
struct pbuf *p,
err_t err)
139 struct echo_state *es;
143 es = (
struct echo_state *)arg;
147 es->state = ES_CLOSING;
151 echo_close(tpcb, es);
156 tcp_sent(tpcb, echo_sent);
171 else if(es->state == ES_ACCEPTED)
174 es->state = ES_RECEIVED;
178 tcp_sent(tpcb, echo_sent);
182 else if (es->state == ES_RECEIVED)
188 tcp_sent(tpcb, echo_sent);
201 else if(es->state == ES_CLOSING)
221 echo_error(
void *arg,
err_t err)
223 struct echo_state *es;
227 es = (
struct echo_state *)arg;
235 echo_poll(
void *arg,
struct tcp_pcb *tpcb)
238 struct echo_state *es;
240 es = (
struct echo_state *)arg;
246 tcp_sent(tpcb, echo_sent);
252 if(es->state == ES_CLOSING)
254 echo_close(tpcb, es);
269 echo_sent(
void *arg,
struct tcp_pcb *tpcb,
u16_t len)
271 struct echo_state *es;
275 es = (
struct echo_state *)arg;
281 tcp_sent(tpcb, echo_sent);
287 if(es->state == ES_CLOSING)
289 echo_close(tpcb, es);
296 echo_send(
struct tcp_pcb *tpcb,
struct echo_state *es)
301 while ((wr_err ==
ERR_OK) &&
303 (es->p->len <= tcp_sndbuf(tpcb)))
308 wr_err = tcp_write(tpcb, ptr->
payload, ptr->
len, 1);
330 tcp_recved(tpcb, plen);
345 echo_close(
struct tcp_pcb *tpcb,
struct echo_state *es)
348 tcp_sent(tpcb,
NULL);
349 tcp_recv(tpcb,
NULL);
351 tcp_poll(tpcb,
NULL, 0);