btcdのp 2 pネットワーク(3)−接続ConnMgr−接続に成功した後
2844 ワード
前節を続けて、まず簡単に振り返ってみましょう.私たちは主にいくつかのステップを通じて接続を確立しました.
そして作業協力の中で
次に主に見ているのは、接続に成功したら何をしますか?
cmを探しに来ましたcfg.OnConnection()は、エディタでグローバルに検索すると、OnConnection()は
接続に成功すると、
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
そして作業協力の中で
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を探しに来ましたcfg.OnConnection()は、エディタでグローバルに検索すると、OnConnection()は
server.go
でのみ構成されており、server.go
ではnewServer()
メソッドでは 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を見てください