btcd的p2p網(wǎng)絡(luò)(3)-連接ConnMgr-連接成功之后

我們繼續(xù)上一節(jié),先簡(jiǎn)要回顧一下
我們主要通過(guò)一下幾個(gè)步驟建立了一個(gè)連接

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:
    }
}

然后在工作協(xié)程中

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)
                }
}

我們接下來(lái)主要看的是,連接成功后干什么?

我們就來(lái)找cm.cfg.OnConnection(),在編輯器中全局搜索之后發(fā)現(xiàn),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,發(fā)現(xiàn)outboundPeerConnected是一個(gè)函數(shù),這和我們連接成功調(diào)用時(shí)是符合的: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包來(lái)處理。請(qǐng)看霞姐的peer

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容