Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMessageID ¶
func NewMessageID() string
NewMessageID generates a random message identifier TODO - replace with UUIDs
Types ¶
type HandlerFunc ¶
type HandlerFunc func(ws *WrappedStream, msg Message) (hangup bool)
HandlerFunc is the signature of a function that can handle p2p messages
type Message ¶
type Message struct { Type MsgType ID string Created time.Time Deadline time.Time // peer that originated this message Initiator peer.ID // Headers proxies the concept of HTTP headers, but with no // mandatory fields. It's intended to be small & simple on purpose // In the future we can upgrade this to map[string]interface{} while keeping // backward compatibility Headers map[string]string // Body carries the payload of a message, if any Body []byte // contains filtered or unexported fields }
Message is a serializable/encodable object that we send & receive on a Stream.
func NewJSONBodyMessage ¶
NewJSONBodyMessage is a convenience wrapper for json-encoding a message
func NewMessage ¶
NewMessage creates a message. provided initiator should always be the peerID of the local node
func (Message) UpdateJSON ¶
UpdateJSON updates a messages by JSON-encoding a body
func (Message) WithHeaders ¶
WithHeaders adds a sequence of key,value,key,value as headers
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 net.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.
func (*WrappedStream) Close ¶
func (ws *WrappedStream) Close() error
Close closes the stream connection
func (*WrappedStream) ReceiveMessage ¶
func (ws *WrappedStream) ReceiveMessage() (msg Message, err error)
ReceiveMessage reads and decodes a message from the stream
func (*WrappedStream) SendMessage ¶
func (ws *WrappedStream) SendMessage(msg Message) error
SendMessage encodes and writes a message to the stream
func (*WrappedStream) Stream ¶
func (ws *WrappedStream) Stream() net.Stream
Stream exposes the underlying libp2p net.Stream