github.com/lbryio/lbcd@v0.22.119/wire/msgpong.go (about)

     1  // Copyright (c) 2013-2015 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package wire
     6  
     7  import (
     8  	"fmt"
     9  	"io"
    10  )
    11  
    12  // MsgPong implements the Message interface and represents a bitcoin pong
    13  // message which is used primarily to confirm that a connection is still valid
    14  // in response to a bitcoin ping message (MsgPing).
    15  //
    16  // This message was not added until protocol versions AFTER BIP0031Version.
    17  type MsgPong struct {
    18  	// Unique value associated with message that is used to identify
    19  	// specific ping message.
    20  	Nonce uint64
    21  }
    22  
    23  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
    24  // This is part of the Message interface implementation.
    25  func (msg *MsgPong) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
    26  	// NOTE: <= is not a mistake here.  The BIP0031 was defined as AFTER
    27  	// the version unlike most others.
    28  	if pver <= BIP0031Version {
    29  		str := fmt.Sprintf("pong message invalid for protocol "+
    30  			"version %d", pver)
    31  		return messageError("MsgPong.BtcDecode", str)
    32  	}
    33  
    34  	return readElement(r, &msg.Nonce)
    35  }
    36  
    37  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
    38  // This is part of the Message interface implementation.
    39  func (msg *MsgPong) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
    40  	// NOTE: <= is not a mistake here.  The BIP0031 was defined as AFTER
    41  	// the version unlike most others.
    42  	if pver <= BIP0031Version {
    43  		str := fmt.Sprintf("pong message invalid for protocol "+
    44  			"version %d", pver)
    45  		return messageError("MsgPong.BtcEncode", str)
    46  	}
    47  
    48  	return writeElement(w, msg.Nonce)
    49  }
    50  
    51  // Command returns the protocol command string for the message.  This is part
    52  // of the Message interface implementation.
    53  func (msg *MsgPong) Command() string {
    54  	return CmdPong
    55  }
    56  
    57  // MaxPayloadLength returns the maximum length the payload can be for the
    58  // receiver.  This is part of the Message interface implementation.
    59  func (msg *MsgPong) MaxPayloadLength(pver uint32) uint32 {
    60  	plen := uint32(0)
    61  	// The pong message did not exist for BIP0031Version and earlier.
    62  	// NOTE: > is not a mistake here.  The BIP0031 was defined as AFTER
    63  	// the version unlike most others.
    64  	if pver > BIP0031Version {
    65  		// Nonce 8 bytes.
    66  		plen += 8
    67  	}
    68  
    69  	return plen
    70  }
    71  
    72  // NewMsgPong returns a new bitcoin pong message that conforms to the Message
    73  // interface.  See MsgPong for details.
    74  func NewMsgPong(nonce uint64) *MsgPong {
    75  	return &MsgPong{
    76  		Nonce: nonce,
    77  	}
    78  }