最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 正文

LWIP之二API_MSG结构及其实现

来源:动视网 责编:小OO 时间:2025-09-29 19:02:20
文档

LWIP之二API_MSG结构及其实现

LWIP之API_MSG结构及其实现2009-05-1600:40:40标签:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://bluefish.blog.51cto.com/214870/1584142009-5-11LWIP之API_MSG结构及其实现从上面一篇的socket实现来看,如果要评起到最关键作用的一个结构体,那么structapi_msg当之无愧。先看下它的定义:/**Thisstructcontainsafunc
推荐度:
导读LWIP之API_MSG结构及其实现2009-05-1600:40:40标签:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://bluefish.blog.51cto.com/214870/1584142009-5-11LWIP之API_MSG结构及其实现从上面一篇的socket实现来看,如果要评起到最关键作用的一个结构体,那么structapi_msg当之无愧。先看下它的定义:/**Thisstructcontainsafunc
LWIP之API_MSG结构及其实现 

2009-05-16 00:40:40

标签: 

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://bluefish.blog.51cto.com/214870/158414 

2009-5-11 LWIP之API_MSG结构及其实现

    从上面一篇的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的设计。 

本文出自 “bluefish” 博客,请务必保留此出处http://bluefish.blog.51cto.com/214870/158414

文档

LWIP之二API_MSG结构及其实现

LWIP之API_MSG结构及其实现2009-05-1600:40:40标签:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://bluefish.blog.51cto.com/214870/1584142009-5-11LWIP之API_MSG结构及其实现从上面一篇的socket实现来看,如果要评起到最关键作用的一个结构体,那么structapi_msg当之无愧。先看下它的定义:/**Thisstructcontainsafunc
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top