LWIP之SOCKET的实现(3)
so do nothing here! */
newconn->pcb.tcp = NULL; netconn_free(newconn); return ERR_MEM; }
return ERR_OK; }
对了,accept函数中从mbox中获取的连接就是这里放进去的。
再回到accept中来,取得了新的连接,接下来就是分配sock了,再然后,再然到此整个APP层,也就是传输层以上对socket的封装讲完了。在最后再总结一后?再然后就等用户来使用接收、发送数据了。 些整个路径的调用情况吧
LWIP之API_MSG结构及其实现
http://bluefish.blog.51cto.com/214870/158414
从上面一篇的socket实现来看,如果要评起到最关键作用的一个结构体,那么struct api_msg当之无愧。先看下它的定义:
/** This struct contains a function to execute in another thread context and a struct api_msg_msg that serves as an argument for this function.
This is passed to tcpip_apimsg to execute functions in tcpip_thread context. */
struct api_msg {
/** function to execute in tcpip_thread context */ void (* function)(struct api_msg_msg *msg); /** arguments for this function */ struct api_msg_msg msg; };
功能说的很清楚。但是具体怎么个操作法还是不知道,没关系,接着看它的调用。
举一个例子,刚好是上一篇中调用,但是没有看具体实现的 err_t netconn_getaddr(struct netconn *conn, struct ip_addr *addr, u16_t *port, u8_t local) {
struct api_msg msg;
msg.function = do_getaddr; msg.msg.conn = conn;
msg.msg.msg.ad.ipaddr = addr; msg.msg.msg.ad.port = port; msg.msg.msg.ad.local = local; TCPIP_APIMSG(&msg);
return conn->err; }
说明一下,api_msg结构几乎都是在netconn_xxx函数中被调用,方式千篇一律,除了msg.funcion的赋值不一样外。上面的调用很简单,对该结构体变量赋值,接着就是调用TCPIP_APIMSG,这个函数上面讲过,可过去看下。既然如此,就不得不说mbox及其相关函数了。
static sys_mbox_t mbox = SYS_MBOX_NULL;【tcp.c】 再看sys_mbox_t的定义,在【src\\include\\lwip\\sys.h】中
/* For a totally minimal and standalone system, we provide null definitions of the sys_ functions. */
typedef u8_t sys_sem_t; typedef u8_t sys_mbox_t; typedef u8_t sys_prot_t;
可以看到这里只是简单的定义成了u8类型,注意上面的红色字体的说明,很明显这个是可移植的一部分,需要根据不同的平台,不同的操作系统具体定义。可以借鉴焦海波大侠的关于ucos上对lwip的移植笔记来看。
我们可以看到在api_msg结构的处理过程中,所有的信息都是包含在
api_msg_msg结构体中的,api_msg只是将其和function简单的组合了。下面看下这个牛结构的定义:
/** This struct includes everything that is necessary to execute a function for a netconn in another thread context (mainly used to process netconns in the tcpip_thread context to be thread safe). */
struct api_msg_msg {
/** The netconn which to process - always needed: it includes the semaphore
which is used to block the application thread until the function finished. */
struct netconn *conn;
/** Depending on the executed function, one of these union members is used */ union {
/** used for do_send */ struct netbuf *b;
/** used for do_newconn */ struct {
u8_t proto; } n;
/** used for do_bind and do_connect */ struct {
struct ip_addr *ipaddr; u16_t port; } bc;
/** used for do_getaddr */ struct {
struct ip_addr *ipaddr; u16_t *port; u8_t local; } ad;
/** used for do_write */ struct {
const void *dataptr; int len;
u8_t apiflags; } w;
/** used ofr do_recv */ struct {
u16_t len; } r;
#if LWIP_IGMP
/** used for do_join_leave_group */ struct {
struct ip_addr *multiaddr; struct ip_addr *interface;
enum netconn_igmp join_or_leave; } jl;
#endif /* LWIP_IGMP */ #if TCP_LISTEN_BACKLOG struct {
u8_t backlog; } lb;
#endif /* TCP_LISTEN_BACKLOG */ } msg; };
一个很合理的设计,至少笔者是这么认为的。关键在于msg union的设计。
LWIP之TCP层发送相关
http://bluefish.blog.51cto.com/214870/158415
现在我们正式开始进入对TCP的研究,它属于传输层协议,它为应用程序提供了可靠的字节流服务。在LWIP中基本的TCP处理过程被分割为六个功能函数的实现:tcp_input(), tcp_process(), tcp_receive()【与TCP输入有关】, tcp_write(), tcp_enqueue(), tcp_output()【用于TCP输出】。这些是从大的方面来划分的。
现在先从小部tcp.c文件来分析一下:我们知道这里的函数都是被socket那一层的最终调用的。为了利于分析,我选择lwip_send函数来分析,具体不多说,最终调用到了
static err_t do_writemore(struct netconn *conn)这个函数,当然这期间也做了不少工作,最主要的就是把发送数据的指针放到了msg的指定变量中 msg.msg.msg.w.dataptr = dataptr;//指针 msg.msg.msg.w.len = size; //长度 这些又经过转化放到了netconn的write_msg中
最后就是对do_writemore的调用了,下面详细分析这个函数。
这个函数的最直接调用有以下几个: available = tcp_sndbuf(conn->pcb.tcp); err = tcp_write(conn->pcb.tcp, dataptr, len, conn->write_msg->msg.w.apiflags); err = tcp_output_nagle(conn->pcb.tcp);
…… 此处隐藏:1796字,全部文档内容请下载后查看。喜欢就下载吧 ……相关推荐:
- [实用模板]第八章:法国“新浪潮”与“左岸派”
- [实用模板]2021年北京上半年临床医学检验技师生物
- [实用模板]SAP GUI 7.10客户端安装配置文档
- [实用模板]2001年临床执业医师资格考试综合笔试试
- [实用模板]36机场工作实用英语词汇总结
- [实用模板](一)社会保险稽核通知书
- [实用模板]安全教育主题班会材料
- [实用模板]濉溪县春季呼吸道传染病防控应急演练方
- [实用模板]长沙房地产市场周报(1.30-2.3)
- [实用模板]六年级数学上册典中点 - 图文
- [实用模板]C程序设计(红皮书)习题官方参考答案
- [实用模板]中国证监会第一届创业板发行审核委员会
- [实用模板]桥梁工程复习题
- [实用模板]2011学而思数学及答案
- [实用模板]初中病句修改专项练习
- [实用模板]监理学习知识1 - 图文
- [实用模板]小机灵杯四年级试题
- [实用模板]国贸专业毕业论文模板
- [实用模板]教育学概论考试练习题-判断题4
- [实用模板]2015届高考英语一轮复习精品资料(译林
- 00Nkmhe_市场营销学工商管理_电子商务_
- 事业单位考试法律常识
- 诚信教育实施方案
- 吉大小天鹅食品安全检测箱方案(高中低
- 房地产销售培训资料
- 高一地理必修1复习提纲
- 新概念英语第二册lesson_1_练习题
- 证券公司内部培训资料
- 小学英语时间介词专项练习
- 新世纪英语专业综合教程(第二版)第1册U
- 【新课标】浙教版最新2018年八年级数学
- 工程建设管理纲要
- 外研版 必修一Module 4 A Social Surve
- Adobe认证考试 AE复习资料
- 基于H.264AVC与AVS标准的帧内预测技术
- 《食品检验机构资质认定管理办法》(质
- ABB变频器培训课件
- (完整版)小学说明文阅读练习题及答案
- 深思洛克(SenseLock) 深思IV,深思4,深
- 弟子规全文带拼音




