github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/p2p/base_reactor.go (about)

     1  package p2p
     2  
     3  import (
     4  	cmn "github.com/tendermint/tmlibs/common"
     5  
     6  	"github.com/bytom/bytom/p2p/connection"
     7  )
     8  
     9  //Reactor is responsible for handling incoming messages of one or more `Channels`
    10  type Reactor interface {
    11  	cmn.Service // Start, Stop
    12  
    13  	// SetSwitch allows setting a switch.
    14  	SetSwitch(*Switch)
    15  
    16  	// GetChannels returns the list of channel descriptors.
    17  	GetChannels() []*connection.ChannelDescriptor
    18  
    19  	// AddPeer is called by the switch when a new peer is added.
    20  	AddPeer(peer *Peer) error
    21  
    22  	// RemovePeer is called by the switch when the peer is stopped (due to error
    23  	// or other reason).
    24  	RemovePeer(peer *Peer, reason interface{})
    25  
    26  	// Receive is called when msgBytes is received from peer.
    27  	//
    28  	// NOTE reactor can not keep msgBytes around after Receive completes without
    29  	// copying.
    30  	//
    31  	// CONTRACT: msgBytes are not nil.
    32  	Receive(chID byte, peer *Peer, msgBytes []byte)
    33  }
    34  
    35  //BaseReactor base service of a reactor
    36  type BaseReactor struct {
    37  	cmn.BaseService // Provides Start, Stop, .Quit
    38  	Switch          *Switch
    39  }
    40  
    41  //NewBaseReactor create new base Reactor
    42  func NewBaseReactor(name string, impl Reactor) *BaseReactor {
    43  	return &BaseReactor{
    44  		BaseService: *cmn.NewBaseService(nil, name, impl),
    45  		Switch:      nil,
    46  	}
    47  }
    48  
    49  //SetSwitch setting a switch for reactor
    50  func (br *BaseReactor) SetSwitch(sw *Switch) {
    51  	br.Switch = sw
    52  }
    53  
    54  //GetChannels returns the list of channel descriptors
    55  func (*BaseReactor) GetChannels() []*connection.ChannelDescriptor { return nil }
    56  
    57  //AddPeer is called by the switch when a new peer is added
    58  func (*BaseReactor) AddPeer(peer *Peer) {}
    59  
    60  //RemovePeer is called by the switch when the peer is stopped (due to error or other reason)
    61  func (*BaseReactor) RemovePeer(peer *Peer, reason interface{}) {}
    62  
    63  //Receive is called when msgBytes is received from peer
    64  func (*BaseReactor) Receive(chID byte, peer *Peer, msgBytes []byte) {}