TCP Raw API questions about efficiency and threads

本文详细介绍了如何高效使用LwIP的RAW API进行多连接处理,避免数据复制,合理利用回调函数如recv和poll来处理大量数据,以及如何在recv中启动发送过程并在poll回调中继续,实现数据处理与发送的分离,提高网络应用的效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://lwip.100.n7.nabble.com/TCP-Raw-API-questions-about-efficiency-and-threads-td26473.html

 

Hi,

 

LwIP RAW API is simple once you understand it.

 

You do not create separate threads, all the connections are handled in one thread !

 

The way to have multiple connections is to add a user ARG value. Every connection has

its own PCB. Meaning for every connection the TCP stack will allocate a new PCB and when

it calls your accept cal-back you use this PCB to differentiate from other connections.

 

Also inside your accept call-back you can allocate your own structure and assign it to the

new connection. You assigns it to ARG !

 

Inside recv or any other call-back you simply read the ARG parameter and use it to differentiate

the connections.

 

You can either allocate your structure from the TCP memory or reallocate it statically and define

a maximum connection that you will be handling.

 

In order to use RAW API efficiently you can use the poll call back to send more data. What do I 

mean by that ?

 

Lets assume you got some request for data and you are handling it inside the recv function.

If you send back all the data you are busy inside that recv instant and blocking other parallel

connections. One way to avoid this is to start sending (a portion) and get out of the recv. The rest of the send will be handled inside the poll function. the poll function is called periodically as long as the

current connection is a live... house keeping.

 

Once you no longer need that connection can gracefully close it from inside the poll function.

 

Another way to use the poll call back is to put some data in a queue (from main thread) while from 

inside the poll call back (TCP own thread) read that data and send it to the open connection.

 

Regarding the ping... 200-300 ms is very slow even on the WAN. You are doing something wrong.

 

Check for the examples that are around and get some ideas how implement your code.

 

Hope that above helped.

 

BR,

Noam.

 

Dear,

 

I am sorry if I was not clear but you should not do what you did. No need to copy the data and process it

In the poll function.

 

I meant that if you get a request (like in HTTP) and you need to send lots of data you start the send process it in the

recv and continue in the poll call-back, if needed.

 

You should cycle all the data in the chain in one go (inside recv). Once you handled all the data you inform

the TCP stack that you have finished processing and exit.

 

Something like this (TCP):

 

  // take the first pBuf address and process the chain

  pBufChain = pBuf;

 

  do

  {    

 

      // process data…..

 

  } while((pBufChain = pBufChain->next) != NULL);

 

 

If data can be spread over several buffers you may need copy and concatenate data inside the loop.

 

Do remember that you have pBufChain->len and   pBuf->tot_len… the first is the chunk length and the

second is the total length of all the chained buffers, if any.

 

Once you are done processing the data you inform the TCP stack you are done:

 

// we need to free the pBuf

  if(pBuf != NULL)

  {

    // Inform TCP that we have taken the data.

    tcp_recved(pcb, pBuf->tot_len);       

    

    // free the buffer !

    pbuf_free(pBuf);

    pBuf = NULL;

  } 

 

 

If it is UDP you are not calling  tcp_recved  but data handling and freeing is similar.

 

Here are a few link that can give you more information and examples:

 

https://github.com/lsgunth

https://github.com/pabigot/lwip-contrib/tree/master/apps

 

 

Also take your time and read the rawapi.txt under the LwIP doc directory

 

 

Hope the above helped.

 

BR,

Noam.

 

 

 

 

 

 

From: lwip-users [mailto:lwip-users-bounces+noam=[hidden email]On Behalf Of P?rvu Mihai
Sent: Tuesday, June 07, 2016 3:44 PM
To: Mailing list for lwIP users
Subject: Re: [lwip-users] TCP Raw API questions about efficiency and threads

内容概要:本文介绍了奕斯伟科技集团基于RISC-V架构开发的EAM2011芯片及其应用研究。EAM2011是一款高性能实时控制芯片,支持160MHz主频和AI算法,符合汽车电子AEC-Q100 Grade 2和ASIL-B安全标准。文章详细描述了芯片的关键特性、配套软件开发套件(SDK)和集成开发环境(IDE),以及基于该芯片的ESWINEBP3901开发板的硬件资源和接口配置。文中提供了详细的代码示例,涵盖时钟配置、GPIO控制、ADC采样、CAN通信、PWM输出及RTOS任务创建等功能实现。此外,还介绍了硬件申领流程、技术资料获取渠道及开发建议,帮助开发者高效启动基于EAM2011芯片的开发工作。 适合人群:具备嵌入式系统开发经验的研发人员,特别是对RISC-V架构感兴趣的工程师和技术爱好者。 使用场景及目标:①了解EAM2011芯片的特性和应用场景,如智能汽车、智能家居和工业控制;②掌握基于EAM2011芯片的开发板和芯片的硬件资源和接口配置;③学习如何实现基本的外设驱动,如GPIO、ADC、CAN、PWM等;④通过RTOS任务创建示例,理解多任务处理和实时系统的实现。 其他说明:开发者可以根据实际需求扩展这些基础功能。建议优先掌握《EAM2011参考手册》中的关键外设寄存器配置方法,这对底层驱动开发至关重要。同时,注意硬件申领的时效性和替代方案,确保开发工作的顺利进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值