use_rawapi.c

来自「如果你把LwIP移植到无操作系统的平台」· C语言 代码 · 共 87 行

C
87
字号

INT16S SendData(SOCKET channel, INT8U *buf, INT16S len)
{	
    if (len <= 0) 
	return (0);

    tcp_write(gpcb, buf, len, 0);
    tcp_output(gpcb);
    return 1;
}


err_t tcprecv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
    BCExecutableCmd *pExecutableCmd;
    INT32S uiSize;
    INT16U i;
	
	if (err == ERR_OK && p != NULL)
	{
		tcp_recved(pcb, p->tot_len);
		gpcb = pcb;
	    uiSize = p->tot_len;
	    for (i = 0; i < uiSize; i++)
	    	g_ucTmpData[i] = ((INT8U*)p->payload)[i];
	   
	    if (uiSize > 0)
	    {
	        if ( gSystem.ulSysStatus[gSystem.ulSysStatusCur] != STATUS_GET_DATA)
	        {
	            pExecutableCmd = (BCExecutableCmd *)g_ucTmpData;

	            if (pExecutableCmd->usCmdFlag != EXECUTABLE_CMD)    //Is 0x55AA?
	            {   //Command error
	                printf("error!");
	            }
	            else
	                bsGetCmdAndData((INT8U *)&g_ucTmpData);
	        }
	        else
	            bcGetRunData(g_ucTmpData, uiSize);
	    }
	    
		pbuf_free(p);
	}
	else
		pbuf_free(p);
	pcb = gpcb;
	if (err == ERR_OK && p == NULL)
	{
		tcp_arg(pcb, NULL);
		tcp_sent(pcb, NULL);
		tcp_recv(pcb, NULL);
		tcp_close(pcb);
	}
	return ERR_OK;

}

err_t tcpaccept(void *arg, struct tcp_pcb *pcb, err_t err)
{	
	tcp_arg(pcb, NULL);
	tcp_sent(pcb, NULL);
	tcp_recv(pcb, tcprecv);
	return ERR_OK;
}

void TcpEcho_Thread(void)
{
	struct tcp_pcb *pcb;
	
	// Create a new TCP PCB. 
	pcb = tcp_new();
	
	// Bind the PCB to TCP port 80. 
	tcp_bind(pcb, NULL, 700);
	
	// Change TCP state to LISTEN. 
	pcb = tcp_listen(pcb);
	
	// Set up http_accet() function to be called
	//when a new connection arrives.
	tcp_accept(pcb, tcpaccept);
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?