github.com/pure-x-eth/consensus_tm@v0.0.0-20230502163723-e3c2ff987250/p2p/base_reactor.go (about)

     1  package p2p
     2  
     3  import (
     4  	"github.com/pure-x-eth/consensus_tm/libs/service"
     5  	"github.com/pure-x-eth/consensus_tm/p2p/conn"
     6  )
     7  
     8  // Reactor is responsible for handling incoming messages on one or more
     9  // Channel. Switch calls GetChannels when reactor is added to it. When a new
    10  // peer joins our node, InitPeer and AddPeer are called. RemovePeer is called
    11  // when the peer is stopped. Receive is called when a message is received on a
    12  // channel associated with this reactor.
    13  //
    14  // Peer#Send or Peer#TrySend should be used to send the message to a peer.
    15  type Reactor interface {
    16  	service.Service // Start, Stop
    17  
    18  	// SetSwitch allows setting a switch.
    19  	SetSwitch(*Switch)
    20  
    21  	// GetChannels returns the list of MConnection.ChannelDescriptor. Make sure
    22  	// that each ID is unique across all the reactors added to the switch.
    23  	GetChannels() []*conn.ChannelDescriptor
    24  
    25  	// InitPeer is called by the switch before the peer is started. Use it to
    26  	// initialize data for the peer (e.g. peer state).
    27  	//
    28  	// NOTE: The switch won't call AddPeer nor RemovePeer if it fails to start
    29  	// the peer. Do not store any data associated with the peer in the reactor
    30  	// itself unless you don't want to have a state, which is never cleaned up.
    31  	InitPeer(peer Peer) Peer
    32  
    33  	// AddPeer is called by the switch after the peer is added and successfully
    34  	// started. Use it to start goroutines communicating with the peer.
    35  	AddPeer(peer Peer)
    36  
    37  	// RemovePeer is called by the switch when the peer is stopped (due to error
    38  	// or other reason).
    39  	RemovePeer(peer Peer, reason interface{})
    40  
    41  	// Receive is called by the switch when msgBytes is received from the peer.
    42  	//
    43  	// NOTE reactor can not keep msgBytes around after Receive completes without
    44  	// copying.
    45  	//
    46  	// CONTRACT: msgBytes are not nil.
    47  	//
    48  	// Only one of Receive or ReceiveEnvelope are called per message. If ReceiveEnvelope
    49  	// is implemented, it will be used, otherwise the switch will fallback to
    50  	// using Receive.
    51  	// Deprecated: Reactors looking to receive data from a peer should implement ReceiveEnvelope.
    52  	// Receive will be deprecated in favor of ReceiveEnvelope in v0.37.
    53  	Receive(chID byte, peer Peer, msgBytes []byte)
    54  }
    55  
    56  type EnvelopeReceiver interface {
    57  	// ReceiveEnvelope is called by the switch when an envelope is received from any connected
    58  	// peer on any of the channels registered by the reactor.
    59  	//
    60  	// Only one of Receive or ReceiveEnvelope are called per message. If ReceiveEnvelope
    61  	// is implemented, it will be used, otherwise the switch will fallback to
    62  	// using Receive. Receive will be replaced by ReceiveEnvelope in a future version
    63  	ReceiveEnvelope(Envelope)
    64  }
    65  
    66  //--------------------------------------
    67  
    68  type BaseReactor struct {
    69  	service.BaseService // Provides Start, Stop, .Quit
    70  	Switch              *Switch
    71  }
    72  
    73  func NewBaseReactor(name string, impl Reactor) *BaseReactor {
    74  	return &BaseReactor{
    75  		BaseService: *service.NewBaseService(nil, name, impl),
    76  		Switch:      nil,
    77  	}
    78  }
    79  
    80  func (br *BaseReactor) SetSwitch(sw *Switch) {
    81  	br.Switch = sw
    82  }
    83  func (*BaseReactor) GetChannels() []*conn.ChannelDescriptor        { return nil }
    84  func (*BaseReactor) AddPeer(peer Peer)                             {}
    85  func (*BaseReactor) RemovePeer(peer Peer, reason interface{})      {}
    86  func (*BaseReactor) ReceiveEnvelope(e Envelope)                    {}
    87  func (*BaseReactor) Receive(chID byte, peer Peer, msgBytes []byte) {}
    88  func (*BaseReactor) InitPeer(peer Peer) Peer                       { return peer }