2016-11-11 02:09:14 +01:00
|
|
|
package lnwire
|
|
|
|
|
2021-06-17 08:17:30 +02:00
|
|
|
import (
|
|
|
|
"bytes"
|
2022-01-11 04:11:59 +01:00
|
|
|
"fmt"
|
2021-06-17 08:17:30 +02:00
|
|
|
"io"
|
|
|
|
)
|
2016-11-11 02:09:14 +01:00
|
|
|
|
2022-01-11 04:11:59 +01:00
|
|
|
// MaxPongBytes is the maximum number of extra bytes a pong can be requested to
|
|
|
|
// send. The type of the message (19) takes 2 bytes, the length field takes up
|
|
|
|
// 2 bytes, leaving 65531 bytes.
|
|
|
|
const MaxPongBytes = 65531
|
|
|
|
|
|
|
|
// ErrMaxPongBytesExceeded indicates that the NumPongBytes field from the ping
|
|
|
|
// message has exceeded MaxPongBytes.
|
|
|
|
var ErrMaxPongBytesExceeded = fmt.Errorf("pong bytes exceeded")
|
|
|
|
|
2017-04-17 03:19:03 +02:00
|
|
|
// PongPayload is a set of opaque bytes sent in response to a ping message.
|
2017-04-17 03:11:39 +02:00
|
|
|
type PongPayload []byte
|
|
|
|
|
2017-04-17 00:48:41 +02:00
|
|
|
// Pong defines a message which is the direct response to a received Ping
|
|
|
|
// message. A Pong reply indicates that a connection is still active. The Pong
|
|
|
|
// reply to a Ping message should contain the nonce carried in the original
|
|
|
|
// Pong message.
|
|
|
|
type Pong struct {
|
2017-04-17 03:11:39 +02:00
|
|
|
// PongBytes is a set of opaque bytes that corresponds to the
|
2018-04-18 04:03:27 +02:00
|
|
|
// NumPongBytes defined in the ping message that this pong is
|
2017-04-17 03:11:39 +02:00
|
|
|
// replying to.
|
|
|
|
PongBytes PongPayload
|
2016-11-11 02:09:14 +01:00
|
|
|
}
|
|
|
|
|
2017-04-17 03:11:39 +02:00
|
|
|
// NewPong returns a new Pong message.
|
|
|
|
func NewPong(pongBytes []byte) *Pong {
|
2017-04-17 00:48:41 +02:00
|
|
|
return &Pong{
|
2017-04-17 03:11:39 +02:00
|
|
|
PongBytes: pongBytes,
|
2016-11-11 02:09:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 00:48:41 +02:00
|
|
|
// A compile time check to ensure Pong implements the lnwire.Message interface.
|
|
|
|
var _ Message = (*Pong)(nil)
|
2016-11-11 02:09:14 +01:00
|
|
|
|
2017-04-17 00:48:41 +02:00
|
|
|
// Decode deserializes a serialized Pong 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 *Pong) Decode(r io.Reader, pver uint32) error {
|
2018-12-10 03:27:41 +01:00
|
|
|
return ReadElements(r,
|
2017-04-17 03:11:39 +02:00
|
|
|
&p.PongBytes,
|
2016-11-11 02:09:14 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-04-17 00:48:41 +02:00
|
|
|
// Encode serializes the target Pong 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.
|
2021-06-17 08:17:30 +02:00
|
|
|
func (p *Pong) Encode(w *bytes.Buffer, pver uint32) error {
|
2021-06-18 09:11:43 +02:00
|
|
|
return WritePongPayload(w, p.PongBytes)
|
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 *Pong) MsgType() MessageType {
|
|
|
|
return MsgPong
|
2016-11-11 02:09:14 +01:00
|
|
|
}
|