p2putil

package
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 4, 2021 License: MIT Imports: 8 Imported by: 1

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

func NewJSONBodyMessage(initiator peer.ID, t MsgType, body interface{}) (Message, error)

NewJSONBodyMessage is a convenience wrapper for json-encoding a message

func NewMessage

func NewMessage(initiator peer.ID, t MsgType, body []byte) Message

NewMessage creates a message. provided initiator should always be the peerID of the local node

func (Message) Header

func (m Message) Header(key string) (value string)

Header gets a header value for a given key

func (Message) Update

func (m Message) Update(body []byte) Message

Update returns a new message with an updated body

func (Message) UpdateJSON

func (m Message) UpdateJSON(body interface{}) (Message, error)

UpdateJSON updates a messages by JSON-encoding a body

func (Message) WithHeaders

func (m Message) WithHeaders(keyval ...string) Message

WithHeaders adds a sequence of key,value,key,value as headers

type MsgType

type MsgType string

MsgType indicates the type of message being sent

func (MsgType) String

func (mt MsgType) String() string

String implements the Stringer interface for MsgType

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL