当前位置: 首页 > news >正文

【LwIP源码学习8】netbuf源码分析

netbuf结构体

netbuf是lwip中对接收和发送数据的更高一层封装,记录了数据发送方的IP地址和端口号,netbuf结构体如下:

/** "Network buffer" - contains data and addressing info */
struct netbuf {struct pbuf *p, *ptr;ip_addr_t addr;u16_t port;
};

其中addr是ip地址,port是端口号,ip地址对应的是一个设备,端口号对应一个设备里的一个线程。
指针p指向首个pbuf,是不可改变的,指针ptr可以通过函数netbuf_next()和函数netbuf_first()来移动。用函数netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)获取ptr指向的pbuf中数据的地址dataptr,并获取其数据量len
在这里插入图片描述

netbuf_new()

申请一个新的netbuf结构体内存空间并返回。

struct
netbuf *netbuf_new(void)
{struct netbuf *buf;buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);if (buf != NULL) {memset(buf, 0, sizeof(struct netbuf));}return buf;
}

netbuf_delete()

删除一个netbuf结构体内存空间,如果删除的netbufptrp指向的pbuf有数据,那么将对应的pbuf也释放掉。

void
netbuf_delete(struct netbuf *buf)
{if (buf != NULL) {if (buf->p != NULL) {pbuf_free(buf->p);buf->p = buf->ptr = NULL;}memp_free(MEMP_NETBUF, buf);}
}

netbuf_alloc()

netbufptrp指向的pbuf申请一块指定大小的内存空间。如果当前netbuf中的pbuf存在数据,则先把数据释放掉。

void *
netbuf_alloc(struct netbuf *buf, u16_t size)
{LWIP_ERROR("netbuf_alloc: invalid buf", (buf != NULL), return NULL;);/* Deallocate any previously allocated memory. */if (buf->p != NULL) {pbuf_free(buf->p);}buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);if (buf->p == NULL) {return NULL;}LWIP_ASSERT("check that first pbuf can hold size",(buf->p->len >= size));buf->ptr = buf->p;return buf->p->payload;
}

netbuf_free()

netbuf中的pbuf的内存空间释放掉。

void
netbuf_free(struct netbuf *buf)
{LWIP_ERROR("netbuf_free: invalid buf", (buf != NULL), return;);if (buf->p != NULL) {pbuf_free(buf->p);}buf->p = buf->ptr = NULL;
#if LWIP_CHECKSUM_ON_COPYbuf->flags = 0;buf->toport_chksum = 0;
#endif /* LWIP_CHECKSUM_ON_COPY */
}

netbuf_ref()

netbuf_alloc()类似,只不过这个是申请一个空的pbuf,然后pbuf的数据指针payload指向用户传入的数据地址。
reference:引用,指向。

err_t
netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size)
{LWIP_ERROR("netbuf_ref: invalid buf", (buf != NULL), return ERR_ARG;);if (buf->p != NULL) {pbuf_free(buf->p);}buf->p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF);if (buf->p == NULL) {buf->ptr = NULL;return ERR_MEM;}((struct pbuf_rom *)buf->p)->payload = dataptr;buf->p->len = buf->p->tot_len = size;buf->ptr = buf->p;return ERR_OK;
}

netbuf_chain()

将指针tail指向的pbuf添加到h指向的head链表的末尾,然后tail会被释放
pbuf_cat()catConcatenate的缩写,中文意思是 “连接” 或 “拼接”。

void
netbuf_chain(struct netbuf *head, struct netbuf *tail)
{LWIP_ERROR("netbuf_chain: invalid head", (head != NULL), return;);LWIP_ERROR("netbuf_chain: invalid tail", (tail != NULL), return;);pbuf_cat(head->p, tail->p);head->ptr = head->p;memp_free(MEMP_NETBUF, tail);
}

netbuf_data()

netbufptr指向的pbuf里数据的起始地址填写到dataptr,并将数据长度填写到len
netbuf中的p指向的是pbuf链表的首个pbufptr指向的是当前pbuf,如果想移动ptr可以用netbuf_next()函数或者netbuf_first()函数。

err_t
netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len)
{LWIP_ERROR("netbuf_data: invalid buf", (buf != NULL), return ERR_ARG;);LWIP_ERROR("netbuf_data: invalid dataptr", (dataptr != NULL), return ERR_ARG;);LWIP_ERROR("netbuf_data: invalid len", (len != NULL), return ERR_ARG;);if (buf->ptr == NULL) {return ERR_BUF;}*dataptr = buf->ptr->payload;*len = buf->ptr->len;return ERR_OK;
}

netbuf_next()

netbufptr移动到pbuf链表的下一个pbuf

s8_t
netbuf_next(struct netbuf *buf)
{LWIP_ERROR("netbuf_next: invalid buf", (buf != NULL), return -1;);if (buf->ptr->next == NULL) {return -1;}buf->ptr = buf->ptr->next;if (buf->ptr->next == NULL) {return 1;}return 0;
}

netbuf_first()

netbufptr指向pbuf链表的首个pbuf,也就是p指向的pbuf

void
netbuf_first(struct netbuf *buf)
{LWIP_ERROR("netbuf_first: invalid buf", (buf != NULL), return;);buf->ptr = buf->p;
}
http://www.dtcms.com/a/618961.html

相关文章:

  • 蓝牙EIR数据
  • 外企网站建设中国庆阳网
  • nfs共享服务
  • vue2[webpack]中接入vue3[vite]的qiankun微前端服务
  • Apache Hadoop-学习笔记1
  • 网站年龄和域名年龄贸易公司怎么做网站比较好
  • 如何拥有一个自己的网站网站安全管理制度建设下载
  • 有做阿里网站的吗网站开发后未付维护费
  • 百度推广必须做手机网站吗网页游戏网站mhn
  • 2018做网站的视频个人直播平台搭建
  • OpenAI 最新研究进展:定义和评估大语言模型中的政治偏见:OpenAI的透明度承诺
  • Streamlit 莫斯电码转换器学习笔记
  • 告别传统PPT!用reveal.js在文汇百川webOS上打造酷炫动态演示
  • 什么是行业网站?揭阳企业建站系统
  • 如何让新网站18.ppt网站是谁做的
  • 第四十三篇:MySQL事务:ACID特性、隔离级别与幻读、脏读详解
  • 网页设计个人网站设计wordpress 头部菜单
  • wordpress网站怎么加小游戏江门市住房和城乡建设局门户网站
  • 嘉兴快速建站模板响应式科技公司网站模板下载
  • 怎样增加网站的权重旧手机 搭建wordpress
  • Rust 命令行密码管理器工具开发
  • Restful协议举例,主要作用
  • h5网站开发流程图保山市住房和城乡建设局门户网站
  • 如何查找网站死链南京制作网站多少钱
  • 学习Ansible Playbook 核心语法
  • flink CDC 3.5.0
  • 阿里巴巴网站备案号用wordpress
  • 网站seo服务商seo文章外包
  • 微信网站设计运营用DW做的网站怎么分享给别人
  • 怎么建网站教程图解棋牌游戏开发多少钱