p2p

package
v0.9.9 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: MIT Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LoggerHandler = HandlerFunc(func(envelope *Envelope) {
	data, err := json.Marshal(envelope)
	if err != nil {
		zlog.Error("marshal err", zap.Error(err))
		return
	}

	zlog.Info("handler", zap.String("message", string(data)))
})

LoggerHandler logs the messages back and forth.

View Source
var StringLoggerHandler = HandlerFunc(func(envelope *Envelope) {
	name, _ := envelope.Packet.Type.Name()
	zlog.Info(
		"handler Packet",
		zap.String("name", name),
		zap.String("sender", envelope.Sender.Address),
		zap.String("receiver", envelope.Receiver.Address),
		zap.Stringer("msg", envelope.Packet.P2PMessage),
	)
})

StringLoggerHandler simply prints the messages as they go through the client.

Functions

func DecodeHex

func DecodeHex(hexString string) (data []byte)

func SyncLogger

func SyncLogger()

SyncLogger sync logger, should `defer SyncLogger()` when use p2p package

Types

type Catchup

type Catchup struct {
	IsCatchingUp bool
	// contains filtered or unexported fields
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(peer *Peer, catchup bool) *Client

func (*Client) CloseConnection

func (c *Client) CloseConnection() error

func (*Client) RegisterHandler

func (c *Client) RegisterHandler(handler Handler)

func (*Client) SetReadTimeout

func (c *Client) SetReadTimeout(readTimeout time.Duration)

func (*Client) Start

func (c *Client) Start() error

type Envelope

type Envelope struct {
	Sender   *Peer
	Receiver *Peer
	Packet   *zsw.Packet `json:"envelope"`
}

func NewEnvelope

func NewEnvelope(sender *Peer, receiver *Peer, packet *zsw.Packet) *Envelope

type Handler

type Handler interface {
	Handle(envelope *Envelope)
}

type HandlerFunc

type HandlerFunc func(envelope *Envelope)

func (HandlerFunc) Handle

func (f HandlerFunc) Handle(envelope *Envelope)

type HandshakeInfo

type HandshakeInfo struct {
	ChainID                  zsw.Checksum256
	HeadBlockNum             uint32
	HeadBlockID              zsw.Checksum256
	HeadBlockTime            time.Time
	LastIrreversibleBlockNum uint32
	LastIrreversibleBlockID  zsw.Checksum256
}

func (HandshakeInfo) MarshalLogObject

func (h HandshakeInfo) MarshalLogObject(enc zapcore.ObjectEncoder) error

MarshalLogObject calls the underlying function from zap.

func (*HandshakeInfo) String

func (h *HandshakeInfo) String() string

type Peer

type Peer struct {
	Address string
	Name    string

	NodeID []byte
	// contains filtered or unexported fields
}

func NewIncommingPeer

func NewIncommingPeer(address string, agent string) *Peer

func NewOutgoingPeer

func NewOutgoingPeer(address string, agent string, handshakeInfo *HandshakeInfo) *Peer

func (*Peer) Connect

func (p *Peer) Connect(errChan chan error) (ready chan bool)

func (Peer) MarshalLogObject

func (p Peer) MarshalLogObject(enc zapcore.ObjectEncoder) error

MarshalLogObject calls the underlying function from zap.

func (*Peer) Read

func (p *Peer) Read() (*zsw.Packet, error)

func (*Peer) SendHandshake

func (p *Peer) SendHandshake(info *HandshakeInfo) error

func (*Peer) SendNotice

func (p *Peer) SendNotice(headBlockNum uint32, libNum uint32, mode byte) error

func (*Peer) SendRequest

func (p *Peer) SendRequest(startBlockNum uint32, endBlockNumber uint32) (err error)

func (*Peer) SendSyncRequest

func (p *Peer) SendSyncRequest(startBlockNum uint32, endBlockNumber uint32) (err error)

func (*Peer) SendTime

func (p *Peer) SendTime() error

func (*Peer) SetConnection

func (p *Peer) SetConnection(conn net.Conn)

func (*Peer) SetConnectionTimeout

func (p *Peer) SetConnectionTimeout(timeout time.Duration)

func (*Peer) SetHandshakeTimeout

func (p *Peer) SetHandshakeTimeout(timeout time.Duration)

func (*Peer) Write

func (p *Peer) Write(bytes []byte) (int, error)

func (*Peer) WriteP2PMessage

func (p *Peer) WriteP2PMessage(message zsw.P2PMessage) (err error)

type Proxy

type Proxy struct {
	Peer1 *Peer
	Peer2 *Peer
	// contains filtered or unexported fields
}

func NewProxy

func NewProxy(peer1 *Peer, peer2 *Peer) *Proxy

func (*Proxy) ConnectAndStart

func (p *Proxy) ConnectAndStart() error

func (*Proxy) RegisterHandler

func (p *Proxy) RegisterHandler(handler Handler)

func (*Proxy) RegisterHandlers

func (p *Proxy) RegisterHandlers(handlers []Handler)

func (*Proxy) Start

func (p *Proxy) Start() error

type Relay

type Relay struct {
	// contains filtered or unexported fields
}

func NewRelay

func NewRelay(listeningAddress string, destinationPeerAddress string) *Relay

func (*Relay) RegisterHandler

func (r *Relay) RegisterHandler(handler Handler)

func (*Relay) Start

func (r *Relay) Start() error

Jump to

Keyboard shortcuts

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