LwIP之netbuf
生活随笔
收集整理的這篇文章主要介紹了
LwIP之netbuf
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?netbuf是應用程序和協議棧內核交互的一種數據結構
netbuf并不復雜,下面是實現代碼
/* 創建netbuf */ struct netbuf *netbuf_new(void) {struct netbuf *buf;/* 為netbuf申請內存空間 */buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);if(buf != NULL) {/* 清空參數 */buf->p = NULL;buf->ptr = NULL;ip_addr_set_any(&buf->addr);buf->port = 0;return buf;} else {return NULL;} }/* 刪除netbuf */ void netbuf_delete(struct netbuf *buf) {if(buf != NULL) {/* 先釋放pbuf */if(buf->p != NULL) {pbuf_free(buf->p);buf->p = buf->ptr = NULL;}/* 再釋放netbuf */memp_free(MEMP_NETBUF, buf);} }/* 為netbuf申請數據(pbuf)空間 */ void *netbuf_alloc(struct netbuf *buf, u16_t size) {/* 為netbuf申請數據(pbuf)空間 */if(buf->p != NULL) {pbuf_free(buf->p);}buf->p = pbuf_alloc(PBUF_TRANSPORT, size, PBUF_RAM);if(buf->p == NULL) {return NULL;}/* ptr指針初始化指向第一個pbuf */buf->ptr = buf->p;/* 返回數據有效數據指針 */return buf->p->payload; }/* 釋放netbuf的數據(pbuf)空間 */ void netbuf_free(struct netbuf *buf) {if(buf->p != NULL) {pbuf_free(buf->p);}buf->p = buf->ptr = NULL; }/* 為netbuf申請PBUF_REF型pbuf內存,指向已存在RAM */ err_t netbuf_ref(struct netbuf *buf, const void *dataptr, u16_t size) {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;}buf->p->payload = (void *)dataptr;buf->p->len = buf->p->tot_len = size;buf->ptr = buf->p;return ERR_OK; }/* 將兩個netbuf拼接起來 */ void netbuf_chain(struct netbuf *head, struct netbuf *tail) {pbuf_cat(head->p, tail->p);head->ptr = head->p;memp_free(MEMP_NETBUF, tail); }/* 獲取netbuf有效數據指針和數據長度 */ err_t netbuf_data(struct netbuf *buf, void **dataptr, u16_t *len) {if(buf->ptr == NULL) {return ERR_BUF;}*dataptr = buf->ptr->payload;*len = buf->ptr->len;return ERR_OK; }/* 向后偏移netbuf的pbuf偏移指針 */ s8_t netbuf_next(struct netbuf *buf) {if(buf->ptr->next == NULL) {return -1;}buf->ptr = buf->ptr->next;if(buf->ptr->next == NULL) {return 1;}return 0; }/* netbuf的pbuf偏移指針指向第一個pbuf */ void netbuf_first(struct netbuf *buf) {buf->ptr = buf->p; }?
總結
以上是生活随笔為你收集整理的LwIP之netbuf的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html页面怎么解决跨域问题,前端web
- 下一篇: STM32之内部FLASH例程