github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/msgping.go (about)

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