美文网首页
btcd的p2p网络(3)-连接ConnMgr-连接成功之后

btcd的p2p网络(3)-连接ConnMgr-连接成功之后

作者: 豆瓣奶茶 | 来源:发表于2019-02-11 19:59 被阅读2次

我们继续上一节,先简要回顾一下
我们主要通过一下几个步骤建立了一个连接

func (cm *ConnManager) Start() {
    for i := atomic.LoadUint64(&cm.connReqCount); i < uint64(cm.cfg.TargetOutbound); i++ {
        go cm.NewConnReq()
    }
}
func (cm *ConnManager) NewConnReq() {
    ......
    c := &ConnReq{}
    addr, err := cm.cfg.GetNewAddress()
    c.Addr = addr
    cm.Connect(c)
}
// Connect assigns an id and dials a connection to the address of the
// connection request.
func (cm *ConnManager) Connect(c *ConnReq) {
    ......
    conn, err := cm.cfg.Dial(c.Addr)
    select {
    case cm.requests <- handleConnected{c, conn}:
    case <-cm.quit:
    }
}

然后在工作协程中

func (cm *ConnManager) connHandler() {

    var (
        // pending holds all registered conn requests that have yet to
        // succeed.
        pending = make(map[uint64]*ConnReq)

        // conns represents the set of all actively connected peers.
        conns = make(map[uint64]*ConnReq, cm.cfg.TargetOutbound)
    )

out:
    for {
        select {
        case req := <-cm.requests:
            switch msg := req.(type) {

            case registerPending:
                connReq := msg.c
                connReq.updateState(ConnPending)
                pending[msg.c.id] = connReq
                close(msg.done)
            // 连城成功后的处理
            case handleConnected:
                connReq := msg.c

                if _, ok := pending[connReq.id]; !ok {
                    if msg.conn != nil {
                        msg.conn.Close()
                    }
                    log.Debugf("Ignoring connection for "+
                        "canceled connreq=%v", connReq)
                    continue
                }

                connReq.updateState(ConnEstablished)
                connReq.conn = msg.conn
                conns[connReq.id] = connReq
                log.Debugf("Connected to %v", connReq)
                connReq.retryCount = 0
                cm.failedAttempts = 0

                delete(pending, connReq.id)

                if cm.cfg.OnConnection != nil {
                    go cm.cfg.OnConnection(connReq, msg.conn)
                }
}

我们接下来主要看的是,连接成功后干什么?

我们就来找cm.cfg.OnConnection(),在编辑器中全局搜索之后发现,OnConnection()只有在server.go中配置了,server.gonewServer()方法中

    cmgr, err := connmgr.New(&connmgr.Config{
        Listeners:      listeners,
        OnAccept:       s.inboundPeerConnected,
        RetryDuration:  connectionRetryInterval,
        TargetOutbound: uint32(targetOutbound),
        Dial:           btcdDial,
        OnConnection:   s.outboundPeerConnected,
        GetNewAddress:  newAddressFunc,
    })

就是这句OnConnection: s.outboundPeerConnected,然后我们就去找outboundPeerConnected,发现outboundPeerConnected是一个函数,这和我们连接成功调用时是符合的:go cm.cfg.OnConnection(connReq, msg.conn)

// outboundPeerConnected is invoked by the connection manager when a new
// outbound connection is established.  It initializes a new outbound server
// peer instance, associates it with the relevant state such as the connection
// request instance and the connection itself, and finally notifies the address
// manager of the attempt.
func (s *server) outboundPeerConnected(c *connmgr.ConnReq, conn net.Conn) {
    sp := newServerPeer(s, c.Permanent)
    p, err := peer.NewOutboundPeer(newPeerConfig(sp), c.Addr.String())
    if err != nil {
        srvrLog.Debugf("Cannot create outbound peer %s: %v", c.Addr, err)
        s.connManager.Disconnect(c.ID())
    }
    sp.Peer = p
    sp.connReq = c
    sp.isWhitelisted = isWhitelisted(conn.RemoteAddr())
    sp.AssociateConnection(conn)
    go s.peerDoneHandler(sp)
    s.addrManager.Attempt(sp.NA())
}

我们看到连接成功后,需要用到peer包来处理。请看霞姐的peer

相关文章

  • btcd的p2p网络(3)-连接ConnMgr-连接成功之后

    我们继续上一节,先简要回顾一下我们主要通过一下几个步骤建立了一个连接 然后在工作协程中 我们接下来主要看的是,连接...

  • btcd 源码分析系列:4 - p2p网络的peer

    参考:btcd btc在p2p网络中与每一个节点的连接都视为一个peer对象,与该节点的消息交换都是通过该peer...

  • btcd的p2p网络(2)-连接ConnMgr

    p2p网络从底层到上层可以分为3层,地址 连接 节点,每一层都有自己的功能声明:文章代码和源码有不一致地方这篇文章...

  • 初次配置CentOS 7

    查看网络连接 连接网络 —— 将 ONBOOT=no ,改成 ONBOOT=yes,保存后退出。 —— OK,成功...

  • btcd 源码分析系列:3 - connmanager

    参考:btcd connmanamger 负责节点的连接处理,包括监听来自其他节点的连接请求和主动向其他节点发起连...

  • 部署前端项目到服务器

    使用的工具是FinalShell 1、首先创建一个连接 2、双击之后连接服务器,成功之后再登录 3、登录服务器,服...

  • CentOS 网络设置开机自动连接

    CentOS安装完成之后默认是关闭网络连接的,每次重启之后还要重新打开网络连接,比较麻烦。直接设置成开机自动连接网...

  • Android简单获取网络数据网络

    获取网络数据 1.URL地址 2.建立连接 3.连接成功判断 4.开始传输数据获取相应的对象流 5.如果操作成功,...

  • ADB 无线连接应用调试

    1.连接USB,开启网络调试功能; 2.查看手机的IP地址; 成功返回IP 3.连接手机ip; 拔出USB,你就可...

  • P2P文件分发

    P2P文件分发 概念介绍 对等方:成对间歇连接的主机 邻近对等方:成功创建一个TCP连接的对等方 洪流(torre...

网友评论

      本文标题:btcd的p2p网络(3)-连接ConnMgr-连接成功之后

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