ipfs, libp2p, swarm notify

Data Structure

type Swarm struct {
	// Close refcount. This allows us to fully wait for the swarm to be torn
	// down before continuing.
	refs sync.WaitGroup

	local peer.ID
	peers peerstore.Peerstore

	conns struct {
		sync.RWMutex
		m map[peer.ID][]*Conn
	}

	listeners struct {
		sync.RWMutex

		ifaceListenAddres []ma.Multiaddr
		cacheEOL          time.Time

		m map[transport.Listener]struct{}
	}

	notifs struct {
		sync.RWMutex
		m map[network.Notifiee]struct{}
	}
	...

notifs is a map, indexed by network.Notifiee

notifiee Registration

IdConn: NewIDService()
Relay: NewAutoRelay()
BasicHost: NewHost()
DHT: NewDht()
Circuit: NewRelay()
NAT: NewAutoNAT()

Interface

type Notifiee interface {
	Listen(Network, ma.Multiaddr)      // called when network starts listening on an addr
	ListenClose(Network, ma.Multiaddr) // called when network stops listening on an addr
	Connected(Network, Conn)           // called when a connection opened
	Disconnected(Network, Conn)        // called when a connection closed
	OpenedStream(Network, Stream)      // called when a stream opened
	ClosedStream(Network, Stream)      // called when a stream closed

	// TODO
	// PeerConnected(Network, peer.ID)    // called when a peer connected
	// PeerDisconnected(Network, peer.ID) // called when a peer disconnected
}

Notify

swarm.go
addConn
	s.notifyAll(func(f network.Notifiee) { f.Connected(s, c) }
swarm_conn.go
addStream
	c.swarm.notifyAll(func(f network.Notifiee) { f.OpenedStream(c.swarm, s) }
doClose
		c.swarm.notifyAll(func(f network.Notifiee) { f.Disconnected(c.swarm, c) }
swarm_listen.go
AddListenAddr
	s.notifyAll(func(n network.Notifiee) { n.Listen(s, maddr) }
swarm_stream.go
remove
		s.conn.swarm.notifyAll(func(f network.Notifiee) { f.ClosedStream(s.conn.swarm, s) }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值