Documentation ¶
Index ¶
- type Chan
- type ChanID
- type Chunk
- type ChunkID
- type DataMsg
- type Datagram
- type HandshakeMsg
- type HaveMsg
- type Msg
- type MsgData
- type MsgError
- type Network
- type Opcode
- type Peer
- type PeerID
- type Ppspp
- func (p *Ppspp) AddLocalChunk(sid SwarmID, cid ChunkID, b []byte) error
- func (p *Ppspp) AddSwarm(config SwarmConfig) error
- func (p *Ppspp) HandleDatagram(d *Datagram, id PeerID) error
- func (p *Ppspp) ProtocolState(sid SwarmID, pid PeerID) (ProtocolState, error)
- func (p *Ppspp) SendClosingHandshake(remote PeerID, sid SwarmID) error
- func (p *Ppspp) SendData(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error
- func (p *Ppspp) SendHave(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error
- func (p *Ppspp) SendRequest(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error
- func (p *Ppspp) SetDatagramSender(f func(Datagram, PeerID) error)
- func (p *Ppspp) StartHandshake(remote PeerID, sid SwarmID) error
- func (p *Ppspp) Swarm(id SwarmID) (*Swarm, error)
- type Protocol
- type ProtocolState
- type RequestMsg
- type StringPeerID
- type StubNetwork
- func (n *StubNetwork) AddAddrs(id PeerID, addrs []ma.Multiaddr)
- func (n *StubNetwork) Addrs() []ma.Multiaddr
- func (n *StubNetwork) Connect(remote PeerID) error
- func (n *StubNetwork) Disconnect(remote PeerID) error
- func (n *StubNetwork) ID() PeerID
- func (n *StubNetwork) InjectIncomingDatagram(d *Datagram, id PeerID) error
- func (n *StubNetwork) NumSentDatagrams() int
- func (n *StubNetwork) ReadSentDatagram() *Datagram
- func (n *StubNetwork) SendDatagram(d Datagram, remote PeerID) error
- func (n *StubNetwork) SetDatagramHandler(f func(*Datagram, PeerID) error)
- type StubProtocol
- func (p *StubProtocol) AddLocalChunk(sid SwarmID, cid ChunkID, b []byte) error
- func (p *StubProtocol) AddSwarm(config SwarmConfig) error
- func (p *StubProtocol) HandleDatagram(d *Datagram, id PeerID) error
- func (p *StubProtocol) NumHandledDatagrams() int
- func (p *StubProtocol) ProtocolState(sid SwarmID, pid PeerID) (ProtocolState, error)
- func (p *StubProtocol) ReadHandledDatagram() (*Datagram, PeerID, error)
- func (p *StubProtocol) SendClosingHandshake(remote PeerID, sid SwarmID) error
- func (p *StubProtocol) SendData(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error
- func (p *StubProtocol) SendHave(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error
- func (p *StubProtocol) SendRequest(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error
- func (p *StubProtocol) SetDatagramSender(f func(Datagram, PeerID) error)
- func (p *StubProtocol) StartHandshake(remote PeerID, sid SwarmID) error
- func (p *StubProtocol) Swarm(id SwarmID) (*Swarm, error)
- type Swarm
- func (s *Swarm) AddLocalChunk(cid ChunkID, c *Chunk) error
- func (s *Swarm) AddLocalChunks(start ChunkID, end ChunkID, data []byte) error
- func (s *Swarm) AddRemoteHave(c ChunkID, p PeerID)
- func (s *Swarm) ChanID(id PeerID) (ChanID, bool)
- func (s *Swarm) ChunkSize() int
- func (s *Swarm) DataFromLocalChunks(start ChunkID, end ChunkID) ([]byte, error)
- func (s *Swarm) LocalChunks() map[ChunkID]*Chunk
- func (s *Swarm) WantChunk(id ChunkID) bool
- type SwarmConfig
- type SwarmDataHandler
- type SwarmID
- type SwarmMetadata
- type WrappedStream
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Chan ¶
type Chan struct {
// contains filtered or unexported fields
}
Chan holds the current state of a channel
type HandshakeMsg ¶
HandshakeMsg holds a handshake message data payload
type Msg ¶
Msg holds a protocol message
func (*Msg) MarshalJSON ¶
MarshalJSON handles the serializing of a message.
See note above UnmarshalJSON for the reason for the custom MarshalJSON
func (*Msg) UnmarshalJSON ¶
UnmarshalJSON handles the deserializing of a message.
We can't get away with off-the-shelf JSON, because we're using an interface type for MsgData, which causes problems on the decode side.
type MsgError ¶
type MsgError struct {
// contains filtered or unexported fields
}
MsgError is an error that happens while handling an incoming message
type Network ¶
type Network interface { // SendDatagram sends a datagram to the remote peer SendDatagram(d Datagram, remote PeerID) error // Connect connects to the remote peer and creates any io resources necessary for the connection Connect(remote PeerID) error // Disconnect disconnects from the remote peer and destroys any io resources created for the connection Disconnect(remote PeerID) error // ID returns the ID of this peer ID() PeerID // SetDatagramHandler sets the function that will be called on receipt of a new datagram // f gets called every time a new Datagram is received. SetDatagramHandler(f func(*Datagram, PeerID) error) // AddAddrs adds multiaddresses for the remote peer to this peer's store AddAddrs(id PeerID, addrs []ma.Multiaddr) // Addrs returns multiaddresses for this peer Addrs() []ma.Multiaddr }
Network handles interactions with the underlying network
type Opcode ¶
type Opcode uint8
Opcode identifies the type of message
From the RFC:
+----------+------------------+ | Msg Type | Description | +----------+------------------+ | 0 | HANDSHAKE | | 1 | DATA | | 2 | ACK | | 3 | HAVE | | 4 | INTEGRITY | | 5 | PEX_RESv4 | | 6 | PEX_REQ | | 7 | SIGNED_INTEGRITY | | 8 | REQUEST | | 9 | CANCEL | | 10 | CHOKE | | 11 | UNCHOKE | | 12 | PEX_RESv6 | | 13 | PEX_REScert | | 14-254 | Unassigned | | 255 | Reserved | +----------+------------------+
type Peer ¶
type Peer struct { // P handles protocol logic. P Protocol // contains filtered or unexported fields }
Peer implements protocol logic and underlying network
func NewLibp2pPeer ¶
NewLibp2pPeer makes a new peer with a libp2p network
func NewPpsppPeer ¶
NewPpsppPeer makes a new peer with a ppspp protocol
func (*Peer) Disconnect ¶
Disconnect closes the stream that p is using to connect to the peer at id
type Ppspp ¶
type Ppspp struct {
// contains filtered or unexported fields
}
Ppspp is a PPSPP implementation
func (*Ppspp) AddLocalChunk ¶
AddLocalChunk stores a chunk locally for the given swarm
func (*Ppspp) AddSwarm ¶
func (p *Ppspp) AddSwarm(config SwarmConfig) error
AddSwarm adds a swarm with a given ID
func (*Ppspp) HandleDatagram ¶
HandleDatagram handles an incoming datagram from a remote peer with the given id
func (*Ppspp) ProtocolState ¶
func (p *Ppspp) ProtocolState(sid SwarmID, pid PeerID) (ProtocolState, error)
ProtocolState returns the current ProtocolState in a swarm for a given remote peer if this returns unknown state, check error for reason
func (*Ppspp) SendClosingHandshake ¶
SendClosingHandshake sends a closing handshake message to the remote peer on swarm sid
func (*Ppspp) SendHave ¶
SendHave sends a have message for the chunk range to the remote peer on the Swarm
func (*Ppspp) SendRequest ¶
SendRequest sends a request for the chunk range to the remote peer on the swarm
func (*Ppspp) SetDatagramSender ¶
SetDatagramSender sets the datagram sender function
func (*Ppspp) StartHandshake ¶
StartHandshake sends a starting handshake message to the remote peer on swarm sid
type Protocol ¶
type Protocol interface { HandleDatagram(d *Datagram, id PeerID) error SetDatagramSender(f func(Datagram, PeerID) error) StartHandshake(remote PeerID, sid SwarmID) error SendClosingHandshake(remote PeerID, sid SwarmID) error ProtocolState(sid SwarmID, pid PeerID) (ProtocolState, error) AddSwarm(config SwarmConfig) error Swarm(id SwarmID) (*Swarm, error) AddLocalChunk(sid SwarmID, cid ChunkID, b []byte) error SendHave(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error SendRequest(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error SendData(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error }
Protocol is the interface to the PPSPP logic
type ProtocolState ¶
type ProtocolState uint
ProtocolState is a per-channel state local to a peer
const ( // Unknown state is used for errors. Unknown ProtocolState = 0 // Begin is the initial state before a handshake. Begin ProtocolState = 1 // WaitHandshake means waiting for ack of the first handshake. WaitHandshake ProtocolState = 2 // Ready means the handshake is complete and the peer is ready for other types of messages. Ready ProtocolState = 3 )
type RequestMsg ¶
RequestMsg holds a have message data payload
type StringPeerID ¶
type StringPeerID struct {
// contains filtered or unexported fields
}
StringPeerID is a simple implementation of PeerID using an underlying string Used for stubs
func (StringPeerID) String ¶
func (s StringPeerID) String() string
type StubNetwork ¶
type StubNetwork struct {
// contains filtered or unexported fields
}
StubNetwork stores all sent datagrams without sending them anywhere
func NewStubNetwork ¶
func NewStubNetwork(s string) *StubNetwork
NewStubNetwork creates a new StubNetwork
func (*StubNetwork) AddAddrs ¶
func (n *StubNetwork) AddAddrs(id PeerID, addrs []ma.Multiaddr)
AddAddrs is a nop for StubNetwork
func (*StubNetwork) Addrs ¶
func (n *StubNetwork) Addrs() []ma.Multiaddr
Addrs is a nop for StubNetwork
func (*StubNetwork) Connect ¶
func (n *StubNetwork) Connect(remote PeerID) error
Connect is a nop for StubNetwork
func (*StubNetwork) Disconnect ¶
func (n *StubNetwork) Disconnect(remote PeerID) error
Disconnect is a nop for StubNetwork
func (*StubNetwork) ID ¶
func (n *StubNetwork) ID() PeerID
ID returns the PeerID for this peer on the network
func (*StubNetwork) InjectIncomingDatagram ¶
func (n *StubNetwork) InjectIncomingDatagram(d *Datagram, id PeerID) error
InjectIncomingDatagram injects a datagram as if it came from the remote peer id
func (*StubNetwork) NumSentDatagrams ¶
func (n *StubNetwork) NumSentDatagrams() int
NumSentDatagrams returns the number of unread sent datagrams
func (*StubNetwork) ReadSentDatagram ¶
func (n *StubNetwork) ReadSentDatagram() *Datagram
ReadSentDatagram pops the oldest sent datagram and returns it Intended to be used by tests that want to inspect what is being sent out on this stub network
func (*StubNetwork) SendDatagram ¶
func (n *StubNetwork) SendDatagram(d Datagram, remote PeerID) error
SendDatagram stores the datagram without sending it anywhere
func (*StubNetwork) SetDatagramHandler ¶
func (n *StubNetwork) SetDatagramHandler(f func(*Datagram, PeerID) error)
SetDatagramHandler sets the datagram handler function for incoming datagrams
type StubProtocol ¶
type StubProtocol struct {
// contains filtered or unexported fields
}
StubProtocol handles incoming datagrams by storing them
func (*StubProtocol) AddLocalChunk ¶
func (p *StubProtocol) AddLocalChunk(sid SwarmID, cid ChunkID, b []byte) error
AddLocalChunk is a noop for StubProtocol
func (*StubProtocol) AddSwarm ¶
func (p *StubProtocol) AddSwarm(config SwarmConfig) error
AddSwarm is a noop for StubProtocol
func (*StubProtocol) HandleDatagram ¶
func (p *StubProtocol) HandleDatagram(d *Datagram, id PeerID) error
HandleDatagram stores incoming datagram, peerid pairs
func (*StubProtocol) NumHandledDatagrams ¶
func (p *StubProtocol) NumHandledDatagrams() int
NumHandledDatagrams returns the number of unread handled datagrams
func (*StubProtocol) ProtocolState ¶
func (p *StubProtocol) ProtocolState(sid SwarmID, pid PeerID) (ProtocolState, error)
ProtocolState is a noop for StubProtocol
func (*StubProtocol) ReadHandledDatagram ¶
func (p *StubProtocol) ReadHandledDatagram() (*Datagram, PeerID, error)
ReadHandledDatagram pops the oldest handled datagram and returns it, along with the peer it came from Intended to be used by tests that want to inspect what is being handled by this stub protocol
func (*StubProtocol) SendClosingHandshake ¶
func (p *StubProtocol) SendClosingHandshake(remote PeerID, sid SwarmID) error
SendClosingHandshake is a noop for StubProtocol
func (*StubProtocol) SendRequest ¶
SendRequest is a noop for StubProtocol
func (*StubProtocol) SetDatagramSender ¶
func (p *StubProtocol) SetDatagramSender(f func(Datagram, PeerID) error)
SetDatagramSender is a noop for StubProtocol
func (*StubProtocol) StartHandshake ¶
func (p *StubProtocol) StartHandshake(remote PeerID, sid SwarmID) error
StartHandshake is a noop for StubProtocol
type Swarm ¶
type Swarm struct {
// contains filtered or unexported fields
}
Swarm tracks info related to a swarm
func (*Swarm) AddLocalChunk ¶
AddLocalChunk stores the chunk locally
func (*Swarm) AddLocalChunks ¶
AddLocalChunks stores a contiguous chunk range, with input data batched in one array
func (*Swarm) AddRemoteHave ¶
AddRemoteHave tells this Swarm that the remote peer p has ChunkID c
func (*Swarm) ChanID ¶
ChanID returns the channel ID for the given peer, also returns bool indicating success of the lookup
func (*Swarm) DataFromLocalChunks ¶
DataFromLocalChunks returns packs the data from the chunk range into a single array
func (*Swarm) LocalChunks ¶
LocalChunks returns the local chunks store for this Swarm
type SwarmConfig ¶
type SwarmConfig struct { // Metadata stores the metadata of a Swarm. See the docs for // SwarmMetadata for details. Metadata SwarmMetadata // DataHandler is invoked in a new goroutine whenever new data chunks // have been received. DataHandler SwarmDataHandler }
SwarmConfig is used to create a Swarm.
type SwarmDataHandler ¶
type SwarmDataHandler func(DataMsg)
SwarmDataHandler is a function that's invoked with new data chunks that have just been received.
type SwarmMetadata ¶
SwarmMetadata stores the metadata of a Swarm See: https://tools.ietf.org/html/rfc7574#section-3.1
type WrappedStream ¶
type WrappedStream struct {
// contains filtered or unexported fields
}
WrappedStream wraps a libp2p stream. We encode/decode whenever we write/read from a stream, so we can just carry the encoders and bufios with us
func WrapStream ¶
func WrapStream(s inet.Stream) *WrappedStream
WrapStream takes a stream and complements it with r/w bufios and decoder/encoder. In order to write raw data to the stream we can use wrap.w.Write(). To encode something into it we can wrap.enc.Encode(). Finally, we should wrap.w.Flush() to actually send the data. Handling incoming data works similarly with wrap.r.Read() for raw-reading and wrap.dec.Decode() to decode.