github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/msgpong.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  	"fmt"
    10  	"io"
    11  )
    12  
    13  // MsgPong implements the Message interface and represents a bitcoin pong
    14  // message which is used primarily to confirm that a connection is still valid
    15  // in response to a bitcoin ping message (MsgPing).
    16  //
    17  // This message was not added until protocol versions AFTER BIP0031Version.
    18  type MsgPong struct {
    19  	// Unique value associated with message that is used to identify
    20  	// specific ping message.
    21  	Nonce uint64
    22  }
    23  
    24  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
    25  // This is part of the Message interface implementation.
    26  func (msg *MsgPong) BtcDecode(r io.Reader, pver uint32) error {
    27  	// NOTE: <= is not a mistake here.  The BIP0031 was defined as AFTER
    28  	// the version unlike most others.
    29  	if pver <= BIP0031Version {
    30  		str := fmt.Sprintf("pong message invalid for protocol "+
    31  			"version %d", pver)
    32  		return messageError("MsgPong.BtcDecode", str)
    33  	}
    34  
    35  	err := readElement(r, &msg.Nonce)
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
    44  // This is part of the Message interface implementation.
    45  func (msg *MsgPong) BtcEncode(w io.Writer, pver uint32) error {
    46  	// NOTE: <= is not a mistake here.  The BIP0031 was defined as AFTER
    47  	// the version unlike most others.
    48  	if pver <= BIP0031Version {
    49  		str := fmt.Sprintf("pong message invalid for protocol "+
    50  			"version %d", pver)
    51  		return messageError("MsgPong.BtcEncode", str)
    52  	}
    53  
    54  	err := writeElement(w, msg.Nonce)
    55  	if err != nil {
    56  		return err
    57  	}
    58  
    59  	return nil
    60  }
    61  
    62  // Command returns the protocol command string for the message.  This is part
    63  // of the Message interface implementation.
    64  func (msg *MsgPong) Command() string {
    65  	return CmdPong
    66  }
    67  
    68  // MaxPayloadLength returns the maximum length the payload can be for the
    69  // receiver.  This is part of the Message interface implementation.
    70  func (msg *MsgPong) MaxPayloadLength(pver uint32) uint32 {
    71  	plen := uint32(0)
    72  	// The pong message did not exist for BIP0031Version and earlier.
    73  	// NOTE: > is not a mistake here.  The BIP0031 was defined as AFTER
    74  	// the version unlike most others.
    75  	if pver > BIP0031Version {
    76  		// Nonce 8 bytes.
    77  		plen += 8
    78  	}
    79  
    80  	return plen
    81  }
    82  
    83  // NewMsgPong returns a new bitcoin pong message that conforms to the Message
    84  // interface.  See MsgPong for details.
    85  func NewMsgPong(nonce uint64) *MsgPong {
    86  	return &MsgPong{
    87  		Nonce: nonce,
    88  	}
    89  }