/**@brief Function to TCP port. * * @details Function to close the TCP port and reset any information on the port. */ static void tcp_port_close(struct tcp_pcb * p_pcb) { m_tcp_state = TCP_STATE_REQUEST_CONNECTION; //Reset all information set on and/or callback registered for the port. tcp_arg(p_pcb, NULL); tcp_sent(p_pcb, NULL); tcp_recv(p_pcb, NULL); tcp_err(p_pcb, NULL); tcp_poll(p_pcb, NULL, 0); UNUSED_VARIABLE(tcp_close(p_pcb)); LEDS_OFF((DISPLAY_LED_0 | DISPLAY_LED_1 | DISPLAY_LED_2 | DISPLAY_LED_3)); LEDS_ON(CONNECTED_LED); }
static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es) { tcp_arg(tpcb, NULL); tcp_sent(tpcb, NULL); tcp_recv(tpcb, NULL); tcp_err(tpcb, NULL); tcp_poll(tpcb, NULL, 0); if (es != NULL) { mem_free(es); } tcp_close(tpcb); }
//lwIP TCP连接的回调函数 err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err) { if(err==ERR_OK) { //tcp_arg(tpcb,es); //ʹÓÃes¸üÐÂtpcbµÄcallback_arg tcp_recv(tpcb,tcp_client_recv); //初始化LwIP的tcp_recv回调功能 tcp_err(tpcb,tcp_client_error); //初始化tcp_err()回调函数 tcp_sent(tpcb,tcp_client_sent); //这里是当数据被server接受到了,那么菜调用这个回调函数 //tcp_poll(tpcb,tcp_client_poll,1); //初始化LwIP的tcp_poll回调功能 tcp_client_senddata(tpcb); }else { tcp_client_connection_close(tpcb);//关闭连接 } return err; }
/** * @brief This function is used to close the tcp connection with server * @param tpcb: tcp connection control block * @param es: pointer on echoclient structure * @retval None */ static void tcp_echoclient_connection_close(struct tcp_pcb *tpcb, struct echoclient * es ) { /* remove callbacks */ tcp_recv(tpcb, NULL); tcp_sent(tpcb, NULL); tcp_poll(tpcb, NULL,0); if (es != NULL) { mem_free(es); } /* close tcp connection */ tcp_close(tpcb); }
static err_t rfb_accept(void *arg, struct tcp_pcb *pcb, err_t err) { struct rfb_state *state; char * blockbuf; LWIP_UNUSED_ARG(arg); LWIP_UNUSED_ARG(err); state = (struct rfb_state *)mem_malloc(sizeof(struct rfb_state)); if (!state) { outputf("rfb_accept: out of memory\n"); return ERR_MEM; } memset(state, 0, sizeof(struct rfb_state)); blockbuf = mem_malloc(ceildiv(fb->curmode.xres, SCREEN_CHUNKS_X) * ceildiv(fb->curmode.yres, SCREEN_CHUNKS_Y) * 4); if (!blockbuf) { outputf("rfb_accept: out of memory allocating blockbuf\n"); mem_free(state); return ERR_MEM; } state->blockbuf = blockbuf; state->state = ST_BEGIN; state->send_state = SST_IDLE; /* XXX: update_server_info() should be called from the 64ms timer, and deal * with screen resizes appropriately. */ update_server_info(); tcp_arg(pcb, state); tcp_recv(pcb, rfb_recv); tcp_sent(pcb, rfb_sent); tcp_poll(pcb, rfb_poll, 1); /* tcp_err(pcb, rfb_err); */ tcp_write(pcb, "RFB 003.008\n", 12, 0); tcp_output(pcb); return ERR_OK; }
//------------------------------------------------------------------------------------------------------------------------------------------------------ err_t CallbackOnAccept(void *_arg, struct tcp_pcb *_newPCB, err_t _err) { err_t ret_err; struct State *s; LWIP_UNUSED_ARG(_arg); LWIP_UNUSED_ARG(_err); /* Unless this pcb should have NORMAL priority, set its priority now. When running out of pcbs, low priority pcbs can be aborted to create new pcbs of higher priority. */ tcp_setprio(_newPCB, TCP_PRIO_MIN); s = (struct State*)mem_malloc(sizeof(struct State)); if (s) { s->state = S_ACCEPTED; s->numPort = ((unsigned short)POLICY_PORT == _newPCB->local_port) ? POLICY_PORT : DEFAULT_PORT; s->p = NULL; /* pass newly allocated s to our callbacks */ tcp_arg(_newPCB, s); tcp_recv(_newPCB, CallbackOnRecieve); tcp_err(_newPCB, CallbackOnError); tcp_poll(_newPCB, CallbackOnPoll, 0); tcp_sent(_newPCB, CallbackOnSent); ret_err = ERR_OK; if (s->numPort == DEFAULT_PORT) { if (pcbClient == 0) { pcbClient = _newPCB; SocketFuncConnect(); gEthIsConnected = true; } } } else { ret_err = ERR_MEM; } return ret_err; }
/*-----------------------------------------------------------------------------------*/ static void do_delconn(struct api_msg_msg *msg) { if (msg->conn->pcb.tcp != NULL) { switch (msg->conn->type) { #if LWIP_UDP case NETCONN_UDPLITE: /* FALLTHROUGH */ case NETCONN_UDPNOCHKSUM: /* FALLTHROUGH */ case NETCONN_UDP: msg->conn->pcb.udp->recv_arg = NULL; udp_remove(msg->conn->pcb.udp); break; #endif /* LWIP_UDP */ #if LWIP_TCP case NETCONN_TCP: if (msg->conn->pcb.tcp->state == LISTEN) { tcp_arg(msg->conn->pcb.tcp, NULL); tcp_accept(msg->conn->pcb.tcp, NULL); tcp_close(msg->conn->pcb.tcp); } else { tcp_arg(msg->conn->pcb.tcp, NULL); tcp_sent(msg->conn->pcb.tcp, NULL); tcp_recv(msg->conn->pcb.tcp, NULL); tcp_poll(msg->conn->pcb.tcp, NULL, 0); tcp_err(msg->conn->pcb.tcp, NULL); if (tcp_close(msg->conn->pcb.tcp) != ERR_OK) { tcp_abort(msg->conn->pcb.tcp); } } #endif default: break; } } /* Trigger select() in socket layer */ if (msg->conn->callback) { (*msg->conn->callback)(msg->conn, NETCONN_EVT_RCVPLUS, 0); (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDPLUS, 0); } if (msg->conn->mbox != SYS_MBOX_NULL) { sys_mbox_post(msg->conn->mbox, NULL); } }
/* * \brief Callback from LWIP when a client connects to our TCP listen sock */ static err_t accept_cb(void *arg, struct tcp_pcb *tpcb, err_t err) { printf("bfscope: connected\n"); assert(err == ERR_OK); tcp_recv(tpcb, recv_cb); tcp_sent(tpcb, send_cb); tcp_err(tpcb, error_cb); tcp_arg(tpcb, (void*)tpcb); tcp_accepted(tpcb); bfscope_client = tpcb; return ERR_OK; }
// new client accepted err_t server_newclient(void *arg, struct tcp_pcb *pcb, err_t err) { struct client *c = mem_malloc(sizeof *c); // XXX should be pooled! if (!c) return ERR_MEM; c->st = ACCEPTED; c->pcb = pcb; c->p = NULL; // hook up callbacks tcp_arg(pcb, c); tcp_recv(pcb, client_recv); // on data received tcp_err(pcb, client_err); // on error tcp_poll(pcb, client_poll, 1); // poll status tcp_sent(pcb, client_sent); // queued data sent return ERR_OK; }
static err_t txperf_connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err) { xil_printf("txperf: Connected to iperf server\r\n"); txperf_client_connected = 1; /* store state */ connected_pcb = tpcb; /* set callback values & functions */ tcp_arg(tpcb, NULL); tcp_sent(tpcb, txperf_sent_callback); //xil_printf("txperf_connected_callback: ok\r\n"); /* initiate data transfer */ return ERR_OK; }
/*..........................................................................*/ static void close_conn(struct tcp_pcb *pcb, struct server_state *hs) { err_t err; // DEBUG_PRINT("Closing connection 0x%08x\n", pcb); tcp_arg(pcb, NULL); tcp_sent(pcb, NULL); tcp_recv(pcb, NULL); if (hs->buf) mem_free(hs->buf); mem_free(hs); err = tcp_close(pcb); // if (err != ERR_OK) // DEBUG_PRINT("Error %d closing 0x%08x\n", err, pcb); }
static void TCP_close(struct tcp_pcb *tpcb, TCP *es) { tcp_arg(tpcb, es->io); /* remove all callbacks */ tcp_sent(tpcb, NULL); tcp_recv(tpcb, NULL); tcp_err(tpcb, NULL); tcp_poll(tpcb, NULL, 0); if (es != NULL) { /* delete es structure */ _thread_remove(es->f,es->io); _thread_remove(_tcp_flush,es); if(es->io) _io_close(es->io); mem_free(es); } tcp_close(tpcb); /* close tcp connection */ }
static void ftpd_msgclose(struct tcp_pcb *pcb, struct ftpd_msgstate *fsm) { tcp_arg(pcb, NULL); tcp_sent(pcb, NULL); tcp_recv(pcb, NULL); if (fsm->datafs) ftpd_dataclose(fsm->datapcb, fsm->datafs); sfifo_close(&fsm->fifo); vfs_close(fsm->vfs); fsm->vfs = NULL; if (fsm->renamefrom) free(fsm->renamefrom); fsm->renamefrom = NULL; free(fsm); tcp_arg(pcb, NULL); tcp_close(pcb); }
static err_t newConnection(void *arg, struct tcp_pcb *newpcb, err_t err) { err_t error; printf("new connection: %p\n", newpcb); if (connectedPCB != NULL){ printf("too many connections\n" ); error = tcp_close(newpcb); if (error != ERR_OK){ printf("Unable to close the connection: %d\n", error); } } connectedPCB = newpcb; tcp_sent(newpcb, sent); tcp_recv(newpcb,recv ); tcp_accepted(newpcb); return ERR_OK; }
void CloseConnection(struct tcp_pcb *tpcb, struct State *ss) { gEthIsConnected = false; tcp_arg(tpcb, NULL); tcp_sent(tpcb, NULL); tcp_recv(tpcb, NULL); tcp_err(tpcb, NULL); tcp_poll(tpcb, NULL, 0); pcbClient = 0; if (ss) { mem_free(ss); } tcp_close(tpcb); }
/** * A new incoming connection has been accepted. */ static err_t http_accept(void *arg, struct tcp_pcb *pcb, err_t err) { struct http_state *hs; struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen*)arg; LWIP_UNUSED_ARG(err); LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept(%p)\n", arg)); /* Decrease the listen backlog counter */ tcp_accepted(lpcb); tcp_setprio(pcb, HTTPD_TCP_PRIO); /* Allocate memory for the structure that holds the state of the connection. */ hs = http_state_alloc(); if (hs == NULL) { LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept: Out of memory, RST\n")); return ERR_MEM; } /* Initialize the structure. */ hs->file = NULL; #if HTTPD_SUPPORT_DYNAMIC_PAGES hs->file_orig = NULL; #endif /* HTTPD_SUPPORT_DYNAMIC_PAGES */ hs->left = 0; hs->retries = 0; #if HTTPD_TRACK_SENT_BYTES hs->sent_total = 0; #endif /* HTTPD_TRACK_SENT_BYTES */ /* Tell TCP that this is the structure we wish to be passed for our callbacks. */ tcp_arg(pcb, hs); /* Set up the various callback functions */ tcp_recv(pcb, http_recv); tcp_err(pcb, http_err); tcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL); tcp_sent(pcb, http_sent); return ERR_OK; }
/** Start TCP connection back to the client (either parallel or after the * receive test has finished. */ static err_t lwiperf_tx_start(lwiperf_state_tcp_t* conn) { err_t err; lwiperf_state_tcp_t* client_conn; struct tcp_pcb* newpcb; ip_addr_t remote_addr; u16_t remote_port; client_conn = (lwiperf_state_tcp_t*)LWIPERF_ALLOC(lwiperf_state_tcp_t); if (client_conn == NULL) { return ERR_MEM; } newpcb = tcp_new(); if (newpcb == NULL) { LWIPERF_FREE(lwiperf_state_tcp_t, client_conn); return ERR_MEM; } memcpy(client_conn, conn, sizeof(lwiperf_state_tcp_t)); client_conn->base.server = 0; client_conn->server_pcb = NULL; client_conn->conn_pcb = newpcb; client_conn->time_started = sys_now(); /* TODO: set this again on 'connected' */ client_conn->poll_count = 0; client_conn->next_num = 4; /* initial nr is '4' since the header has 24 byte */ client_conn->bytes_transferred = 0; client_conn->settings.flags = 0; /* prevent the remote side starting back as client again */ tcp_arg(newpcb, client_conn); tcp_sent(newpcb, lwiperf_tcp_client_sent); tcp_poll(newpcb, lwiperf_tcp_poll, 2U); tcp_err(newpcb, lwiperf_tcp_err); ip_addr_copy(remote_addr, conn->conn_pcb->remote_ip); remote_port = (u16_t)htonl(client_conn->settings.remote_port); err = tcp_connect(newpcb, &remote_addr, remote_port, lwiperf_tcp_client_connected); if (err != ERR_OK) { lwiperf_tcp_close(client_conn, LWIPERF_TCP_ABORTED_LOCAL); return err; } lwiperf_list_add(&client_conn->base); return ERR_OK; }
/** * @brief This function implements the tcp_sent LwIP callback (called when ACK * is received from remote host for sent data) * @param None * @retval None */ static err_t TCP_sent(void *arg, struct tcp_pcb *tpcb, u16_t len) { TCP *es; LWIP_UNUSED_ARG(len); es = (TCP *)arg; if (es->tx != NULL) { /* still got pbufs to send */ tcp_sent(es->pcb, TCP_sent); TCP_send(es->pcb, es); } else { /* if no more data to send and client closed connection*/ if (es->state == ES_CLOSING) { TCP_close(tpcb, es); } } return ERR_OK; }
/****************************************************************************** * FunctionName : espconn_client_connect * Description : A new incoming connection has been connected. * Parameters : arg -- Additional argument to pass to the callback function * tpcb -- The connection pcb which is connected * err -- An unused error code, always ERR_OK currently * Returns : connection result *******************************************************************************/ static err_t ICACHE_FLASH_ATTR espconn_client_connect(void *arg, struct tcp_pcb *tpcb, err_t err) { espconn_msg *pcon = arg; espconn_printf("espconn_client_connect pcon %p tpcb %p\n", pcon, tpcb); if (err == ERR_OK){ /*Reserve the remote information for current active connection*/ pcon->pespconn->state = ESPCONN_CONNECT; pcon->pcommon.err = err; pcon->pcommon.pcb = tpcb; pcon->pcommon.local_port = tpcb->local_port; pcon->pcommon.local_ip = tpcb->local_ip.u_addr.ip4.addr; pcon->pcommon.remote_port = tpcb->remote_port; pcon->pcommon.remote_ip[0] = ip4_addr1_16(&tpcb->remote_ip.u_addr.ip4); pcon->pcommon.remote_ip[1] = ip4_addr2_16(&tpcb->remote_ip.u_addr.ip4); pcon->pcommon.remote_ip[2] = ip4_addr3_16(&tpcb->remote_ip.u_addr.ip4); pcon->pcommon.remote_ip[3] = ip4_addr4_16(&tpcb->remote_ip.u_addr.ip4); pcon->pcommon.write_flag = true; tcp_arg(tpcb, (void *) pcon); /*Set the specify function that should be called * when TCP data has been successfully delivered, * when active connection receives data*/ tcp_sent(tpcb, espconn_client_sent); tcp_recv(tpcb, espconn_client_recv); /*Disable Nagle algorithm default*/ tcp_nagle_disable(tpcb); /*Default set the total number of espconn_buf on the unsent lists for one*/ espconn_tcp_set_buf_count(pcon->pespconn, 1); if (pcon->pespconn->proto.tcp->connect_callback != NULL) { pcon->pespconn->proto.tcp->connect_callback(pcon->pespconn); } /*Enable keep alive option*/ if (espconn_keepalive_disabled(pcon)) espconn_keepalive_enable(tpcb); } else{ printf("err in host connected (%s)\n",lwip_strerr(err)); } return err; }
void _tcp_flush(void *v) { TCP *es=v; if(es->pcb->snd_queuelen > TCP_SND_QUEUELEN-1) // !!!! pred vpisom preveri, ce ni queue ze poln tcp_output(es->pcb); // kratkih blokov (Nagle algoritem bo javil MEM error....) else if(es->io) { // sicer nadaljevanje... char c[256]; int k,n=0; if(es->rx) { // a je kaj za sprejem ??? struct pbuf *q; for(q=es->rx; q != NULL; q=es->rx) { // preskanirat je treba celo verigo pbuf n+=k=_buffer_push(es->io[0],q->payload, q->len); // push v io if(k < q->len) { pbuf_header(q,-k); // skrajsaj header break; } es->rx = es->rx->next; if (es->rx != NULL) pbuf_ref(es->rx); pbuf_free(q); } tcp_recved(es->pcb,n); // free raw input } n=_buffer_count(es->io[1]); // koliko je v buferju za izpis ... if(n > tcp_sndbuf(es->pcb)) // ne sme biti vec kot je placa na raw output .... n = tcp_sndbuf(es->pcb); if(n > 256) // ne sme bit vec kot 1024.... glej c[1024] n=256; if(n) { // ce je sploh kej ... struct pbuf *p = pbuf_alloc(PBUF_TRANSPORT, n , PBUF_POOL); if(p != NULL) { // ce je alokacija pbuf uspela n=_buffer_pull(es->io[1],c,n); // kopiraj pull vsebino v vmesni buffer pbuf_take(p,c,n); // formiraj pbuf if(es->tx) // verizi, ce je se kaj od prej.. pbuf_chain(es->tx,p); // else es->tx = p; // sicer nastavi nov pointer tcp_sent(es->pcb, TCP_sent); // set callback & send.. TCP_send(es->pcb, es); tcp_output(es->pcb); } } } }
void LwipNetTcpSocket::cleanUp() //Flush input buffer { //Ensure that further error won't be followed to this inst (which can be destroyed) if( m_pPcb ) { tcp_arg( (tcp_pcb*) m_pPcb, (void*) NULL ); tcp_recv( (tcp_pcb*) m_pPcb, NULL ); tcp_sent((tcp_pcb*) m_pPcb, NULL ); tcp_err( (tcp_pcb*) m_pPcb, NULL ); } if( m_pReadPbuf ) { DBG("Deallocating unread data.\n"); pbuf_free((pbuf*)m_pReadPbuf); //Free all unread data m_pReadPbuf = NULL; recv(NULL,0); //Update recv ptr position } }
static err_t mg_lwip_accept_cb(void *arg, struct tcp_pcb *newtpcb, err_t err) { struct mg_connection *lc = (struct mg_connection *) arg, *nc; union socket_address sa; DBG(("%p conn from %s:%u\n", lc, ipaddr_ntoa(&newtpcb->remote_ip), newtpcb->remote_port)); sa.sin.sin_addr.s_addr = newtpcb->remote_ip.addr; sa.sin.sin_port = htons(newtpcb->remote_port); nc = mg_if_accept_tcp_cb(lc, &sa, sizeof(sa.sin)); if (nc == NULL) { tcp_abort(newtpcb); return ERR_ABRT; } nc->sock = (int) newtpcb; tcp_arg(newtpcb, nc); tcp_err(newtpcb, mg_lwip_tcp_error_cb); tcp_sent(newtpcb, mg_lwip_tcp_sent_cb); tcp_recv(newtpcb, mg_lwip_tcp_recv_cb); return ERR_OK; }
static err_t tcp_is_connected_client(void *arg, struct tcp_pcb *pcb, err_t err) { if (err != ERR_OK) { fprintf(stderr, "tcp connection failed\n"); close_connection(pcb); return err; } tcp_sent(pcb, tcp_is_sent_client); tcp_recv(pcb, tcp_is_recv_client); tcp_err( pcb, tcp_is_err_client); tcp_poll(pcb, tcp_is_poll_client, 10); printf("tcp client connected\n"); handle_connection_opened(); return ERR_OK; }
static err_t ftpd_msgaccept(void *arg, struct tcp_pcb *pcb, err_t err) { struct ftpd_msgstate *fsm; /* Allocate memory for the structure that holds the state of the connection. */ fsm = malloc(sizeof(struct ftpd_msgstate)); if (fsm == NULL) { dbg_printf("ftpd_msgaccept: Out of memory\n"); return ERR_MEM; } memset(fsm, 0, sizeof(struct ftpd_msgstate)); /* Initialize the structure. */ sfifo_init(&fsm->fifo, 2000); fsm->state = FTPD_IDLE; fsm->vfs = vfs_openfs(); if (!fsm->vfs) { free(fsm); return ERR_CLSD; } /* Tell TCP that this is the structure we wish to be passed for our callbacks. */ tcp_arg(pcb, fsm); /* Tell TCP that we wish to be informed of incoming data by a call to the http_recv() function. */ tcp_recv(pcb, ftpd_msgrecv); /* Tell TCP that we wish be to informed of data that has been successfully sent by a call to the ftpd_sent() function. */ tcp_sent(pcb, ftpd_msgsent); tcp_err(pcb, ftpd_msgerr); tcp_poll(pcb, ftpd_msgpoll, 1); send_msg(pcb, fsm, msg220); return ERR_OK; }
// This function is called by both lwip_close and the fs_close handler. It // expects a socket #, not a VFS fd. static int close_common(int s) { // Make sure we have access mutex_lock(fds[s].mutex); // Close off any lwIP hooks if (fds[s].tcppcb) { struct tcp_pcb * pcb = fds[s].tcppcb; tcp_arg(pcb, NULL); if (pcb->state == LISTEN) { tcp_accept(pcb, NULL); tcp_close(pcb); } else { tcp_sent(pcb, NULL); tcp_recv(pcb, NULL); tcp_poll(pcb, NULL, 0); tcp_err(pcb, NULL); if (tcp_close(pcb) != ERR_OK) { tcp_abort(pcb); } } } if (fds[s].udppcb) { fds[s].udppcb->recv_arg = NULL; udp_remove(fds[s].udppcb); } // Free unanswered backlog connections as well if (fds[s].conns) { int i; for (i=0; i<fds[s].connmax; i++) { if (fds[s].conns[i] >= 0) close_common(fds[s].conns[i]); } free(fds[s].conns); } // Close the socket sock_close(s); return 0; }
/** * @brief This functions closes the tcp connection * @param tcp_pcb: pointer on the tcp connection * @param es: pointer on echo_state structure * @retval None */ static void tcp_echoserver_connection_close(struct tcp_pcb *tpcb, struct tcp_echoserver_struct *es) { /* remove all callbacks */ tcp_arg(tpcb, NULL); tcp_sent(tpcb, NULL); tcp_recv(tpcb, NULL); tcp_err(tpcb, NULL); tcp_poll(tpcb, NULL, 0); /* delete es structure */ if (es != NULL) { mem_free(es); } /* close tcp connection */ tcp_close(tpcb); }
static err_t tcp_is_connected(void *arg, struct tcp_pcb *pcb, err_t err) { // debug_printf("tcp connected\n"); if (err != ERR_OK) { fprintf(stderr, "tcp connection failed\n"); wait_cond = false; return err; } tcp_sent(pcb, tcp_is_sent); tcp_recv(pcb, tcp_is_recv); tcp_err( pcb, tcp_is_err); tcp_poll(pcb, tcp_is_poll, 10); wait_cond = false; return ERR_OK; }
/** * @brief This function is the implementation for tcp_recv LwIP callback * @param arg: pointer on a argument for the tcp_pcb connection * @param tpcb: pointer on the tcp_pcb connection * @param pbuf: pointer on the received pbuf * @param err: error information regarding the reveived pbuf * @retval err_t: error code */ static err_t TCP_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { err_t ret_err; TCP *es; LWIP_ASSERT("arg != NULL",arg != NULL); es = (TCP *)arg; if (p == NULL) { /* if we receive an empty tcp frame from client => close connection */ es->state = ES_CLOSING; /* remote host closed connection */ if (es->tx == NULL) { TCP_close(tpcb, es); /* we're done sending, close connection */ } else { /* we're not done yet */ tcp_sent(tpcb, TCP_sent); /* send remaining data*/ TCP_send(tpcb, es); /* acknowledge received packet */ } ret_err = ERR_OK; } else if(err != ERR_OK) { /* else : a non empty frame was received from client but for some reason err != ERR_OK */ if (p != NULL) { es->tx = NULL; pbuf_free(p); /* free received pbuf*/ } ret_err = err; } else if(es->state == ES_ACCEPTED) { /* first data chunk in p->payload */ es->state = ES_RECEIVED; es->rx = p; /* store reference to incoming pbuf (chain) */ ret_err = ERR_OK; } else if (es->state == ES_RECEIVED) { if (es->rx) pbuf_chain(es->rx,p); else es->rx = p; ret_err = ERR_OK; } else { /* data received when connection already closed */ tcp_recved(tpcb, p->tot_len); /* Acknowledge data reception */ es->tx = NULL; pbuf_free(p); /* free pbuf and do nothing */ ret_err = ERR_OK; } return ret_err; }
/*..........................................................................*/ static void close_conn(struct tcp_pcb *pcb, struct http_state *hs) { err_t err; DEBUG_PRINT("Closing connection 0x%08x\n", pcb); tcp_arg(pcb, NULL); tcp_sent(pcb, NULL); tcp_recv(pcb, NULL); if (hs->handle) { fs_close(hs->handle); hs->handle = NULL; } if (hs->buf) { mem_free(hs->buf); } mem_free(hs); err = tcp_close(pcb); if (err != ERR_OK) { DEBUG_PRINT("Error %d closing 0x%08x\n", err, pcb); } }
/** * Accept the client and resume accepting thread. */ static void tcp_accept_resume(struct socket_t *socket_p) { struct tcp_accept_args_t *args_p; struct tcp_pcb *pcb_p; fs_counter_increment(&module.tcp_accepts, 1); pcb_p = socket_p->input.u.accept.pcb_p; socket_p->input.u.accept.left = 0; socket_p->input.u.accept.pcb_p = NULL; args_p = socket_p->input.cb.args_p; tcp_arg(pcb_p, args_p->accepted_p); tcp_recv(pcb_p, on_tcp_recv); tcp_sent(pcb_p, on_tcp_sent); tcp_err(pcb_p, on_tcp_err); init(args_p->accepted_p, SOCKET_TYPE_STREAM, pcb_p); tcp_accepted(((struct tcp_pcb *)socket_p->pcb_p)); resume_thrd(socket_p->input.cb.thrd_p, 0); }