2016-11-11 02:09:14 +01:00
|
|
|
package lnwire
|
|
|
|
|
2017-01-15 02:52:05 +01:00
|
|
|
import "io"
|
2016-11-11 02:09:14 +01:00
|
|
|
|
2017-04-17 03:11:39 +02:00
|
|
|
// PingPayload is a set of opaque bytes used to pad out a ping message.
|
|
|
|
type PingPayload []byte
|
|
|
|
|
2017-04-17 00:48:41 +02:00
|
|
|
// Ping defines a message which is sent by peers periodically to determine if
|
2017-04-17 03:11:39 +02: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 00:48:41 +02:00
|
|
|
type Ping struct {
|
2017-04-17 03:11:39 +02: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 02:09:14 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 03:11:39 +02:00
|
|
|
// NewPing returns a new Ping message.
|
|
|
|
func NewPing(numBytes uint16) *Ping {
|
2017-04-17 00:48:41 +02:00
|
|
|
return &Ping{
|
2017-04-17 03:11:39 +02:00
|
|
|
NumPongBytes: numBytes,
|
2016-11-11 02:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 00:48:41 +02:00
|
|
|
// A compile time check to ensure Ping implements the lnwire.Message interface.
|
|
|
|
var _ Message = (*Ping)(nil)
|
2016-11-11 02:09:14 +01:00
|
|
|
|
2017-04-17 00:48:41 +02:00
|
|
|
// Decode deserializes a serialized Ping message stored in the passed io.Reader
|
2016-11-11 02:09:14 +01:00
|
|
|
// observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-17 00:48:41 +02:00
|
|
|
func (p *Ping) Decode(r io.Reader, pver uint32) error {
|
2017-02-23 19:59:50 +01:00
|
|
|
return readElements(r,
|
2017-04-17 03:11:39 +02:00
|
|
|
&p.NumPongBytes,
|
|
|
|
&p.PaddingBytes)
|
2016-11-11 02:09:14 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 00:48:41 +02:00
|
|
|
// Encode serializes the target Ping into the passed io.Writer observing the
|
2016-11-11 02:09:14 +01:00
|
|
|
// protocol version specified.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-17 00:48:41 +02:00
|
|
|
func (p *Ping) Encode(w io.Writer, pver uint32) error {
|
2017-02-23 19:59:50 +01:00
|
|
|
return writeElements(w,
|
2017-04-17 03:11:39 +02:00
|
|
|
p.NumPongBytes,
|
|
|
|
p.PaddingBytes)
|
2016-11-11 02:09:14 +01:00
|
|
|
}
|
|
|
|
|
2017-04-20 00:57:43 +02:00
|
|
|
// MsgType returns the integer uniquely identifying this message type on the
|
2016-11-11 02:09:14 +01:00
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-20 00:57:43 +02:00
|
|
|
func (p *Ping) MsgType() MessageType {
|
|
|
|
return MsgPing
|
2016-11-11 02:09:14 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 00:48:41 +02:00
|
|
|
// MaxPayloadLength returns the maximum allowed payload size for a Ping
|
2016-11-11 02:09:14 +01:00
|
|
|
// complete message observing the specified protocol version.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-17 00:48:41 +02:00
|
|
|
func (p Ping) MaxPayloadLength(uint32) uint32 {
|
2017-04-17 03:11:39 +02:00
|
|
|
return 65532
|
2016-11-11 02:09:14 +01:00
|
|
|
}
|