Documentation
¶
Overview ¶
Package peer provides objects for managing a connection to a remote peer that obeys the bitmessage protocol.
Connection abstracts a tcp connection to a remote bitmessage peer so that the rest of the peer does not have to deal with byte arrays. It also prevents timeout by regularly sending pong messages.
Listener listens for incoming tcp connections and creates Connection objects for them when a connection is opened.
Send manages everything that is to be sent to the remote peer eventually. Data requests, inv trickles, and other messages.
Peer manages the peer object overall and routes incoming messages.
Logic is an interface that has functions such as HandleAddrMsg that handles the different kinds of messages in the bitmessage protocol.
Inventory can be used to store the known inventory and the requested inventory for a peer.
Index ¶
- type Connection
- type Inventory
- func (I *Inventory) AddKnown(invVect *wire.InvVect)
- func (I *Inventory) AddRequest(invVect *wire.InvVect)
- func (I *Inventory) DeleteRequest(invVect *wire.InvVect) (ok bool)
- func (I *Inventory) FilterKnown(inv []*wire.InvVect) []*wire.InvVect
- func (I *Inventory) FilterRequested(inv []*wire.InvVect) []*wire.InvVect
- func (I *Inventory) IsKnown(invVect *wire.InvVect) bool
- type Listener
- type Logic
- type MruInventoryMap
- type Peer
- type Send
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Connection ¶
type Connection interface { WriteMessage(wire.Message) error ReadMessage() (wire.Message, error) BytesWritten() uint64 BytesRead() uint64 LastWrite() time.Time LastRead() time.Time RemoteAddr() net.Addr Connected() bool Connect() error Close() }
Connection is a bitmessage connection that abstracts the underlying tcp connection away. The user of the Connection only uses bitmessage wire.Message objects instead of the underlying byte stream. This is written as an interface so that it can easily be swapped out for a mock object for testing purposes.
func NewConnection ¶
func NewConnection(addr net.Addr) Connection
NewConnection creates a new *connection.
type Inventory ¶
type Inventory struct {
// contains filtered or unexported fields
}
Inventory is the part of a peer that manages object hashes and remembers which are known to the peer and which have been requested from it. It is safe for concurrent access.
func (*Inventory) AddKnown ¶
AddKnown adds the passed inventory to the cache of known inventory for the peer.
func (*Inventory) AddRequest ¶
AddRequest adds a request to the set of requested inventory.
func (*Inventory) DeleteRequest ¶
DeleteRequest removes an entry from the set of requested inventory. It returns true if the inventory was really removed.
func (*Inventory) FilterKnown ¶
FilterKnown takes a list of InvVects, adds them to the list of known inventory, and returns those which were not already in the list. It is used to ensure that data is not sent to the peer that it already is known to have.
func (*Inventory) FilterRequested ¶
FilterRequested takes a list of InvVects, adds them to the list of requested data, and returns those which were not already in the list. It is used to ensure that data is not requested twice.
type Listener ¶
type Listener interface { Accept() (Connection, error) Close() error Addr() net.Addr }
Listener represents an open port listening for bitmessage connections. It is given as an interface so that mock peer listeners can easily swapped for the genuine ones.
type Logic ¶
type Logic interface { ProtocolVersion() uint32 HandleVersionMsg(*wire.MsgVersion) error HandleVerAckMsg() error HandleAddrMsg(*wire.MsgAddr) error HandleInvMsg(*wire.MsgInv) error HandleGetDataMsg(*wire.MsgGetData) error HandleObjectMsg(wire.Message) error PushVersionMsg() PushVerAckMsg() PushAddrMsg(addresses []*wire.NetAddress) error PushInvMsg(invVect []*wire.InvVect) PushGetDataMsg(invVect []*wire.InvVect) PushObjectMsg(sha *wire.ShaHash) }
Logic is an interface that represents the behavior of a peer object excluding the parts that must be continually running.
type MruInventoryMap ¶
type MruInventoryMap struct {
// contains filtered or unexported fields
}
MruInventoryMap provides a map that is limited to a maximum number of items with eviction for the oldest entry when the limit is exceeded.
func NewMruInventoryMap ¶
func NewMruInventoryMap(limit uint) *MruInventoryMap
NewMruInventoryMap returns a new inventory map that is limited to the number of entries specified by limit. When the number of entries exceeds the limit, the oldest (least recently used) entry will be removed to make room for the new entry.
func (*MruInventoryMap) Add ¶
func (m *MruInventoryMap) Add(iv *wire.InvVect)
Add adds the passed inventory to the map and handles eviction of the oldest item if adding the new item would exceed the max limit.
func (*MruInventoryMap) Delete ¶
func (m *MruInventoryMap) Delete(iv *wire.InvVect)
Delete deletes the passed inventory item from the map (if it exists).
func (*MruInventoryMap) Exists ¶
func (m *MruInventoryMap) Exists(iv *wire.InvVect) bool
Exists returns whether or not the passed inventory item is in the map.
func (*MruInventoryMap) Filter ¶
func (m *MruInventoryMap) Filter(invs []*wire.InvVect) []*wire.InvVect
Filter takes a slice of values and adds those to the map that don't already exist. It returns a slice containing only the values that were added.
func (MruInventoryMap) String ¶
func (m MruInventoryMap) String() string
String returns the map as a human-readable string.
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
Peer handles and routes incoming messages and manages other peer components.
func NewPeer ¶
func NewPeer(logic Logic, conn Connection, send Send) *Peer
NewPeer returns a new Peer object.
func (*Peer) Connect ¶
Connect connects the peer object to the remote peer if it is not already connected.
func (*Peer) Disconnect ¶
func (p *Peer) Disconnect()
Disconnect disconnects the peer by closing the connection. It also sets a flag so the impending shutdown can be detected.
type Send ¶
type Send interface { // QueueMessage queues a message to be sent to the peer. QueueMessage(wire.Message) error // QueueDataRequest QueueDataRequest([]*wire.InvVect) error // QueueInventory adds the passed inventory to the inventory send queue which // might not be sent right away, rather it is trickled to the peer in batches. // Inventory that the peer is already known to have is ignored. It is safe for // concurrent access. QueueInventory([]*wire.InvVect) error Start(conn Connection) Running() bool Stop() }
Send handles everything that is to be sent to the remote peer eventually. It takes messages and sends them over the outgoing connection, inventory vectors corresponding to objects that the peer has requested, and inventory vectors representing objects that we have, which will be periodically sent to the peer in a series of inv messages.