2016-11-11 04:09:14 +03:00
|
|
|
package lnwire
|
|
|
|
|
2017-01-15 04:52:05 +03:00
|
|
|
import "io"
|
2016-11-11 04:09:14 +03:00
|
|
|
|
2017-04-17 04:11:39 +03:00
|
|
|
// PingPayload is a set of opaque bytes used to pad out a ping message.
|
|
|
|
type PingPayload []byte
|
|
|
|
|
2017-04-17 01:48:41 +03:00
|
|
|
// Ping defines a message which is sent by peers periodically to determine if
|
2017-04-17 04:11:39 +03:00
|
|
|
// the connection is still valid. Each ping message carries the number of bytes
|
|
|
|
// to pad the pong response with, and also a number of bytes to be ignored at
|
|
|
|
// the end of the ping message (which is padding).
|
2017-04-17 01:48:41 +03:00
|
|
|
type Ping struct {
|
2017-04-17 04:11:39 +03:00
|
|
|
// NumPongBytes is the number of bytes the pong response to this
|
|
|
|
// message should carry.
|
|
|
|
NumPongBytes uint16
|
|
|
|
|
|
|
|
// PaddingBytes is a set of opaque bytes used to pad out this ping
|
|
|
|
// message. Using this field in conjunction to the one above, it's
|
|
|
|
// possible for node to generate fake cover traffic.
|
|
|
|
PaddingBytes PingPayload
|
2016-11-11 04:09:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 04:11:39 +03:00
|
|
|
// NewPing returns a new Ping message.
|
|
|
|
func NewPing(numBytes uint16) *Ping {
|
2017-04-17 01:48:41 +03:00
|
|
|
return &Ping{
|
2017-04-17 04:11:39 +03:00
|
|
|
NumPongBytes: numBytes,
|
2016-11-11 04:09:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 01:48:41 +03:00
|
|
|
// A compile time check to ensure Ping implements the lnwire.Message interface.
|
|
|
|
var _ Message = (*Ping)(nil)
|
2016-11-11 04:09:14 +03:00
|
|
|
|
2017-04-17 01:48:41 +03:00
|
|
|
// Decode deserializes a serialized Ping message stored in the passed io.Reader
|
2016-11-11 04:09:14 +03:00
|
|
|
// observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-17 01:48:41 +03:00
|
|
|
func (p *Ping) Decode(r io.Reader, pver uint32) error {
|
2017-02-23 21:59:50 +03:00
|
|
|
return readElements(r,
|
2017-04-17 04:11:39 +03:00
|
|
|
&p.NumPongBytes,
|
|
|
|
&p.PaddingBytes)
|
2016-11-11 04:09:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 01:48:41 +03:00
|
|
|
// Encode serializes the target Ping into the passed io.Writer observing the
|
2016-11-11 04:09:14 +03:00
|
|
|
// protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-17 01:48:41 +03:00
|
|
|
func (p *Ping) Encode(w io.Writer, pver uint32) error {
|
2017-02-23 21:59:50 +03:00
|
|
|
return writeElements(w,
|
2017-04-17 04:11:39 +03:00
|
|
|
p.NumPongBytes,
|
|
|
|
p.PaddingBytes)
|
2016-11-11 04:09:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the integer uniquely identifying this message type on the
|
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-17 01:48:41 +03:00
|
|
|
func (p *Ping) Command() uint32 {
|
|
|
|
return CmdPing
|
2016-11-11 04:09:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-17 01:48:41 +03:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for a Ping
|
2016-11-11 04:09:14 +03:00
|
|
|
// complete message observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-17 01:48:41 +03:00
|
|
|
func (p Ping) MaxPayloadLength(uint32) uint32 {
|
2017-04-17 04:11:39 +03:00
|
|
|
return 65532
|
2016-11-11 04:09:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate performs any necessary sanity checks to ensure all fields present
|
2017-04-17 01:48:41 +03:00
|
|
|
// on the Ping are valid.
|
2016-11-11 04:09:14 +03:00
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-17 01:48:41 +03:00
|
|
|
func (p *Ping) Validate() error {
|
2016-11-11 04:09:14 +03:00
|
|
|
return nil
|
|
|
|
}
|