📄 echo.c
字号:
/* * Copyright (c) 2001-2003 Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY * OF SUCH DAMAGE. * * This file is part of the lwIP TCP/IP stack. * * Author: Adam Dunkels <adam@sics.se> * */#include "lwip/debug.h"#include "lwip/stats.h"#include "lwip/tcp.h"#include "xparameters.h"#include "echo.h"struct echo_state { struct pbuf *p; u8_t failed;#define FAILED_MAX 8};/*---------------------------------------------------------------------------*/static voidecho_err(void *arg, err_t err){ struct echo_state *es = arg; xil_printf("echo_err\r\n"); if(arg != NULL) { pbuf_free(es->p); mem_free(arg); }}/*---------------------------------------------------------------------------*/static voidclose_conn(struct tcp_pcb *pcb, struct echo_state *es){ tcp_arg(pcb, NULL); tcp_sent(pcb, NULL); tcp_recv(pcb, NULL); if(es != NULL) { pbuf_free(es->p); mem_free(es); } tcp_close(pcb); xil_printf("Connection Closed \r\n");}/*---------------------------------------------------------------------------*/static voidsend_buf(struct tcp_pcb *pcb, struct echo_state *es){ struct pbuf *q; do { q = es->p; es->p = pbuf_dechain(q); if(tcp_write(pcb, q->payload, q->len, 1) == ERR_MEM) { pbuf_chain(q, es->p); es->p = q; return; } tcp_recved(pcb, q->len); pbuf_free(q); } while(es->p != NULL); }/*---------------------------------------------------------------------------*/static err_techo_poll(void *arg, struct tcp_pcb *pcb){ struct echo_state *es; if(arg == NULL) { return tcp_close(pcb); } es = arg; if(es->failed >= FAILED_MAX) { close_conn(pcb, es); tcp_abort(pcb); return ERR_ABRT; } if(es->p != NULL) { ++es->failed; send_buf(pcb, es); } return ERR_OK;}/*---------------------------------------------------------------------------*/static err_techo_sent(void *arg, struct tcp_pcb *pcb, u16_t len){ struct echo_state *es; es = arg; if(es != NULL && es->p != NULL) { send_buf(pcb, es); } return ERR_OK;}/*---------------------------------------------------------------------------*/static err_techo_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err){ struct echo_state *es; es = arg; if(p == NULL) { close_conn(pcb, es); return ERR_OK; } if(es->p != NULL) { pbuf_chain(es->p, p); } else { es->p = p; } send_buf(pcb, es); return ERR_OK;}/*---------------------------------------------------------------------------*/static err_techo_accept(void *arg, struct tcp_pcb *pcb, err_t err){ struct echo_state *es; tcp_setprio(pcb, TCP_PRIO_MIN); /* Allocate memory for the structure that holds the state of the connection. */ es = mem_malloc(sizeof(struct echo_state)); if(es == NULL) { xil_printf("could not malloc memory for echo_state\r\n"); return ERR_MEM; } /* Initialize the structure. */ es->p = NULL; es->failed = 0; /* Tell TCP that this is the structure we wish to be passed for our callbacks. */ tcp_arg(pcb, es); /* Tell TCP that we wish to be informed of incoming data by a call to the http_recv() function. */ tcp_recv(pcb, echo_recv); tcp_err(pcb, echo_err); tcp_poll(pcb, echo_poll, 1); xil_printf("Connection Established\r\n"); return ERR_OK;}/*---------------------------------------------------------------------------*/voidecho_init(void){ struct tcp_pcb *pcb; pcb = tcp_new(); tcp_bind(pcb, IP_ADDR_ANY, ECHO_PORT); pcb = tcp_listen(pcb); tcp_accept(pcb, echo_accept);}/*---------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -