美文网首页
nanomsg 1.1.5 源代码分析 (三)

nanomsg 1.1.5 源代码分析 (三)

作者: RonZheng2010 | 来源:发表于2018-11-27 21:52 被阅读0次

    1. 协议中的数据结构

    nanomsg定义3种数据结构nn_dist、nn_hash和nn_priolist,来保存nn_pipe(实际上就是nn_pipebase)。有了它们,就可以按不同的顺序访问它们包括的nn_pipe。

    1.1 nn_dist

    nn_dist直接包括一个普通的列表nn_list。
    将nn_pipe包裹在nn_dist_data的派生类实例中,就可以保存在nn_dist中。其中的nn_pipe顺序访问。

    1.2 nn_hash

    nn_hash是一个hash列表。它的array成员是一组nn_list,可以根据nn_list_item的key值将它保存到其中一个nn_list中。

    将nn_pip包裹在nn_hash_item的派生类实例中,并赋予nn_pipe唯一的key,就可以将它保存在nn_hash中。其中的nn_pipe根据key快速访问。

    1.3 nn_priolist

    nn_priolist的成员slots[]是一组nn_priolist_slot,而nn_priolist_slot包括一个nn_list。可以根据nn_list_item的优先值将它保存在对应的nn_priolist_slot实例中。在nn_priolist中,nn_priolist_slot是按优先值排序的(实际上priority对应的就是slots[priority-1])。

    将连接的nn_pipe被包裹在nn_priolist_data的派生类实例中,并给nn_pipe一个优先值,就可以将它保存在nn_priolist中。其中的nn_pipe按优先级访问,高优先级的先访问,高优先级的被移除了,才轮到低优先级的。

    为了快速确定当前最高优先级的nn_pipe,nn_priolist_data将优先值保存在priority成员中。

    2. 协议

    2.1 xpub/xsub

    在PUB端创建nn_xpub对象。

    nn_xpub可以发送消息给多个SUB端。
    nn_xpub用nn_dist保存nn_pipe。发送消息时,nn_xpub依次遍历其中的nn_pipe并发送。

    在SUB端创建nn_xsub对象。

    nn_xsub可以从多个PUB端接收消息。

    nn_xsub用nn_fq保存nn_pipe, nn_fq内部包括nn_priolist。调用者通过socket选项指定nn_pipe的优先值。
    接收消息时,nn_xsub优先从优先级高的nn_pipe接收。优先级高的nn_pipe没有消息了,才轮到优先级低的。

    nn_xsub的nn_trie成员是一个前缀树,用来保存过滤条件。调用者可以通过socket选项指定这个条件。接收消息时,如果不匹配条件,则消息被丢弃。

    2.2 xreq/xrep

    在RESPOND端创建nn_xrep对象。

    nn_xrep可以响应多个REQUEST端的请求。
    nn_xrep用nn_hash保存nn_pipe用于发送。nn_xrep用一个单调递增值给nn_pipe设置key,key的初值是随机产生的。

    nn_xrep用nn_fq保存nn_pipe用于接收,nn_fq中包括nn_priolist。 接收消息时,nn_xsub按照优先级顺序接收,这点与xsub的用法相同。不同的是,nn_xrep还要有消息回应。nn_xrep把nn_pipe的key值保存消息头中,返回给调用者。调用者发送回应消息时应该附带这个key,nn_xrep从中解析出key后,就可以在nn_hash中找到相应的nn_pipe发送消息了。

    在REQUEST端创建nn_xreq对象。

    nn_xrep可以向多个RESPOND端发送请求。
    nn_xrep将nn_pipe保存在nn_lb用于发送,同时也保存在nn_fq用于接收。 nn_lb和nn_fq都是nn_priolist。调用者可以对nn_pipe的发送方向和接收方向分别设置优先级。
    发送请求消息时,nn_xrep优先向优先级高的nn_pipe发送。优先级高的nn_pipe发送空间满了,才轮到优先级低的。
    接收回应消息时,nn_xrep优先从优先级高的nn_pipe接收。优先级高的nn_pipe没有消息了,才轮到优先级低的。

    2.3 xsurveyor/xrespondent

    在RESPONDENT端创建nn_xrespondent。

    nn_xrespondent可以响应多个SURVEYOR的问询请求。
    nn_xrespondent将nn_pipe保存在nn_hash中用于发送。nn_xrespondent用一个单调递增值给nn_pipe设置key,key的初值是随机产生的。
    nn_xrespondent将nn_pipe保存在nn_fq中用于接收。接收消息时,nn_xresondent按照优先级顺序接收。nn_xrespondent要发送回应消息。nn_xrespondent把nn_pipe的key值保存消息头中,返回给调用者。调用者发送回应消息时应该附带这个key,nn_xrespondent从中解析出key后,就可以在nn_hash中找到相应的nn_pipe发送消息了。

    在SURVEYOR端创建nn_xsurveyor。

    nn_xsurveyor可以向多个xrespondent问询。
    nn_xsurveyor将nn_pipe保存在nn_dist中用于发送。发送时,nn_xresondent向所有nn_pipe依次发送询问消息。
    nn_xsurveyor将nn_pipe保存在nn_fq中用于接收。接收时,nn_xsurveyor优先从优先级高的nn_pipe接收。优先级高的nn_pipe没有消息了,才轮到优先级低的。

    2.4 pipeline

    nanomsg直接支持的协议是nn_xpull和nn_xpush协议。要构造pipeline,调用者需要组合nn_xpull和nn_xpush。pipeline起点需要一个xpush,终点需要一个nn_xpull,中间的节点则组合xpull和xpush各一个。

    在PUSH端创建nn_xpush。

    nn_xpush可以向多个PULL端推送消息。
    nn_xpush将nn_pipe保存在nn_lb中用于发送,nn_lb包括nn_priolist。
    发送消息时,nn_xpush优先向优先级高的nn_pipe发送。优先级高的nn_pipe发送空间满了,才轮到优先级低的。

    在PULL端创建nn_xpull。

    nn_xpull可以从多个PUSH端接收消息。
    nn_xpull将nn_pipe保存在nn_fq中用于接收。 nn_lb包括nn_priolist。
    接收消息时,nn_xpull优先从优先级高的nn_pipe接收。优先级高的nn_pipe没有消息了,才轮到优先级低的。

    相关链接
    nanomsg 1.1.5 源代码分析 (一)
    nanomsg 1.1.5 源代码分析 (二)
    nanomsg 1.1.5 源代码分析 (三)
    nanomsg 1.1.5 源代码分析 (四)

    相关文章

      网友评论

          本文标题:nanomsg 1.1.5 源代码分析 (三)

          本文链接:https://www.haomeiwen.com/subject/fhanqqtx.html