当前位置:首页 > LWIP的底层结构
{
LOCK_TCPIP_CORE();
apimsg->function(&(apimsg->msg)); UNLOCK_TCPIP_CORE(); return ERR_OK; }
* Call the lower part of a netconn_* function * This function is then running in the thread context * of tcpip_thread and has exclusive access to lwIP core code. err_t tcpip_apimsg(struct api_msg *apimsg)【此为非locking的】 {
struct tcpip_msg msg;
if (mbox != SYS_MBOX_NULL) { msg.type = TCPIP_MSG_API; msg.msg.apimsg = apimsg; sys_mbox_post(mbox, &msg);
sys_arch_sem_wait(apimsg->msg.conn->op_completed, 0); return ERR_OK; }
return ERR_VAL; }
其实,功能都是一样的,都是要对apimsg->function函数的调用。只是途径不一样而已。看看它们的功能说明就知道了。这么来说apimsg->function的调用很重要了。从netconn_new_with_proto_and_callback函数的实现,可以知道这个function就是do_newconn
Void do_newconn(struct api_msg_msg *msg) {
if(msg->conn->pcb.tcp == NULL) { pcb_new(msg); }
/* Else? This \ /* Is this an error condition? Should it be deleted? */ /* We currently just are happy and return. */
TCPIP_APIMSG_ACK(msg); }
还是看TCP的,在pcb_new函数中有如下代码: case NETCONN_TCP:
msg->conn->pcb.tcp = tcp_new(); if(msg->conn->pcb.tcp == NULL) { msg->conn->err = ERR_MEM;
break; }
setup_tcp(msg->conn); break;
我们知道在这里建立了这个tcp的连接。至于这个超级牛的函数,以后再做介绍。
嗯,还是回过头来接着看accept函数吧。
Sock获得了,接着就是newconn = netconn_accept(sock->conn);通过mbox取得新的
连接。粗略的估计了一下,这个新的连接应该和listen有关系。那就再次打断一下,看看那个listen操作。
lwip_listen --? netconn_listen_with_backlog--? do_listen--? tcp_arg(msg->conn->pcb.tcp, msg->conn);
tcp_accept(msg->conn->pcb.tcp, accept_function);//注册了一个接受函数
* Accept callback function for TCP netconns.
* Allocates a new netconn and posts that to conn->acceptmbox.
static err_t accept_function(void *arg, struct tcp_pcb *newpcb, err_t err) {
struct netconn *newconn; struct netconn *conn;
conn = (struct netconn *)arg;
/* We have to set the callback here even though
* the new socket is unknown. conn->socket is marked as -1. */ newconn = netconn_alloc(conn->type, conn->callback); if (newconn == NULL) { return ERR_MEM; }
newconn->pcb.tcp = newpcb; setup_tcp(newconn); newconn->err = err;
/* Register event with callback */ API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
if (sys_mbox_trypost(conn->acceptmbox, newconn) != ERR_OK) {
/* When returning != ERR_OK, the connection is aborted in tcp_process(), so do nothing here! */ newconn->pcb.tcp = NULL; netconn_free(newconn); return ERR_MEM; }
return ERR_OK; }
共分享92篇相关文档