github.com/lbryio/lbcd@v0.22.119/wire/msgping.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  	"io"
     9  )
    10  
    11  // MsgPing implements the Message interface and represents a bitcoin ping
    12  // message.
    13  //
    14  // For versions BIP0031Version and earlier, it is used primarily to confirm
    15  // that a connection is still valid.  A transmission error is typically
    16  // interpreted as a closed connection and that the peer should be removed.
    17  // For versions AFTER BIP0031Version it contains an identifier which can be
    18  // returned in the pong message to determine network timing.
    19  //
    20  // The payload for this message just consists of a nonce used for identifying
    21  // it later.
    22  type MsgPing struct {
    23  	// Unique value associated with message that is used to identify
    24  	// specific ping message.
    25  	Nonce uint64
    26  }
    27  
    28  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
    29  // This is part of the Message interface implementation.
    30  func (msg *MsgPing) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
    31  	// There was no nonce for BIP0031Version and earlier.
    32  	// NOTE: > is not a mistake here.  The BIP0031 was defined as AFTER
    33  	// the version unlike most others.
    34  	if pver > BIP0031Version {
    35  		err := readElement(r, &msg.Nonce)
    36  		if err != nil {
    37  			return err
    38  		}
    39  	}
    40  
    41  	return nil
    42  }
    43  
    44  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
    45  // This is part of the Message interface implementation.
    46  func (msg *MsgPing) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
    47  	// There was no nonce for BIP0031Version and earlier.
    48  	// NOTE: > is not a mistake here.  The BIP0031 was defined as AFTER
    49  	// the version unlike most others.
    50  	if pver > BIP0031Version {
    51  		err := writeElement(w, msg.Nonce)
    52  		if err != nil {
    53  			return err
    54  		}
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  // Command returns the protocol command string for the message.  This is part
    61  // of the Message interface implementation.
    62  func (msg *MsgPing) Command() string {
    63  	return CmdPing
    64  }
    65  
    66  // MaxPayloadLength returns the maximum length the payload can be for the
    67  // receiver.  This is part of the Message interface implementation.
    68  func (msg *MsgPing) MaxPayloadLength(pver uint32) uint32 {
    69  	plen := uint32(0)
    70  	// There was no nonce for BIP0031Version and earlier.
    71  	// NOTE: > is not a mistake here.  The BIP0031 was defined as AFTER
    72  	// the version unlike most others.
    73  	if pver > BIP0031Version {
    74  		// Nonce 8 bytes.
    75  		plen += 8
    76  	}
    77  
    78  	return plen
    79  }
    80  
    81  // NewMsgPing returns a new bitcoin ping message that conforms to the Message
    82  // interface.  See MsgPing for details.
    83  func NewMsgPing(nonce uint64) *MsgPing {
    84  	return &MsgPing{
    85  		Nonce: nonce,
    86  	}
    87  }